1
1
use anyhow:: Result ;
2
+ use std:: process:: ExitCode ;
2
3
use thiserror:: Error ;
3
4
4
5
/// The ErrorWithExitCode is the error type used at Scarb's CLI-layer.
5
6
#[ derive( Error , Debug ) ]
6
- #[ error( "ErrorWithExitCode exit_code: {exit_code}" ) ]
7
+ #[ error( "ErrorWithExitCode exit_code: {:?}" , exit_code ) ]
7
8
pub struct ErrorWithExitCode {
8
9
/// The error to display. This can be `None` in rare cases to exit with a
9
10
/// code without displaying a message.
10
11
#[ source]
11
12
pub source : Option < anyhow:: Error > ,
12
13
/// The process exit code.
13
- pub exit_code : i32 ,
14
+ pub exit_code : ExitCode ,
14
15
}
15
16
16
17
impl ErrorWithExitCode {
17
- pub fn new ( error : anyhow:: Error , code : i32 ) -> Self {
18
+ pub fn new ( error : anyhow:: Error , code : ExitCode ) -> Self {
18
19
Self {
19
20
source : Some ( error) ,
20
21
exit_code : code,
21
22
}
22
23
}
23
24
24
- pub fn code ( code : i32 ) -> Self {
25
+ pub fn code ( code : ExitCode ) -> Self {
25
26
Self {
26
27
source : None ,
27
28
exit_code : code,
@@ -31,16 +32,16 @@ impl ErrorWithExitCode {
31
32
32
33
impl From < anyhow:: Error > for ErrorWithExitCode {
33
34
fn from ( err : anyhow:: Error ) -> ErrorWithExitCode {
34
- ErrorWithExitCode :: new ( err, 1 )
35
+ ErrorWithExitCode :: new ( err, ExitCode :: FAILURE )
35
36
}
36
37
}
37
38
38
39
impl From < std:: io:: Error > for ErrorWithExitCode {
39
40
fn from ( err : std:: io:: Error ) -> ErrorWithExitCode {
40
- ErrorWithExitCode :: new ( err. into ( ) , 1 )
41
+ ErrorWithExitCode :: new ( err. into ( ) , ExitCode :: FAILURE )
41
42
}
42
43
}
43
44
44
- pub fn error_with_exit_code < T > ( code : i32 ) -> Result < T > {
45
+ pub fn error_with_exit_code < T > ( code : ExitCode ) -> Result < T > {
45
46
Err ( ErrorWithExitCode :: code ( code) . into ( ) )
46
47
}
0 commit comments