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 error
26    #[error("XML serialization error: {0}")]
27    XmlSerialization(String),
28
29    /// XML deserialization error
30    #[error("XML deserialization error: {0}")]
31    XmlDeserialization(String),
32
33    /// XML validation error
34    #[error("XML validation error: {0}")]
35    XmlValidation(String),
36
37    /// General XML error
38    #[error("XML error: {0}")]
39    Xml(String),
40
41    /// JSON serialization/deserialization error
42    #[error("JSON error: {0}")]
43    Json(#[from] serde_json::Error),
44
45    /// Validation error with details
46    #[error("Validation error: {message}")]
47    Validation {
48        code: u32,
49        message: String,
50        field: Option<String>,
51        path: Option<String>,
52    },
53
54    /// Format detection error
55    #[error("Cannot detect message format")]
56    FormatDetection,
57
58    /// Unknown message type
59    #[error("Unknown message type: {0}")]
60    UnknownMessageType(String),
61
62    /// IO error
63    #[error("IO error: {0}")]
64    Io(#[from] std::io::Error),
65}
66
67/// Legacy ValidationError for backward compatibility
68#[derive(Debug, Clone, PartialEq)]
69pub struct ValidationError {
70    pub code: u32,
71    pub message: String,
72    pub field: Option<String>,
73    pub path: Option<String>,
74}
75
76impl ValidationError {
77    pub fn new(code: u32, message: String) -> Self {
78        ValidationError {
79            code,
80            message,
81            field: None,
82            path: None,
83        }
84    }
85
86    pub fn with_field(mut self, field: String) -> Self {
87        self.field = Some(field);
88        self
89    }
90
91    pub fn with_path(mut self, path: String) -> Self {
92        self.path = Some(path);
93        self
94    }
95}
96
97impl From<ValidationError> for MxError {
98    fn from(err: ValidationError) -> Self {
99        MxError::Validation {
100            code: err.code,
101            message: err.message,
102            field: err.field,
103            path: err.path,
104        }
105    }
106}