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.
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.
11 //! Traits for working with Errors.
13 //! # The `Error` trait
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:
21 //! use std::fmt::Display;
23 //! trait Error: Display {
24 //! fn description(&self) -> &str;
26 //! fn cause(&self) -> Option<&Error> { None }
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.
37 //! [`Result<T, E>`]: ../result/enum.Result.html
38 //! [`Display`]: ../fmt/trait.Display.html
39 //! [`cause`]: trait.Error.html#method.cause
41 #![stable(feature = "rust1", since = "1.0.0")]
43 // A note about crates and the facade:
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.
61 use fmt::{self, Debug, Display};
67 /// Base functionality for all errors in Rust.
68 #[stable(feature = "rust1", since = "1.0.0")]
69 pub trait Error: Debug + Display {
70 /// A short description of the error.
72 /// The description should only be used for a simple message.
73 /// It should not contain newlines or sentence-ending punctuation,
74 /// to facilitate embedding in larger user-facing strings.
75 /// For showing formatted error messages with more information see
78 /// [`Display`]: ../fmt/trait.Display.html
83 /// use std::error::Error;
85 /// match "xc".parse::<u32>() {
87 /// println!("Error: {}", e.description());
89 /// _ => println!("No error"),
92 #[stable(feature = "rust1", since = "1.0.0")]
93 fn description(&self) -> &str;
95 /// The lower-level cause of this error, if any.
100 /// use std::error::Error;
104 /// struct SuperError {
105 /// side: SuperErrorSideKick,
108 /// impl fmt::Display for SuperError {
109 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110 /// write!(f, "SuperError is here!")
114 /// impl Error for SuperError {
115 /// fn description(&self) -> &str {
116 /// "I'm the superhero of errors"
119 /// fn cause(&self) -> Option<&Error> {
125 /// struct SuperErrorSideKick;
127 /// impl fmt::Display for SuperErrorSideKick {
128 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
129 /// write!(f, "SuperErrorSideKick is here!")
133 /// impl Error for SuperErrorSideKick {
134 /// fn description(&self) -> &str {
135 /// "I'm SuperError side kick"
139 /// fn get_super_error() -> Result<(), SuperError> {
140 /// Err(SuperError { side: SuperErrorSideKick })
144 /// match get_super_error() {
146 /// println!("Error: {}", e.description());
147 /// println!("Caused by: {}", e.cause().unwrap());
149 /// _ => println!("No error"),
153 #[stable(feature = "rust1", since = "1.0.0")]
154 fn cause(&self) -> Option<&Error> { None }
156 /// Get the `TypeId` of `self`
158 #[unstable(feature = "error_type_id",
159 reason = "unclear whether to commit to this public implementation detail",
161 fn type_id(&self) -> TypeId where Self: 'static {
166 #[stable(feature = "rust1", since = "1.0.0")]
167 impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> {
168 fn from(err: E) -> Box<Error + 'a> {
173 #[stable(feature = "rust1", since = "1.0.0")]
174 impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<Error + Send + Sync + 'a> {
175 fn from(err: E) -> Box<Error + Send + Sync + 'a> {
180 #[stable(feature = "rust1", since = "1.0.0")]
181 impl From<String> for Box<Error + Send + Sync> {
182 fn from(err: String) -> Box<Error + Send + Sync> {
184 struct StringError(String);
186 impl Error for StringError {
187 fn description(&self) -> &str { &self.0 }
190 impl Display for StringError {
191 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
192 Display::fmt(&self.0, f)
196 Box::new(StringError(err))
200 #[stable(feature = "string_box_error", since = "1.6.0")]
201 impl From<String> for Box<Error> {
202 fn from(str_err: String) -> Box<Error> {
203 let err1: Box<Error + Send + Sync> = From::from(str_err);
204 let err2: Box<Error> = err1;
209 #[stable(feature = "rust1", since = "1.0.0")]
210 impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a> {
211 fn from(err: &'b str) -> Box<Error + Send + Sync + 'a> {
212 From::from(String::from(err))
216 #[stable(feature = "string_box_error", since = "1.6.0")]
217 impl<'a> From<&'a str> for Box<Error> {
218 fn from(err: &'a str) -> Box<Error> {
219 From::from(String::from(err))
223 #[stable(feature = "cow_box_error", since = "1.22.0")]
224 impl<'a, 'b> From<Cow<'b, str>> for Box<Error + Send + Sync + 'a> {
225 fn from(err: Cow<'b, str>) -> Box<Error + Send + Sync + 'a> {
226 From::from(String::from(err))
230 #[stable(feature = "cow_box_error", since = "1.22.0")]
231 impl<'a> From<Cow<'a, str>> for Box<Error> {
232 fn from(err: Cow<'a, str>) -> Box<Error> {
233 From::from(String::from(err))
237 #[unstable(feature = "never_type_impls", issue = "35121")]
239 fn description(&self) -> &str { *self }
242 #[unstable(feature = "allocator_api",
243 reason = "the precise API and guarantees it provides may be tweaked.",
245 impl Error for allocator::AllocErr {
246 fn description(&self) -> &str {
247 allocator::AllocErr::description(self)
251 #[unstable(feature = "allocator_api",
252 reason = "the precise API and guarantees it provides may be tweaked.",
254 impl Error for allocator::CannotReallocInPlace {
255 fn description(&self) -> &str {
256 allocator::CannotReallocInPlace::description(self)
260 #[stable(feature = "rust1", since = "1.0.0")]
261 impl Error for str::ParseBoolError {
262 fn description(&self) -> &str { "failed to parse bool" }
265 #[stable(feature = "rust1", since = "1.0.0")]
266 impl Error for str::Utf8Error {
267 fn description(&self) -> &str {
268 "invalid utf-8: corrupt contents"
272 #[stable(feature = "rust1", since = "1.0.0")]
273 impl Error for num::ParseIntError {
274 fn description(&self) -> &str {
279 #[unstable(feature = "try_from", issue = "33417")]
280 impl Error for num::TryFromIntError {
281 fn description(&self) -> &str {
286 #[unstable(feature = "try_from", issue = "33417")]
287 impl Error for array::TryFromSliceError {
288 fn description(&self) -> &str {
293 #[stable(feature = "rust1", since = "1.0.0")]
294 impl Error for num::ParseFloatError {
295 fn description(&self) -> &str {
300 #[stable(feature = "rust1", since = "1.0.0")]
301 impl Error for string::FromUtf8Error {
302 fn description(&self) -> &str {
307 #[stable(feature = "rust1", since = "1.0.0")]
308 impl Error for string::FromUtf16Error {
309 fn description(&self) -> &str {
314 #[stable(feature = "str_parse_error2", since = "1.8.0")]
315 impl Error for string::ParseError {
316 fn description(&self) -> &str {
321 #[stable(feature = "decode_utf16", since = "1.9.0")]
322 impl Error for char::DecodeUtf16Error {
323 fn description(&self) -> &str {
324 "unpaired surrogate found"
328 #[stable(feature = "box_error", since = "1.8.0")]
329 impl<T: Error> Error for Box<T> {
330 fn description(&self) -> &str {
331 Error::description(&**self)
334 fn cause(&self) -> Option<&Error> {
335 Error::cause(&**self)
339 #[stable(feature = "fmt_error", since = "1.11.0")]
340 impl Error for fmt::Error {
341 fn description(&self) -> &str {
342 "an error occurred when formatting an argument"
346 #[stable(feature = "try_borrow", since = "1.13.0")]
347 impl Error for cell::BorrowError {
348 fn description(&self) -> &str {
349 "already mutably borrowed"
353 #[stable(feature = "try_borrow", since = "1.13.0")]
354 impl Error for cell::BorrowMutError {
355 fn description(&self) -> &str {
360 #[unstable(feature = "try_from", issue = "33417")]
361 impl Error for char::CharTryFromError {
362 fn description(&self) -> &str {
363 "converted integer out of range for `char`"
367 #[stable(feature = "char_from_str", since = "1.20.0")]
368 impl Error for char::ParseCharError {
369 fn description(&self) -> &str {
374 #[unstable(feature = "try_from", issue = "33417")]
375 impl Error for convert::Infallible {
376 fn description(&self) -> &str {
382 // copied from any.rs
383 impl Error + 'static {
384 /// Returns true if the boxed type is the same as `T`
385 #[stable(feature = "error_downcast", since = "1.3.0")]
387 pub fn is<T: Error + 'static>(&self) -> bool {
388 // Get TypeId of the type this function is instantiated with
389 let t = TypeId::of::<T>();
391 // Get TypeId of the type in the trait object
392 let boxed = self.type_id();
394 // Compare both TypeIds on equality
398 /// Returns some reference to the boxed value if it is of type `T`, or
399 /// `None` if it isn't.
400 #[stable(feature = "error_downcast", since = "1.3.0")]
402 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
405 Some(&*(self as *const Error as *const T))
412 /// Returns some mutable reference to the boxed value if it is of type `T`, or
413 /// `None` if it isn't.
414 #[stable(feature = "error_downcast", since = "1.3.0")]
416 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
419 Some(&mut *(self as *mut Error as *mut T))
427 impl Error + 'static + Send {
428 /// Forwards to the method defined on the type `Any`.
429 #[stable(feature = "error_downcast", since = "1.3.0")]
431 pub fn is<T: Error + 'static>(&self) -> bool {
432 <Error + 'static>::is::<T>(self)
435 /// Forwards to the method defined on the type `Any`.
436 #[stable(feature = "error_downcast", since = "1.3.0")]
438 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
439 <Error + 'static>::downcast_ref::<T>(self)
442 /// Forwards to the method defined on the type `Any`.
443 #[stable(feature = "error_downcast", since = "1.3.0")]
445 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
446 <Error + 'static>::downcast_mut::<T>(self)
450 impl Error + 'static + Send + Sync {
451 /// Forwards to the method defined on the type `Any`.
452 #[stable(feature = "error_downcast", since = "1.3.0")]
454 pub fn is<T: Error + 'static>(&self) -> bool {
455 <Error + 'static>::is::<T>(self)
458 /// Forwards to the method defined on the type `Any`.
459 #[stable(feature = "error_downcast", since = "1.3.0")]
461 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
462 <Error + 'static>::downcast_ref::<T>(self)
465 /// Forwards to the method defined on the type `Any`.
466 #[stable(feature = "error_downcast", since = "1.3.0")]
468 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
469 <Error + 'static>::downcast_mut::<T>(self)
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>) -> Result<Box<T>, Box<Error>> {
480 let raw: *mut Error = Box::into_raw(self);
481 Ok(Box::from_raw(raw as *mut T))
491 #[stable(feature = "error_downcast", since = "1.3.0")]
492 /// Attempt to downcast the box to a concrete type.
493 pub fn downcast<T: Error + 'static>(self: Box<Self>)
494 -> Result<Box<T>, Box<Error + Send>> {
495 let err: Box<Error> = self;
496 <Error>::downcast(err).map_err(|s| unsafe {
497 // reapply the Send marker
498 transmute::<Box<Error>, Box<Error + Send>>(s)
503 impl Error + Send + Sync {
505 #[stable(feature = "error_downcast", since = "1.3.0")]
506 /// Attempt to downcast the box to a concrete type.
507 pub fn downcast<T: Error + 'static>(self: Box<Self>)
508 -> Result<Box<T>, Box<Self>> {
509 let err: Box<Error> = self;
510 <Error>::downcast(err).map_err(|s| unsafe {
511 // reapply the Send+Sync marker
512 transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
522 #[derive(Debug, PartialEq)]
524 #[derive(Debug, PartialEq)]
527 impl fmt::Display for A {
528 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
532 impl fmt::Display for B {
533 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
539 fn description(&self) -> &str { "A-desc" }
542 fn description(&self) -> &str { "A-desc" }
548 let a = &mut a as &mut (Error + 'static);
549 assert_eq!(a.downcast_ref::<A>(), Some(&A));
550 assert_eq!(a.downcast_ref::<B>(), None);
551 assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
552 assert_eq!(a.downcast_mut::<B>(), None);
554 let a: Box<Error> = Box::new(A);
555 match a.downcast::<B>() {
556 Ok(..) => panic!("expected error"),
557 Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),