FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
masterror/examples/basic_usage.rs at main · RAprogramm/masterror · GitHub
RAprogramm
/
masterror
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
0
Star
7
Code
Issues
1
Pull requests
1
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
masterror
/
examples
/
basic_usage.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
99 lines (92 loc) · 3.15 KB
Breadcrumbs
masterror
/
examples
/
basic_usage.rs
Copy path
File metadata and controls
99 lines (92 loc) · 3.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
//
// SPDX-License-Identifier: MIT
//! Basic usage example showing the simplest ways to create and use errors.
//!
//! Run with:
//! ```sh
//! cargo run --example basic_usage
//! ```
use
masterror
::
{
AppError
,
AppErrorKind
,
AppResult
,
field
}
;
fn
validate_age
(
age
:
i32
)
->
AppResult
<
(
)
>
{
masterror
::
ensure!
(
age >=
0
,
AppError
::
bad_request
(
"Age cannot be negative"
)
)
;
masterror
::
ensure!
(
age <=
150
,
AppError
::
bad_request
(
"Age seems unrealistic"
)
)
;
Ok
(
(
)
)
}
fn
fetch_user
(
id
:
&
str
)
->
AppResult
<
String
>
{
if
id
.
is_empty
(
)
{
masterror
::
fail!
(
AppError
::
bad_request
(
"User ID cannot be empty"
)
)
;
}
if
id ==
"404"
{
return
Err
(
AppError
::
not_found
(
"User not found"
)
)
;
}
Ok
(
format
!
(
"User {id}"
)
)
}
fn
process_request
(
user_id
:
&
str
,
age
:
i32
)
->
AppResult
<
String
>
{
validate_age
(
age
)
?
;
let
user =
fetch_user
(
user_id
)
?
;
Ok
(
format
!
(
"{user} is {age} years old"
)
)
}
fn
database_operation
(
)
->
AppResult
<
(
)
>
{
let
err =
AppError
::
database_with_message
(
"Connection failed"
)
.
with_field
(
field
::
str
(
"host"
,
"localhost"
)
)
.
with_field
(
field
::
u64
(
"port"
,
5432
)
)
.
with_field
(
field
::
duration
(
"timeout"
,
std
::
time
::
Duration
::
from_secs
(
5
)
)
)
;
Err
(
err
)
}
fn
main
(
)
{
println
!
(
"=== Basic Error Creation ===
\n
"
)
;
let
err =
AppError
::
new
(
AppErrorKind
::
BadRequest
,
"Invalid input"
)
;
println
!
(
"Simple error: {err}"
)
;
println
!
(
"Error kind: {:?}"
,
err
.
kind
)
;
println
!
(
"Error code: {:?}
\n
"
,
err
.
code
)
;
println
!
(
"=== Using ensure! macro ===
\n
"
)
;
match
validate_age
(
-
5
)
{
Ok
(
(
)
)
=>
println
!
(
"Age valid"
)
,
Err
(
e
)
=>
println
!
(
"Validation failed: {e}"
)
}
match
validate_age
(
200
)
{
Ok
(
(
)
)
=>
println
!
(
"Age valid"
)
,
Err
(
e
)
=>
println
!
(
"Validation failed: {e}"
)
}
match
validate_age
(
25
)
{
Ok
(
(
)
)
=>
println
!
(
"Age 25 is valid
\n
"
)
,
Err
(
e
)
=>
println
!
(
"Validation failed: {e}"
)
}
println
!
(
"=== Using fail! macro ===
\n
"
)
;
match
fetch_user
(
""
)
{
Ok
(
user
)
=>
println
!
(
"Found: {user}"
)
,
Err
(
e
)
=>
println
!
(
"Fetch failed: {e}"
)
}
match
fetch_user
(
"404"
)
{
Ok
(
user
)
=>
println
!
(
"Found: {user}"
)
,
Err
(
e
)
=>
println
!
(
"Fetch failed: {e}"
)
}
match
fetch_user
(
"alice"
)
{
Ok
(
user
)
=>
println
!
(
"Found: {user}
\n
"
)
,
Err
(
e
)
=>
println
!
(
"Fetch failed: {e}"
)
}
println
!
(
"=== Error Propagation ===
\n
"
)
;
match
process_request
(
"bob"
,
30
)
{
Ok
(
result
)
=>
println
!
(
"Success: {result}"
)
,
Err
(
e
)
=>
println
!
(
"Request failed: {e}"
)
}
match
process_request
(
"404"
,
30
)
{
Ok
(
result
)
=>
println
!
(
"Success: {result}"
)
,
Err
(
e
)
=>
println
!
(
"Request failed: {e}
\n
"
)
}
println
!
(
"=== Structured Metadata ===
\n
"
)
;
match
database_operation
(
)
{
Ok
(
(
)
)
=>
println
!
(
"Database operation succeeded"
)
,
Err
(
e
)
=>
{
println
!
(
"Database error: {e}"
)
;
println
!
(
"Metadata fields: {}"
,
e
.
metadata
(
)
.
len
(
)
)
;
for
(
key
,
value
)
in
e
.
metadata
(
)
.
iter
(
)
{
println
!
(
" {key}: {value:?}"
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL