1 //! Traits for working with Errors.
3 #![stable(feature = "rust1", since = "1.0.0")]
5 // A note about crates and the facade:
7 // Originally, the `Error` trait was defined in libcore, and the impls
8 // were scattered about. However, coherence objected to this
9 // arrangement, because to create the blanket impls for `Box` required
10 // knowing that `&str: !Error`, and we have no means to deal with that
11 // sort of conflict just now. Therefore, for the time being, we have
12 // moved the `Error` trait into libstd. As we evolve a sol'n to the
13 // coherence challenge (e.g., specialization, neg impls, etc) we can
14 // reconsider what crate these items belong in.
17 use core::convert::Infallible;
19 use crate::alloc::{AllocErr, LayoutErr};
20 use crate::any::TypeId;
21 use crate::backtrace::Backtrace;
22 use crate::borrow::Cow;
25 use crate::fmt::{self, Debug, Display};
26 use crate::mem::transmute;
31 /// `Error` is a trait representing the basic expectations for error values,
32 /// i.e., values of type `E` in [`Result<T, E>`]. Errors must describe
33 /// themselves through the [`Display`] and [`Debug`] traits, and may provide
34 /// cause chain information:
36 /// The [`source`] method is generally used when errors cross "abstraction
37 /// boundaries". If one module must report an error that is caused by an error
38 /// from a lower-level module, it can allow access to that error via the
39 /// [`source`] method. This makes it possible for the high-level module to
40 /// provide its own errors while also revealing some of the implementation for
41 /// debugging via [`source`] chains.
43 /// [`Result<T, E>`]: ../result/enum.Result.html
44 /// [`Display`]: ../fmt/trait.Display.html
45 /// [`Debug`]: ../fmt/trait.Debug.html
46 /// [`source`]: trait.Error.html#method.source
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub trait Error: Debug + Display {
49 /// The lower-level source of this error, if any.
54 /// use std::error::Error;
58 /// struct SuperError {
59 /// side: SuperErrorSideKick,
62 /// impl fmt::Display for SuperError {
63 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 /// write!(f, "SuperError is here!")
68 /// impl Error for SuperError {
69 /// fn source(&self) -> Option<&(dyn Error + 'static)> {
75 /// struct SuperErrorSideKick;
77 /// impl fmt::Display for SuperErrorSideKick {
78 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 /// write!(f, "SuperErrorSideKick is here!")
83 /// impl Error for SuperErrorSideKick {}
85 /// fn get_super_error() -> Result<(), SuperError> {
86 /// Err(SuperError { side: SuperErrorSideKick })
90 /// match get_super_error() {
92 /// println!("Error: {}", e);
93 /// println!("Caused by: {}", e.source().unwrap());
95 /// _ => println!("No error"),
99 #[stable(feature = "error_source", since = "1.30.0")]
100 fn source(&self) -> Option<&(dyn Error + 'static)> {
104 /// Gets the `TypeId` of `self`.
107 feature = "error_type_id",
108 reason = "this is memory-unsafe to override in user code",
111 fn type_id(&self, _: private::Internal) -> TypeId
118 /// Returns a stack backtrace, if available, of where this error occurred.
120 /// This function allows inspecting the location, in code, of where an error
121 /// happened. The returned `Backtrace` contains information about the stack
122 /// trace of the OS thread of execution of where the error originated from.
124 /// Note that not all errors contain a `Backtrace`. Also note that a
125 /// `Backtrace` may actually be empty. For more information consult the
126 /// `Backtrace` type itself.
127 #[unstable(feature = "backtrace", issue = "53487")]
128 fn backtrace(&self) -> Option<&Backtrace> {
133 /// if let Err(e) = "xc".parse::<u32>() {
134 /// // Print `e` itself, no need for description().
135 /// eprintln!("Error: {}", e);
138 #[stable(feature = "rust1", since = "1.0.0")]
139 #[rustc_deprecated(since = "1.42.0", reason = "use the Display impl or to_string()")]
140 fn description(&self) -> &str {
141 "description() is deprecated; use Display"
144 #[stable(feature = "rust1", since = "1.0.0")]
147 reason = "replaced by Error::source, which can support downcasting"
149 #[allow(missing_docs)]
150 fn cause(&self) -> Option<&dyn Error> {
156 // This is a hack to prevent `type_id` from being overridden by `Error`
157 // implementations, since that can enable unsound downcasting.
158 #[unstable(feature = "error_type_id", issue = "60784")]
163 #[stable(feature = "rust1", since = "1.0.0")]
164 impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
165 /// Converts a type of [`Error`] into a box of dyn [`Error`].
167 /// [`Error`]: ../error/trait.Error.html
172 /// use std::error::Error;
179 /// impl fmt::Display for AnError {
180 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181 /// write!(f , "An error")
185 /// impl Error for AnError {}
187 /// let an_error = AnError;
188 /// assert!(0 == mem::size_of_val(&an_error));
189 /// let a_boxed_error = Box::<dyn Error>::from(an_error);
190 /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
192 fn from(err: E) -> Box<dyn Error + 'a> {
197 #[stable(feature = "rust1", since = "1.0.0")]
198 impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
199 /// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of
200 /// dyn [`Error`] + [`Send`] + [`Sync`].
202 /// [`Error`]: ../error/trait.Error.html
207 /// use std::error::Error;
214 /// impl fmt::Display for AnError {
215 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216 /// write!(f , "An error")
220 /// impl Error for AnError {}
222 /// unsafe impl Send for AnError {}
224 /// unsafe impl Sync for AnError {}
226 /// let an_error = AnError;
227 /// assert!(0 == mem::size_of_val(&an_error));
228 /// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(an_error);
230 /// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
232 fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
237 #[stable(feature = "rust1", since = "1.0.0")]
238 impl From<String> for Box<dyn Error + Send + Sync> {
239 /// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
241 /// [`Error`]: ../error/trait.Error.html
246 /// use std::error::Error;
249 /// let a_string_error = "a string error".to_string();
250 /// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_string_error);
252 /// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
255 fn from(err: String) -> Box<dyn Error + Send + Sync> {
256 struct StringError(String);
258 impl Error for StringError {
260 fn description(&self) -> &str {
265 impl Display for StringError {
266 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
267 Display::fmt(&self.0, f)
271 // Purposefully skip printing "StringError(..)"
272 impl Debug for StringError {
273 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
274 Debug::fmt(&self.0, f)
278 Box::new(StringError(err))
282 #[stable(feature = "string_box_error", since = "1.6.0")]
283 impl From<String> for Box<dyn Error> {
284 /// Converts a [`String`] into a box of dyn [`Error`].
286 /// [`Error`]: ../error/trait.Error.html
291 /// use std::error::Error;
294 /// let a_string_error = "a string error".to_string();
295 /// let a_boxed_error = Box::<dyn Error>::from(a_string_error);
296 /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
298 fn from(str_err: String) -> Box<dyn Error> {
299 let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
300 let err2: Box<dyn Error> = err1;
305 #[stable(feature = "rust1", since = "1.0.0")]
306 impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
307 /// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
309 /// [`Error`]: ../error/trait.Error.html
314 /// use std::error::Error;
317 /// let a_str_error = "a str error";
318 /// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_str_error);
320 /// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
323 fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
324 From::from(String::from(err))
328 #[stable(feature = "string_box_error", since = "1.6.0")]
329 impl From<&str> for Box<dyn Error> {
330 /// Converts a [`str`] into a box of dyn [`Error`].
332 /// [`Error`]: ../error/trait.Error.html
337 /// use std::error::Error;
340 /// let a_str_error = "a str error";
341 /// let a_boxed_error = Box::<dyn Error>::from(a_str_error);
342 /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
344 fn from(err: &str) -> Box<dyn Error> {
345 From::from(String::from(err))
349 #[stable(feature = "cow_box_error", since = "1.22.0")]
350 impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
351 /// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
353 /// [`Cow`]: ../borrow/enum.Cow.html
354 /// [`Error`]: ../error/trait.Error.html
359 /// use std::error::Error;
361 /// use std::borrow::Cow;
363 /// let a_cow_str_error = Cow::from("a str error");
364 /// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_cow_str_error);
366 /// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
368 fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
369 From::from(String::from(err))
373 #[stable(feature = "cow_box_error", since = "1.22.0")]
374 impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
375 /// Converts a [`Cow`] into a box of dyn [`Error`].
377 /// [`Cow`]: ../borrow/enum.Cow.html
378 /// [`Error`]: ../error/trait.Error.html
383 /// use std::error::Error;
385 /// use std::borrow::Cow;
387 /// let a_cow_str_error = Cow::from("a str error");
388 /// let a_boxed_error = Box::<dyn Error>::from(a_cow_str_error);
389 /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
391 fn from(err: Cow<'a, str>) -> Box<dyn Error> {
392 From::from(String::from(err))
396 #[unstable(feature = "never_type", issue = "35121")]
400 feature = "allocator_api",
401 reason = "the precise API and guarantees it provides may be tweaked.",
404 impl Error for AllocErr {}
407 feature = "allocator_api",
408 reason = "the precise API and guarantees it provides may be tweaked.",
411 impl Error for LayoutErr {}
413 #[stable(feature = "rust1", since = "1.0.0")]
414 impl Error for str::ParseBoolError {
416 fn description(&self) -> &str {
417 "failed to parse bool"
421 #[stable(feature = "rust1", since = "1.0.0")]
422 impl Error for str::Utf8Error {
424 fn description(&self) -> &str {
425 "invalid utf-8: corrupt contents"
429 #[stable(feature = "rust1", since = "1.0.0")]
430 impl Error for num::ParseIntError {
432 fn description(&self) -> &str {
437 #[stable(feature = "try_from", since = "1.34.0")]
438 impl Error for num::TryFromIntError {
440 fn description(&self) -> &str {
445 #[stable(feature = "try_from", since = "1.34.0")]
446 impl Error for array::TryFromSliceError {
448 fn description(&self) -> &str {
453 #[stable(feature = "rust1", since = "1.0.0")]
454 impl Error for num::ParseFloatError {
456 fn description(&self) -> &str {
461 #[stable(feature = "rust1", since = "1.0.0")]
462 impl Error for string::FromUtf8Error {
464 fn description(&self) -> &str {
469 #[stable(feature = "rust1", since = "1.0.0")]
470 impl Error for string::FromUtf16Error {
472 fn description(&self) -> &str {
477 #[stable(feature = "str_parse_error2", since = "1.8.0")]
478 impl Error for Infallible {
479 fn description(&self) -> &str {
484 #[stable(feature = "decode_utf16", since = "1.9.0")]
485 impl Error for char::DecodeUtf16Error {
487 fn description(&self) -> &str {
488 "unpaired surrogate found"
492 #[stable(feature = "box_error", since = "1.8.0")]
493 impl<T: Error> Error for Box<T> {
494 #[allow(deprecated, deprecated_in_future)]
495 fn description(&self) -> &str {
496 Error::description(&**self)
500 fn cause(&self) -> Option<&dyn Error> {
501 Error::cause(&**self)
504 fn source(&self) -> Option<&(dyn Error + 'static)> {
505 Error::source(&**self)
509 #[stable(feature = "fmt_error", since = "1.11.0")]
510 impl Error for fmt::Error {
512 fn description(&self) -> &str {
513 "an error occurred when formatting an argument"
517 #[stable(feature = "try_borrow", since = "1.13.0")]
518 impl Error for cell::BorrowError {
520 fn description(&self) -> &str {
521 "already mutably borrowed"
525 #[stable(feature = "try_borrow", since = "1.13.0")]
526 impl Error for cell::BorrowMutError {
528 fn description(&self) -> &str {
533 #[stable(feature = "try_from", since = "1.34.0")]
534 impl Error for char::CharTryFromError {
536 fn description(&self) -> &str {
537 "converted integer out of range for `char`"
541 #[stable(feature = "char_from_str", since = "1.20.0")]
542 impl Error for char::ParseCharError {
544 fn description(&self) -> &str {
549 #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
550 impl Error for alloc::collections::TryReserveError {}
552 // Copied from `any.rs`.
553 impl dyn Error + 'static {
554 /// Returns `true` if the boxed type is the same as `T`
555 #[stable(feature = "error_downcast", since = "1.3.0")]
557 pub fn is<T: Error + 'static>(&self) -> bool {
558 // Get `TypeId` of the type this function is instantiated with.
559 let t = TypeId::of::<T>();
561 // Get `TypeId` of the type in the trait object.
562 let boxed = self.type_id(private::Internal);
564 // Compare both `TypeId`s on equality.
568 /// Returns some reference to the boxed value if it is of type `T`, or
569 /// `None` if it isn't.
570 #[stable(feature = "error_downcast", since = "1.3.0")]
572 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
574 unsafe { Some(&*(self as *const dyn Error as *const T)) }
580 /// Returns some mutable reference to the boxed value if it is of type `T`, or
581 /// `None` if it isn't.
582 #[stable(feature = "error_downcast", since = "1.3.0")]
584 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
586 unsafe { Some(&mut *(self as *mut dyn Error as *mut T)) }
593 impl dyn Error + 'static + Send {
594 /// Forwards to the method defined on the type `dyn Error`.
595 #[stable(feature = "error_downcast", since = "1.3.0")]
597 pub fn is<T: Error + 'static>(&self) -> bool {
598 <dyn Error + 'static>::is::<T>(self)
601 /// Forwards to the method defined on the type `dyn Error`.
602 #[stable(feature = "error_downcast", since = "1.3.0")]
604 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
605 <dyn Error + 'static>::downcast_ref::<T>(self)
608 /// Forwards to the method defined on the type `dyn Error`.
609 #[stable(feature = "error_downcast", since = "1.3.0")]
611 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
612 <dyn Error + 'static>::downcast_mut::<T>(self)
616 impl dyn Error + 'static + Send + Sync {
617 /// Forwards to the method defined on the type `dyn Error`.
618 #[stable(feature = "error_downcast", since = "1.3.0")]
620 pub fn is<T: Error + 'static>(&self) -> bool {
621 <dyn Error + 'static>::is::<T>(self)
624 /// Forwards to the method defined on the type `dyn Error`.
625 #[stable(feature = "error_downcast", since = "1.3.0")]
627 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
628 <dyn Error + 'static>::downcast_ref::<T>(self)
631 /// Forwards to the method defined on the type `dyn Error`.
632 #[stable(feature = "error_downcast", since = "1.3.0")]
634 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
635 <dyn Error + 'static>::downcast_mut::<T>(self)
641 #[stable(feature = "error_downcast", since = "1.3.0")]
642 /// Attempts to downcast the box to a concrete type.
643 pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
646 let raw: *mut dyn Error = Box::into_raw(self);
647 Ok(Box::from_raw(raw as *mut T))
654 /// Returns an iterator starting with the current error and continuing with
655 /// recursively calling [`source`].
657 /// If you want to omit the current error and only use its sources,
663 /// #![feature(error_iter)]
664 /// use std::error::Error;
671 /// struct B(Option<Box<dyn Error + 'static>>);
673 /// impl fmt::Display for A {
674 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
679 /// impl fmt::Display for B {
680 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
685 /// impl Error for A {}
687 /// impl Error for B {
688 /// fn source(&self) -> Option<&(dyn Error + 'static)> {
689 /// self.0.as_ref().map(|e| e.as_ref())
693 /// let b = B(Some(Box::new(A)));
695 /// // let err : Box<Error> = b.into(); // or
696 /// let err = &b as &(dyn Error);
698 /// let mut iter = err.chain();
700 /// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
701 /// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
702 /// assert!(iter.next().is_none());
703 /// assert!(iter.next().is_none());
706 /// [`source`]: trait.Error.html#method.source
707 #[unstable(feature = "error_iter", issue = "58520")]
709 pub fn chain(&self) -> Chain<'_> {
710 Chain { current: Some(self) }
714 /// An iterator over an [`Error`] and its sources.
716 /// If you want to omit the initial error and only process
717 /// its sources, use `skip(1)`.
719 /// [`Error`]: trait.Error.html
720 #[unstable(feature = "error_iter", issue = "58520")]
721 #[derive(Clone, Debug)]
722 pub struct Chain<'a> {
723 current: Option<&'a (dyn Error + 'static)>,
726 #[unstable(feature = "error_iter", issue = "58520")]
727 impl<'a> Iterator for Chain<'a> {
728 type Item = &'a (dyn Error + 'static);
730 fn next(&mut self) -> Option<Self::Item> {
731 let current = self.current;
732 self.current = self.current.and_then(Error::source);
737 impl dyn Error + Send {
739 #[stable(feature = "error_downcast", since = "1.3.0")]
740 /// Attempts to downcast the box to a concrete type.
741 pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error + Send>> {
742 let err: Box<dyn Error> = self;
743 <dyn Error>::downcast(err).map_err(|s| unsafe {
744 // Reapply the `Send` marker.
745 transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
750 impl dyn Error + Send + Sync {
752 #[stable(feature = "error_downcast", since = "1.3.0")]
753 /// Attempts to downcast the box to a concrete type.
754 pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
755 let err: Box<dyn Error> = self;
756 <dyn Error>::downcast(err).map_err(|s| unsafe {
757 // Reapply the `Send + Sync` marker.
758 transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
768 #[derive(Debug, PartialEq)]
770 #[derive(Debug, PartialEq)]
773 impl fmt::Display for A {
774 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
778 impl fmt::Display for B {
779 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
790 let a = &mut a as &mut (dyn Error + 'static);
791 assert_eq!(a.downcast_ref::<A>(), Some(&A));
792 assert_eq!(a.downcast_ref::<B>(), None);
793 assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
794 assert_eq!(a.downcast_mut::<B>(), None);
796 let a: Box<dyn Error> = Box::new(A);
797 match a.downcast::<B>() {
798 Ok(..) => panic!("expected error"),
799 Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),