1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! [](./LICENSE)
//! [](https://crates.io/crates/oxiderr)
//! [](https://docs.rs/oxiderr)
//! [](https://github.com/cdumay/oxiderr)
//!
//! `oxiderr` is a Rust library designed for extended error management. It leverages the `oxiderr-derive` crate, which provides procedural macros
//! to simplify the definition of structured error types. The primary goal of `oxiderr` is to enhance error handling in Rust applications by
//! making error definition more declarative and reducing boilerplate code.
//!
//! # Features
//!
//! * Provides extended error management capabilities.
//! * Implements the `oxiderr::AsError` trait for easy integration.
//! * Supports structured error kinds and categorized error handling.
//!
//! # Usage
//!
//! To utilize oxiderr in your project, follow these steps:
//!
//! 1. **Add Dependencies**: Include `oxiderr` with the feature `derive` in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! oxiderr = "0.1"
//! ```
//!
//! 2. **Define Error**: Define `oxiderr::ErrorKind` and struct which implement `oxiderr::AsError` to handle an error:
//!
//! ```rust
//! #[allow(non_upper_case_globals)]
//! pub const IoError: oxiderr::ErrorKind = oxiderr::ErrorKind(
//! "IoError",
//! "Input / output error",
//! 500,
//! "The requested file raised error"
//! );
//! #[derive(Debug, Clone)]
//! pub struct NotFoundError {
//! class: String,
//! message: String,
//! details: Option<std::collections::BTreeMap<String, serde_value::Value>>,
//! }
//!
//! impl NotFoundError {
//! pub const kind: oxiderr::ErrorKind = IoError;
//!
//! pub fn new() -> Self {
//! Self {
//! class: format!("{}::{}::{}", Self::kind.side(), Self::kind.name(), "NotFoundError"),
//! message: Self::kind.description().into(),
//! details: None,
//! }
//! }
//!
//! pub fn set_message(mut self, message: String) -> Self {
//! self.message = message;
//! self
//! }
//!
//! pub fn set_details(mut self, details: std::collections::BTreeMap<String, serde_value::Value>) -> Self {
//! self.details = Some(details);
//! self
//! }
//!
//! pub fn convert(error: oxiderr::Error) -> Self {
//! let mut err_clone = error.clone();
//! let mut details = error.details.unwrap_or_default();
//! err_clone.details = None;
//! details.insert("origin".to_string(), serde_value::to_value(err_clone).unwrap());
//!
//! Self {
//! class: format!("{}::{}::{}", Self::kind.side(), Self::kind.name(), "NotFoundError"),
//! message: Self::kind.description().into(),
//! details: Some(details),
//! }
//! }
//! }
//!
//! impl oxiderr::AsError for NotFoundError {
//! fn kind() -> oxiderr::ErrorKind {
//! Self::kind
//! }
//! fn class(&self) -> String {
//! self.class.clone()
//! }
//! fn message(&self) -> String {
//! self.message.clone()
//! }
//! fn details(&self) -> Option<std::collections::BTreeMap<String, serde_value::Value>> {
//! self.details.clone()
//! }
//! }
//!
//! impl std::error::Error for NotFoundError {}
//!
//! impl std::fmt::Display for NotFoundError {
//! fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! write!(f, "[{}] {} ({}): {}", Self::kind.message_id(), "NotFoundError", Self::kind.code(), self.message())
//! }
//! }
//! ```
//!
//! In this example:
//!
//! * we create the struct `IoError` as `oxiderr::ErrorKind`
//! * we create the struct `NotFoundError` which implements `oxiderr::AsError`
//!
//! 3. Implementing Error Handling: With the above definitions, you can now handle errors in your application as follows:
//!
//! ```rust
//! use std::fs::File;
//! use std::io::Read;
//!
//! fn try_open_file(path: &str) -> oxiderr::Result<File> {
//! Ok(File::open(path).map_err(|err| NotFoundError::new().set_message(err.to_string()))?)
//! }
//!
//! fn main() {
//! let path = "example.txt";
//!
//! match try_open_file(path) {
//! Ok(file) => println!("File: {:?}", file),
//! Err(e) => eprintln!("{}", e),
//! }
//! }
//! ```
//! This will output:
//!
//! ```text
//! [Err-00001] Client::IoError::NotFoundError (500) - No such file or directory (os error 2)
//! ```
//!
//! # Macros
//!
//! To automatically generate implementations for custom error types, enable the feature `derive` in your Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! oxiderr = { version = "0.1", features = ["derive"] }
//! ```
//!
//! Then, use the provided derive macros to define your error and error kind structs:
//!
//! ```rust
//! use oxiderr::{define_errors, define_kinds, AsError};
//!
//! define_kinds! {
//! UnknownError = ("Err-00001", 500, "Unexpected error"),
//! IoError = ("Err-00001", 400, "IO error")
//! }
//! define_errors! {
//! Unexpected = UnknownError,
//! FileRead = IoError,
//! FileNotExists = IoError
//! }
//! ```
//!
//! See [oxiderr-derive](https://docs.rs/oxiderr-derive) documentation for more information.
//!
pub use *;
pub use *;
/// A type alias for `Result<T, Error>`.
///
/// This alias simplifies the usage of `Result` in the context of errors in your application.
/// Instead of writing out `std::result::Result<T, Error>` every time, you can now use `Result<T>`
/// for better readability and convenience.
///
/// # Example
/// ```
/// fn example() -> oxiderr::Result<i32> {
/// Err(oxiderr::Error::new(...))
/// }
/// ```
pub type Result<T> = Result;
pub use *;