mx_message/
error.rs

1// Plasmatic MX Message Parsing Library
2// https://github.com/GoPlasmatic/MXMessage
3//
4// Copyright (c) 2025 Plasmatic
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// 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// You may obtain a copy of this library at
18// https://github.com/GoPlasmatic/MXMessage
19
20use thiserror::Error;
21
22/// MX Message processing errors
23#[derive(Error, Debug)]
24pub enum MxError {
25    /// XML serialization/deserialization error
26    #[error("XML error: {0}")]
27    Xml(String),
28
29    /// JSON serialization/deserialization error
30    #[error("JSON error: {0}")]
31    Json(#[from] serde_json::Error),
32
33    /// Validation error with details
34    #[error("Validation error: {message}")]
35    Validation {
36        code: u32,
37        message: String,
38        field: Option<String>,
39        path: Option<String>,
40    },
41
42    /// Format detection error
43    #[error("Cannot detect message format")]
44    FormatDetection,
45
46    /// Unknown message type
47    #[error("Unknown message type: {0}")]
48    UnknownMessageType(String),
49
50    /// IO error
51    #[error("IO error: {0}")]
52    Io(#[from] std::io::Error),
53}
54
55/// Legacy ValidationError for backward compatibility
56#[derive(Debug, Clone, PartialEq)]
57pub struct ValidationError {
58    pub code: u32,
59    pub message: String,
60    pub field: Option<String>,
61    pub path: Option<String>,
62}
63
64impl ValidationError {
65    pub fn new(code: u32, message: String) -> Self {
66        ValidationError {
67            code,
68            message,
69            field: None,
70            path: None,
71        }
72    }
73
74    pub fn with_field(mut self, field: String) -> Self {
75        self.field = Some(field);
76        self
77    }
78
79    pub fn with_path(mut self, path: String) -> Self {
80        self.path = Some(path);
81        self
82    }
83}
84
85impl From<ValidationError> for MxError {
86    fn from(err: ValidationError) -> Self {
87        MxError::Validation {
88            code: err.code,
89            message: err.message,
90            field: err.field,
91            path: err.path,
92        }
93    }
94}