@@ -7,30 +7,45 @@ use std::sync::Arc;
7
7
use figment:: Profile ;
8
8
9
9
use crate :: listener:: Endpoint ;
10
- use crate :: { Ignite , Orbit , Phase , Rocket } ;
10
+ use crate :: { Catcher , Ignite , Orbit , Phase , Rocket , Route } ;
11
11
use crate :: trace:: Trace ;
12
12
13
- /// An error that occurs during launch.
13
+ /// An error that occurred during launch or ignition .
14
14
///
15
- /// An `Error` is returned by [`launch()`](Rocket::launch()) when launching an
16
- /// application fails or, more rarely, when the runtime fails after launching.
15
+ /// An `Error` is returned by [`Rocket::launch()`] or [`Rocket::ignite()`] on
16
+ /// failure to launch or ignite, respectively. An `Error` may occur when the
17
+ /// configuration is invalid, when a route or catcher collision is detected, or
18
+ /// when a fairing fails to launch. An `Error` may also occur when the Rocket
19
+ /// instance fails to liftoff or when the Rocket instance fails to shutdown.
20
+ /// Finally, an `Error` may occur when a sentinel requests an abort.
17
21
///
18
- /// # Usage
22
+ /// To determine the kind of error that occurred, use [`Error::kind()`].
19
23
///
20
- /// An `Error` value should usually be allowed to `drop` without inspection.
21
- /// There are at least two exceptions:
24
+ /// # Example
22
25
///
23
- /// 1. If you are writing a library or high-level application on-top of
24
- /// Rocket, you likely want to inspect the value before it drops to avoid a
25
- /// Rocket-specific `panic!`. This typically means simply printing the
26
- /// value.
26
+ /// ```rust
27
+ /// # use rocket::*;
28
+ /// use rocket::trace::Trace;
29
+ /// use rocket::error::ErrorKind;
27
30
///
28
- /// 2. You want to display your own error messages.
31
+ /// # async fn run() -> Result<(), rocket::error::Error> {
32
+ /// if let Err(e) = rocket::build().ignite().await {
33
+ /// match e.kind() {
34
+ /// ErrorKind::Bind(_, e) => info!("binding failed: {}", e),
35
+ /// ErrorKind::Io(e) => info!("I/O error: {}", e),
36
+ /// _ => e.trace_error(),
37
+ /// }
38
+ ///
39
+ /// return Err(e);
40
+ /// }
41
+ /// # Ok(())
42
+ /// # }
43
+ /// ```
29
44
pub struct Error {
30
45
pub ( crate ) kind : ErrorKind
31
46
}
32
47
33
- /// The kind error that occurred.
48
+ /// The error kind that occurred. Returned by [`Error::kind()`] .
34
49
///
35
50
/// In almost every instance, a launch error occurs because of an I/O error;
36
51
/// this is represented by the `Io` variant. A launch error may also occur
@@ -39,17 +54,22 @@ pub struct Error {
39
54
/// `FailedFairing` variants, respectively.
40
55
#[ derive( Debug ) ]
41
56
#[ non_exhaustive]
42
- // FIXME: Don't expose this. Expose access methods from `Error` instead.
43
57
pub enum ErrorKind {
44
- /// Binding to the network interface at `.0` failed with error `.1`.
58
+ /// Binding to the network interface at `.0` (if known) failed with `.1`.
45
59
Bind ( Option < Endpoint > , Box < dyn StdError + Send > ) ,
46
60
/// An I/O error occurred during launch.
47
61
Io ( io:: Error ) ,
48
62
/// A valid [`Config`](crate::Config) could not be extracted from the
49
63
/// configured figment.
50
64
Config ( figment:: Error ) ,
51
- /// Route collisions were detected.
52
- Collisions ( crate :: router:: Collisions ) ,
65
+ /// Route or catcher collisions were detected. At least one of `routes` or
66
+ /// `catchers` is guaranteed to be non-empty.
67
+ Collisions {
68
+ /// Pairs of colliding routes, if any.
69
+ routes : Vec < ( Route , Route ) > ,
70
+ /// Pairs of colliding catchers, if any.
71
+ catchers : Vec < ( Catcher , Catcher ) > ,
72
+ } ,
53
73
/// Launch fairing(s) failed.
54
74
FailedFairings ( Vec < crate :: fairing:: Info > ) ,
55
75
/// Sentinels requested abort.
@@ -75,11 +95,48 @@ impl Error {
75
95
Error { kind }
76
96
}
77
97
78
- // FIXME: Don't expose this. Expose finer access methods instead.
98
+ /// Returns the kind of error that occurred.
99
+ ///
100
+ /// # Example
101
+ ///
102
+ /// ```rust
103
+ /// # use rocket::*;
104
+ /// use rocket::trace::Trace;
105
+ /// use rocket::error::ErrorKind;
106
+ ///
107
+ /// # async fn run() -> Result<(), rocket::error::Error> {
108
+ /// if let Err(e) = rocket::build().ignite().await {
109
+ /// match e.kind() {
110
+ /// ErrorKind::Bind(_, e) => info!("binding failed: {}", e),
111
+ /// ErrorKind::Io(e) => info!("I/O error: {}", e),
112
+ /// _ => e.trace_error(),
113
+ /// }
114
+ /// }
115
+ /// # Ok(())
116
+ /// # }
117
+ /// ```
79
118
pub fn kind ( & self ) -> & ErrorKind {
80
119
& self . kind
81
120
}
82
121
122
+ /// Given the return value of [`Rocket::launch()`] or [`Rocket::ignite()`],
123
+ /// which return a `Result<Rocket<P>, Error>`, logs the error, if any, and
124
+ /// returns the appropriate exit code.
125
+ ///
126
+ /// For `Ok(_)`, returns `ExitCode::SUCCESS`. For `Err(e)`, logs the error
127
+ /// and returns `ExitCode::FAILURE`.
128
+ ///
129
+ /// # Example
130
+ ///
131
+ /// ```rust
132
+ /// # use rocket::*;
133
+ /// use std::process::ExitCode;
134
+ /// use rocket::error::Error;
135
+ ///
136
+ /// async fn run() -> ExitCode {
137
+ /// Error::report(rocket::build().launch().await)
138
+ /// }
139
+ /// ```
83
140
pub fn report < P : Phase > ( result : Result < Rocket < P > , Error > ) -> process:: ExitCode {
84
141
match result {
85
142
Ok ( _) => process:: ExitCode :: SUCCESS ,
@@ -114,7 +171,7 @@ impl StdError for Error {
114
171
match & self . kind {
115
172
ErrorKind :: Bind ( _, e) => Some ( & * * e) ,
116
173
ErrorKind :: Io ( e) => Some ( e) ,
117
- ErrorKind :: Collisions ( _ ) => None ,
174
+ ErrorKind :: Collisions { .. } => None ,
118
175
ErrorKind :: FailedFairings ( _) => None ,
119
176
ErrorKind :: InsecureSecretKey ( _) => None ,
120
177
ErrorKind :: Config ( e) => Some ( e) ,
@@ -131,7 +188,7 @@ impl fmt::Display for ErrorKind {
131
188
match self {
132
189
ErrorKind :: Bind ( _, e) => write ! ( f, "binding failed: {e}" ) ,
133
190
ErrorKind :: Io ( e) => write ! ( f, "I/O error: {e}" ) ,
134
- ErrorKind :: Collisions ( _ ) => "collisions detected" . fmt ( f) ,
191
+ ErrorKind :: Collisions { .. } => "collisions detected" . fmt ( f) ,
135
192
ErrorKind :: FailedFairings ( _) => "launch fairing(s) failed" . fmt ( f) ,
136
193
ErrorKind :: InsecureSecretKey ( _) => "insecure secret key config" . fmt ( f) ,
137
194
ErrorKind :: Config ( _) => "failed to extract configuration" . fmt ( f) ,
0 commit comments