Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Changes
Page history
Create rust/rust error
authored
Dec 31, 2020
by
umaumax
Show whitespace changes
Inline
Side-by-side
rust/rust-error.md
0 → 100644
View page @
667c4ca2
[[
_TOC_
]]
### thiserror
```
rs
use
std
::
fs
::
File
;
use
thiserror
::
Error
;
#[derive(Error,
Debug)]
pub
enum
MyAppError
{
#[error(
"Hoge error"
)]
Hoge
,
#[error(
"IO error"
)]
IO
(
#[source]
std
::
io
::
Error
),
}
fn
main
()
->
Result
<
(),
MyAppError
>
{
let
err
=
MyAppError
::
Hoge
;
print!
(
"err: {:?}
\n
"
,
err
);
let
file_result
=
File
::
create
(
"."
)
.map_err
(|
e
|
MyAppError
::
IO
(
e
));
print!
(
"file_result: {:?}
\n
"
,
file_result
);
Ok
(())
}
```
```
bash
err: Hoge
file_result: Err
(
IO
(
Os
{
code: 21, kind: Other, message:
"Is a directory"
}))
```
`source`
属性を付加すると既存のエラーをラップして独自のエラーの中に情報を追加できる
\ No newline at end of file