rocketmq_remoting/
error_helpers.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18//! Error helper functions for rocketmq-remoting
19//!
20//! This module provides convenient helper functions to create unified errors
21//! for common remoting scenarios.
22
23use rocketmq_error::RocketMQError;
24
25/// Create an I/O error from std::io::Error
26#[inline]
27pub fn io_error(err: std::io::Error) -> RocketMQError {
28    RocketMQError::IO(err)
29}
30
31/// Create a connection invalid error
32#[inline]
33pub fn connection_invalid(msg: impl Into<String>) -> RocketMQError {
34    RocketMQError::network_connection_failed("unknown", msg)
35}
36
37/// Create a remote error
38#[inline]
39pub fn remote_error(msg: impl Into<String>) -> RocketMQError {
40    RocketMQError::network_connection_failed("remote", msg)
41}
42
43/// Create a remoting command decoder error
44#[inline]
45pub fn decoder_error(ext_fields_len: usize, header_len: usize) -> RocketMQError {
46    RocketMQError::Protocol(rocketmq_error::unified::ProtocolError::DecodeError {
47        ext_fields_len,
48        header_len,
49    })
50}
51
52/// Create a deserialize header error
53#[inline]
54pub fn deserialize_header_error(msg: impl Into<String>) -> RocketMQError {
55    RocketMQError::Serialization(rocketmq_error::unified::SerializationError::DecodeFailed {
56        format: "header",
57        message: msg.into(),
58    })
59}
60
61/// Create a decoding error
62#[inline]
63pub fn decoding_error(required: usize, available: usize) -> RocketMQError {
64    RocketMQError::Serialization(rocketmq_error::unified::SerializationError::DecodeFailed {
65        format: "binary",
66        message: format!("required {} bytes, got {}", required, available),
67    })
68}
69
70/// Create an unsupported serialize type error
71#[inline]
72pub fn unsupported_serialize_type(serialize_type: u8) -> RocketMQError {
73    RocketMQError::Protocol(
74        rocketmq_error::unified::ProtocolError::UnsupportedSerializationType { serialize_type },
75    )
76}
77
78/// Create an illegal argument error
79#[inline]
80pub fn illegal_argument(msg: impl Into<String>) -> RocketMQError {
81    RocketMQError::illegal_argument(msg)
82}
83
84/// Create a channel send request failed error
85#[inline]
86pub fn channel_send_failed(msg: impl Into<String>) -> RocketMQError {
87    RocketMQError::network_connection_failed("channel", format!("send failed: {}", msg.into()))
88}
89
90/// Create a channel receive request failed error
91#[inline]
92pub fn channel_recv_failed(msg: impl Into<String>) -> RocketMQError {
93    RocketMQError::network_connection_failed("channel", format!("receive failed: {}", msg.into()))
94}
95
96/// Create an abort process error
97#[inline]
98pub fn abort_process_error(code: i32, msg: impl Into<String>) -> RocketMQError {
99    RocketMQError::Internal(format!("Abort process error {}: {}", code, msg.into()))
100}
101
102/// Create a remoting command encoder error
103#[inline]
104pub fn encoder_error(msg: impl Into<String>) -> RocketMQError {
105    RocketMQError::Serialization(rocketmq_error::unified::SerializationError::EncodeFailed {
106        format: "command",
107        message: msg.into(),
108    })
109}