-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderive_error.rs
More file actions
136 lines (122 loc) · 3.92 KB
/
Copy pathderive_error.rs
File metadata and controls
136 lines (122 loc) · 3.92 KB
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
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
//
// SPDX-License-Identifier: MIT
//! Derive Error example showing thiserror compatibility and AppError mapping.
//!
//! Run with:
//! ```sh
//! cargo run --example derive_error
//! ```
use std::{error::Error as StdError, io};
use masterror::{AppCode, AppError, AppErrorKind, Error};
#[derive(Debug, Error)]
#[error("I/O failed: {source}")]
struct IoWrapperError {
#[from]
#[source]
source: io::Error
}
#[derive(Debug, Error)]
#[error("User {user_id} not found")]
#[app_error(kind = AppErrorKind::NotFound, code = AppCode::NotFound, message)]
struct UserNotFound {
user_id: String
}
#[derive(Debug, Error)]
#[error("Database connection failed")]
#[app_error(kind = AppErrorKind::Database, code = AppCode::Database)]
struct DatabaseError {
#[source]
cause: io::Error
}
#[derive(Debug, Error)]
enum ServiceError {
#[error("Authentication failed: {reason}")]
#[app_error(kind = AppErrorKind::Unauthorized, code = AppCode::Unauthorized, message)]
AuthFailed { reason: String },
#[error("Rate limit exceeded")]
#[app_error(kind = AppErrorKind::RateLimited, code = AppCode::RateLimited, message)]
RateLimited,
#[error(transparent)]
#[app_error(kind = AppErrorKind::Database, code = AppCode::Database)]
Database(#[from] DatabaseError)
}
fn simulate_io_error() -> Result<(), IoWrapperError> {
Err(io::Error::other("disk offline").into())
}
fn find_user(user_id: &str) -> Result<String, UserNotFound> {
if user_id.is_empty() {
return Err(UserNotFound {
user_id: user_id.to_string()
});
}
Ok(format!("User: {user_id}"))
}
fn connect_database() -> Result<(), DatabaseError> {
Err(DatabaseError {
cause: io::Error::other("connection refused")
})
}
fn authenticate(valid: bool) -> Result<(), ServiceError> {
if !valid {
return Err(ServiceError::AuthFailed {
reason: "invalid token".to_string()
});
}
Ok(())
}
fn main() {
println!("=== thiserror Compatibility ===\n");
match simulate_io_error() {
Ok(()) => println!("I/O succeeded"),
Err(e) => {
println!("Error: {e}");
println!("Source: {:?}", e.source());
}
}
println!("\n=== AppError Mapping ===\n");
match find_user("") {
Ok(user) => println!("Found: {user}"),
Err(e) => {
println!("Domain error: {e}");
let app_error: AppError = e.into();
println!("AppError kind: {:?}", app_error.kind);
println!("AppError code: {:?}", app_error.code);
println!("Message exposed: {:?}", app_error.message);
}
}
println!("\n=== Error Source Chain ===\n");
match connect_database() {
Ok(()) => println!("Connected"),
Err(e) => {
println!("Domain error: {e}");
let app_error: AppError = e.into();
println!("Has source: {}", app_error.source_ref().is_some());
if let Some(source) = app_error.source_ref() {
println!("Source: {source}");
}
}
}
println!("\n=== Enum Variants ===\n");
match authenticate(false) {
Ok(()) => println!("Authenticated"),
Err(e) => {
println!("Service error: {e}");
let app_error: AppError = e.into();
println!("Kind: {:?}", app_error.kind);
println!("Code: {:?}", app_error.code);
}
}
let rate_limit_err = ServiceError::RateLimited;
println!("\nRate limit error: {rate_limit_err}");
let app_error: AppError = rate_limit_err.into();
println!("Kind: {:?}", app_error.kind);
match connect_database().map_err(ServiceError::from) {
Ok(()) => println!("Connected"),
Err(e) => {
println!("\nService error: {e}");
let app_error: AppError = e.into();
println!("Kind: {:?}", app_error.kind);
}
}
}