rocketmq_error/unified/
serialization.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//! Serialization and deserialization errors
19
20use thiserror::Error;
21
22/// Serialization/Deserialization errors
23#[derive(Debug, Error)]
24pub enum SerializationError {
25    /// Encoding failed
26    #[error("Encoding failed ({format}): {message}")]
27    EncodeFailed {
28        format: &'static str,
29        message: String,
30    },
31
32    /// Decoding failed
33    #[error("Decoding failed ({format}): {message}")]
34    DecodeFailed {
35        format: &'static str,
36        message: String,
37    },
38
39    /// Invalid data format
40    #[error("Invalid format: expected {expected}, got {got}")]
41    InvalidFormat { expected: &'static str, got: String },
42
43    /// Missing required field
44    #[error("Missing required field: {field}")]
45    MissingField { field: &'static str },
46
47    /// Invalid field value
48    #[error("Invalid value for field '{field}': {reason}")]
49    InvalidValue { field: &'static str, reason: String },
50
51    /// UTF-8 encoding error
52    #[error("UTF-8 encoding error: {0}")]
53    Utf8Error(#[from] std::str::Utf8Error),
54
55    /// JSON serialization error
56    #[cfg(feature = "with_serde")]
57    #[error("JSON error: {0}")]
58    JsonError(String),
59
60    /// Protobuf serialization error
61    #[error("Protobuf error: {0}")]
62    ProtobufError(String),
63}
64
65impl SerializationError {
66    /// Create an encode failed error
67    #[inline]
68    pub fn encode_failed(format: &'static str, message: impl Into<String>) -> Self {
69        Self::EncodeFailed {
70            format,
71            message: message.into(),
72        }
73    }
74
75    /// Create a decode failed error
76    #[inline]
77    pub fn decode_failed(format: &'static str, message: impl Into<String>) -> Self {
78        Self::DecodeFailed {
79            format,
80            message: message.into(),
81        }
82    }
83
84    /// Create an invalid format error
85    #[inline]
86    pub fn invalid_format(expected: &'static str, got: impl Into<String>) -> Self {
87        Self::InvalidFormat {
88            expected,
89            got: got.into(),
90        }
91    }
92
93    /// Create a missing field error
94    #[inline]
95    pub fn missing_field(field: &'static str) -> Self {
96        Self::MissingField { field }
97    }
98}
99
100#[cfg(feature = "with_serde")]
101impl From<serde_json::Error> for SerializationError {
102    fn from(e: serde_json::Error) -> Self {
103        Self::JsonError(e.to_string())
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn test_serialization_error() {
113        let err = SerializationError::encode_failed("JSON", "unexpected token");
114        assert!(err.to_string().contains("Encoding failed"));
115        assert!(err.to_string().contains("JSON"));
116    }
117
118    #[test]
119    fn test_missing_field() {
120        let err = SerializationError::missing_field("broker_name");
121        assert_eq!(err.to_string(), "Missing required field: broker_name");
122    }
123}