rocketmq_error/unified/
protocol.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//! RocketMQ protocol-specific errors
19
20use thiserror::Error;
21
22/// Protocol validation and processing errors
23#[derive(Debug, Error)]
24pub enum ProtocolError {
25    /// Invalid command code
26    #[error("Invalid command code: {code}")]
27    InvalidCommand { code: i32 },
28
29    /// Unsupported protocol version
30    #[error("Unsupported protocol version: {version}")]
31    UnsupportedVersion { version: i32 },
32
33    /// Required header field is missing
34    #[error("Missing required header field: {field}")]
35    HeaderMissing { field: &'static str },
36
37    /// Required body is missing
38    #[error("Missing required message body")]
39    BodyMissing,
40
41    /// Checksum mismatch
42    #[error("Checksum mismatch: expected {expected:x}, got {actual:x}")]
43    ChecksumMismatch { expected: u32, actual: u32 },
44
45    /// Invalid message format
46    #[error("Invalid message format: {reason}")]
47    InvalidMessage { reason: String },
48
49    /// Protocol decode error
50    #[error(
51        "Protocol decode error: ext_fields_length={ext_fields_len}, header_length={header_len}"
52    )]
53    DecodeError {
54        ext_fields_len: usize,
55        header_len: usize,
56    },
57
58    /// Unsupported serialization type
59    #[error("Unsupported serialization type: {serialize_type}")]
60    UnsupportedSerializationType { serialize_type: u8 },
61}
62
63impl ProtocolError {
64    /// Create an invalid command error
65    #[inline]
66    pub fn invalid_command(code: i32) -> Self {
67        Self::InvalidCommand { code }
68    }
69
70    /// Create a header missing error
71    #[inline]
72    pub fn header_missing(field: &'static str) -> Self {
73        Self::HeaderMissing { field }
74    }
75
76    /// Create a checksum mismatch error
77    #[inline]
78    pub fn checksum_mismatch(expected: u32, actual: u32) -> Self {
79        Self::ChecksumMismatch { expected, actual }
80    }
81
82    /// Create an invalid message error
83    #[inline]
84    pub fn invalid_message(reason: impl Into<String>) -> Self {
85        Self::InvalidMessage {
86            reason: reason.into(),
87        }
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn test_protocol_error() {
97        let err = ProtocolError::invalid_command(999);
98        assert_eq!(err.to_string(), "Invalid command code: 999");
99    }
100
101    #[test]
102    fn test_checksum_mismatch() {
103        let err = ProtocolError::checksum_mismatch(0xABCD, 0x1234);
104        assert!(err.to_string().contains("abcd"));
105        assert!(err.to_string().contains("1234"));
106    }
107}