]> git.lizzy.rs Git - rust.git/blob - src/libstd/error.rs
Auto merge of #44350 - GuillaumeGomez:id-false-positive, r=QuietMisdreavus
[rust.git] / src / libstd / error.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Traits for working with Errors.
12 //!
13 //! # The `Error` trait
14 //!
15 //! `Error` is a trait representing the basic expectations for error values,
16 //! i.e. values of type `E` in [`Result<T, E>`]. At a minimum, errors must provide
17 //! a description, but they may optionally provide additional detail (via
18 //! [`Display`]) and cause chain information:
19 //!
20 //! ```
21 //! use std::fmt::Display;
22 //!
23 //! trait Error: Display {
24 //!     fn description(&self) -> &str;
25 //!
26 //!     fn cause(&self) -> Option<&Error> { None }
27 //! }
28 //! ```
29 //!
30 //! The [`cause`] method is generally used when errors cross "abstraction
31 //! boundaries", i.e.  when a one module must report an error that is "caused"
32 //! by an error from a lower-level module. This setup makes it possible for the
33 //! high-level module to provide its own errors that do not commit to any
34 //! particular implementation, but also reveal some of its implementation for
35 //! debugging via [`cause`] chains.
36 //!
37 //! [`Result<T, E>`]: ../result/enum.Result.html
38 //! [`Display`]: ../fmt/trait.Display.html
39 //! [`cause`]: trait.Error.html#method.cause
40
41 #![stable(feature = "rust1", since = "1.0.0")]
42
43 // A note about crates and the facade:
44 //
45 // Originally, the `Error` trait was defined in libcore, and the impls
46 // were scattered about. However, coherence objected to this
47 // arrangement, because to create the blanket impls for `Box` required
48 // knowing that `&str: !Error`, and we have no means to deal with that
49 // sort of conflict just now. Therefore, for the time being, we have
50 // moved the `Error` trait into libstd. As we evolve a sol'n to the
51 // coherence challenge (e.g., specialization, neg impls, etc) we can
52 // reconsider what crate these items belong in.
53
54 use alloc::allocator;
55 use any::TypeId;
56 use borrow::Cow;
57 use cell;
58 use char;
59 use fmt::{self, Debug, Display};
60 use mem::transmute;
61 use num;
62 use str;
63 use string;
64
65 /// Base functionality for all errors in Rust.
66 #[stable(feature = "rust1", since = "1.0.0")]
67 pub trait Error: Debug + Display {
68     /// A short description of the error.
69     ///
70     /// The description should only be used for a simple message.
71     /// It should not contain newlines or sentence-ending punctuation,
72     /// to facilitate embedding in larger user-facing strings.
73     /// For showing formatted error messages with more information see
74     /// [`Display`].
75     ///
76     /// [`Display`]: ../fmt/trait.Display.html
77     ///
78     /// # Examples
79     ///
80     /// ```
81     /// use std::error::Error;
82     ///
83     /// match "xc".parse::<u32>() {
84     ///     Err(e) => {
85     ///         println!("Error: {}", e.description());
86     ///     }
87     ///     _ => println!("No error"),
88     /// }
89     /// ```
90     #[stable(feature = "rust1", since = "1.0.0")]
91     fn description(&self) -> &str;
92
93     /// The lower-level cause of this error, if any.
94     ///
95     /// # Examples
96     ///
97     /// ```
98     /// use std::error::Error;
99     /// use std::fmt;
100     ///
101     /// #[derive(Debug)]
102     /// struct SuperError {
103     ///     side: SuperErrorSideKick,
104     /// }
105     ///
106     /// impl fmt::Display for SuperError {
107     ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108     ///         write!(f, "SuperError is here!")
109     ///     }
110     /// }
111     ///
112     /// impl Error for SuperError {
113     ///     fn description(&self) -> &str {
114     ///         "I'm the superhero of errors"
115     ///     }
116     ///
117     ///     fn cause(&self) -> Option<&Error> {
118     ///         Some(&self.side)
119     ///     }
120     /// }
121     ///
122     /// #[derive(Debug)]
123     /// struct SuperErrorSideKick;
124     ///
125     /// impl fmt::Display for SuperErrorSideKick {
126     ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127     ///         write!(f, "SuperErrorSideKick is here!")
128     ///     }
129     /// }
130     ///
131     /// impl Error for SuperErrorSideKick {
132     ///     fn description(&self) -> &str {
133     ///         "I'm SuperError side kick"
134     ///     }
135     /// }
136     ///
137     /// fn get_super_error() -> Result<(), SuperError> {
138     ///     Err(SuperError { side: SuperErrorSideKick })
139     /// }
140     ///
141     /// fn main() {
142     ///     match get_super_error() {
143     ///         Err(e) => {
144     ///             println!("Error: {}", e.description());
145     ///             println!("Caused by: {}", e.cause().unwrap());
146     ///         }
147     ///         _ => println!("No error"),
148     ///     }
149     /// }
150     /// ```
151     #[stable(feature = "rust1", since = "1.0.0")]
152     fn cause(&self) -> Option<&Error> { None }
153
154     /// Get the `TypeId` of `self`
155     #[doc(hidden)]
156     #[unstable(feature = "error_type_id",
157                reason = "unclear whether to commit to this public implementation detail",
158                issue = "27745")]
159     fn type_id(&self) -> TypeId where Self: 'static {
160         TypeId::of::<Self>()
161     }
162 }
163
164 #[stable(feature = "rust1", since = "1.0.0")]
165 impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> {
166     fn from(err: E) -> Box<Error + 'a> {
167         Box::new(err)
168     }
169 }
170
171 #[stable(feature = "rust1", since = "1.0.0")]
172 impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<Error + Send + Sync + 'a> {
173     fn from(err: E) -> Box<Error + Send + Sync + 'a> {
174         Box::new(err)
175     }
176 }
177
178 #[stable(feature = "rust1", since = "1.0.0")]
179 impl From<String> for Box<Error + Send + Sync> {
180     fn from(err: String) -> Box<Error + Send + Sync> {
181         #[derive(Debug)]
182         struct StringError(String);
183
184         impl Error for StringError {
185             fn description(&self) -> &str { &self.0 }
186         }
187
188         impl Display for StringError {
189             fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190                 Display::fmt(&self.0, f)
191             }
192         }
193
194         Box::new(StringError(err))
195     }
196 }
197
198 #[stable(feature = "string_box_error", since = "1.6.0")]
199 impl From<String> for Box<Error> {
200     fn from(str_err: String) -> Box<Error> {
201         let err1: Box<Error + Send + Sync> = From::from(str_err);
202         let err2: Box<Error> = err1;
203         err2
204     }
205 }
206
207 #[stable(feature = "rust1", since = "1.0.0")]
208 impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a> {
209     fn from(err: &'b str) -> Box<Error + Send + Sync + 'a> {
210         From::from(String::from(err))
211     }
212 }
213
214 #[stable(feature = "string_box_error", since = "1.6.0")]
215 impl<'a> From<&'a str> for Box<Error> {
216     fn from(err: &'a str) -> Box<Error> {
217         From::from(String::from(err))
218     }
219 }
220
221 #[stable(feature = "cow_box_error", since = "1.22.0")]
222 impl<'a, 'b> From<Cow<'b, str>> for Box<Error + Send + Sync + 'a> {
223     fn from(err: Cow<'b, str>) -> Box<Error + Send + Sync + 'a> {
224         From::from(String::from(err))
225     }
226 }
227
228 #[stable(feature = "cow_box_error", since = "1.22.0")]
229 impl<'a> From<Cow<'a, str>> for Box<Error> {
230     fn from(err: Cow<'a, str>) -> Box<Error> {
231         From::from(String::from(err))
232     }
233 }
234
235 #[unstable(feature = "never_type_impls", issue = "35121")]
236 impl Error for ! {
237     fn description(&self) -> &str { *self }
238 }
239
240 #[unstable(feature = "allocator_api",
241            reason = "the precise API and guarantees it provides may be tweaked.",
242            issue = "32838")]
243 impl Error for allocator::AllocErr {
244     fn description(&self) -> &str {
245         allocator::AllocErr::description(self)
246     }
247 }
248
249 #[unstable(feature = "allocator_api",
250            reason = "the precise API and guarantees it provides may be tweaked.",
251            issue = "32838")]
252 impl Error for allocator::CannotReallocInPlace {
253     fn description(&self) -> &str {
254         allocator::CannotReallocInPlace::description(self)
255     }
256 }
257
258 #[stable(feature = "rust1", since = "1.0.0")]
259 impl Error for str::ParseBoolError {
260     fn description(&self) -> &str { "failed to parse bool" }
261 }
262
263 #[stable(feature = "rust1", since = "1.0.0")]
264 impl Error for str::Utf8Error {
265     fn description(&self) -> &str {
266         "invalid utf-8: corrupt contents"
267     }
268 }
269
270 #[stable(feature = "rust1", since = "1.0.0")]
271 impl Error for num::ParseIntError {
272     fn description(&self) -> &str {
273         self.__description()
274     }
275 }
276
277 #[unstable(feature = "try_from", issue = "33417")]
278 impl Error for num::TryFromIntError {
279     fn description(&self) -> &str {
280         self.__description()
281     }
282 }
283
284 #[stable(feature = "rust1", since = "1.0.0")]
285 impl Error for num::ParseFloatError {
286     fn description(&self) -> &str {
287         self.__description()
288     }
289 }
290
291 #[stable(feature = "rust1", since = "1.0.0")]
292 impl Error for string::FromUtf8Error {
293     fn description(&self) -> &str {
294         "invalid utf-8"
295     }
296 }
297
298 #[stable(feature = "rust1", since = "1.0.0")]
299 impl Error for string::FromUtf16Error {
300     fn description(&self) -> &str {
301         "invalid utf-16"
302     }
303 }
304
305 #[stable(feature = "str_parse_error2", since = "1.8.0")]
306 impl Error for string::ParseError {
307     fn description(&self) -> &str {
308         match *self {}
309     }
310 }
311
312 #[stable(feature = "decode_utf16", since = "1.9.0")]
313 impl Error for char::DecodeUtf16Error {
314     fn description(&self) -> &str {
315         "unpaired surrogate found"
316     }
317 }
318
319 #[stable(feature = "box_error", since = "1.8.0")]
320 impl<T: Error> Error for Box<T> {
321     fn description(&self) -> &str {
322         Error::description(&**self)
323     }
324
325     fn cause(&self) -> Option<&Error> {
326         Error::cause(&**self)
327     }
328 }
329
330 #[stable(feature = "fmt_error", since = "1.11.0")]
331 impl Error for fmt::Error {
332     fn description(&self) -> &str {
333         "an error occurred when formatting an argument"
334     }
335 }
336
337 #[stable(feature = "try_borrow", since = "1.13.0")]
338 impl Error for cell::BorrowError {
339     fn description(&self) -> &str {
340         "already mutably borrowed"
341     }
342 }
343
344 #[stable(feature = "try_borrow", since = "1.13.0")]
345 impl Error for cell::BorrowMutError {
346     fn description(&self) -> &str {
347         "already borrowed"
348     }
349 }
350
351 #[unstable(feature = "try_from", issue = "33417")]
352 impl Error for char::CharTryFromError {
353     fn description(&self) -> &str {
354         "converted integer out of range for `char`"
355     }
356 }
357
358 #[stable(feature = "char_from_str", since = "1.20.0")]
359 impl Error for char::ParseCharError {
360     fn description(&self) -> &str {
361         self.__description()
362     }
363 }
364
365
366 // copied from any.rs
367 impl Error + 'static {
368     /// Returns true if the boxed type is the same as `T`
369     #[stable(feature = "error_downcast", since = "1.3.0")]
370     #[inline]
371     pub fn is<T: Error + 'static>(&self) -> bool {
372         // Get TypeId of the type this function is instantiated with
373         let t = TypeId::of::<T>();
374
375         // Get TypeId of the type in the trait object
376         let boxed = self.type_id();
377
378         // Compare both TypeIds on equality
379         t == boxed
380     }
381
382     /// Returns some reference to the boxed value if it is of type `T`, or
383     /// `None` if it isn't.
384     #[stable(feature = "error_downcast", since = "1.3.0")]
385     #[inline]
386     pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
387         if self.is::<T>() {
388             unsafe {
389                 Some(&*(self as *const Error as *const T))
390             }
391         } else {
392             None
393         }
394     }
395
396     /// Returns some mutable reference to the boxed value if it is of type `T`, or
397     /// `None` if it isn't.
398     #[stable(feature = "error_downcast", since = "1.3.0")]
399     #[inline]
400     pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
401         if self.is::<T>() {
402             unsafe {
403                 Some(&mut *(self as *mut Error as *mut T))
404             }
405         } else {
406             None
407         }
408     }
409 }
410
411 impl Error + 'static + Send {
412     /// Forwards to the method defined on the type `Any`.
413     #[stable(feature = "error_downcast", since = "1.3.0")]
414     #[inline]
415     pub fn is<T: Error + 'static>(&self) -> bool {
416         <Error + 'static>::is::<T>(self)
417     }
418
419     /// Forwards to the method defined on the type `Any`.
420     #[stable(feature = "error_downcast", since = "1.3.0")]
421     #[inline]
422     pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
423         <Error + 'static>::downcast_ref::<T>(self)
424     }
425
426     /// Forwards to the method defined on the type `Any`.
427     #[stable(feature = "error_downcast", since = "1.3.0")]
428     #[inline]
429     pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
430         <Error + 'static>::downcast_mut::<T>(self)
431     }
432 }
433
434 impl Error + 'static + Send + Sync {
435     /// Forwards to the method defined on the type `Any`.
436     #[stable(feature = "error_downcast", since = "1.3.0")]
437     #[inline]
438     pub fn is<T: Error + 'static>(&self) -> bool {
439         <Error + 'static>::is::<T>(self)
440     }
441
442     /// Forwards to the method defined on the type `Any`.
443     #[stable(feature = "error_downcast", since = "1.3.0")]
444     #[inline]
445     pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
446         <Error + 'static>::downcast_ref::<T>(self)
447     }
448
449     /// Forwards to the method defined on the type `Any`.
450     #[stable(feature = "error_downcast", since = "1.3.0")]
451     #[inline]
452     pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
453         <Error + 'static>::downcast_mut::<T>(self)
454     }
455 }
456
457 impl Error {
458     #[inline]
459     #[stable(feature = "error_downcast", since = "1.3.0")]
460     /// Attempt to downcast the box to a concrete type.
461     pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
462         if self.is::<T>() {
463             unsafe {
464                 let raw: *mut Error = Box::into_raw(self);
465                 Ok(Box::from_raw(raw as *mut T))
466             }
467         } else {
468             Err(self)
469         }
470     }
471 }
472
473 impl Error + Send {
474     #[inline]
475     #[stable(feature = "error_downcast", since = "1.3.0")]
476     /// Attempt to downcast the box to a concrete type.
477     pub fn downcast<T: Error + 'static>(self: Box<Self>)
478                                         -> Result<Box<T>, Box<Error + Send>> {
479         let err: Box<Error> = self;
480         <Error>::downcast(err).map_err(|s| unsafe {
481             // reapply the Send marker
482             transmute::<Box<Error>, Box<Error + Send>>(s)
483         })
484     }
485 }
486
487 impl Error + Send + Sync {
488     #[inline]
489     #[stable(feature = "error_downcast", since = "1.3.0")]
490     /// Attempt to downcast the box to a concrete type.
491     pub fn downcast<T: Error + 'static>(self: Box<Self>)
492                                         -> Result<Box<T>, Box<Self>> {
493         let err: Box<Error> = self;
494         <Error>::downcast(err).map_err(|s| unsafe {
495             // reapply the Send+Sync marker
496             transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
497         })
498     }
499 }
500
501 #[cfg(test)]
502 mod tests {
503     use super::Error;
504     use fmt;
505
506     #[derive(Debug, PartialEq)]
507     struct A;
508     #[derive(Debug, PartialEq)]
509     struct B;
510
511     impl fmt::Display for A {
512         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
513             write!(f, "A")
514         }
515     }
516     impl fmt::Display for B {
517         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
518             write!(f, "B")
519         }
520     }
521
522     impl Error for A {
523         fn description(&self) -> &str { "A-desc" }
524     }
525     impl Error for B {
526         fn description(&self) -> &str { "A-desc" }
527     }
528
529     #[test]
530     fn downcasting() {
531         let mut a = A;
532         let a = &mut a as &mut (Error + 'static);
533         assert_eq!(a.downcast_ref::<A>(), Some(&A));
534         assert_eq!(a.downcast_ref::<B>(), None);
535         assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
536         assert_eq!(a.downcast_mut::<B>(), None);
537
538         let a: Box<Error> = Box::new(A);
539         match a.downcast::<B>() {
540             Ok(..) => panic!("expected error"),
541             Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),
542         }
543     }
544 }