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 #![stable(feature = "rust1", since = "1.0.0")]
15 // A note about crates and the facade:
17 // Originally, the `Error` trait was defined in libcore, and the impls
18 // were scattered about. However, coherence objected to this
19 // arrangement, because to create the blanket impls for `Box` required
20 // knowing that `&str: !Error`, and we have no means to deal with that
21 // sort of conflict just now. Therefore, for the time being, we have
22 // moved the `Error` trait into libstd. As we evolve a sol'n to the
23 // coherence challenge (e.g., specialization, neg impls, etc) we can
24 // reconsider what crate these items belong in.
26 use alloc::{AllocErr, LayoutErr, CannotReallocInPlace};
32 use fmt::{self, Debug, Display};
38 /// `Error` is a trait representing the basic expectations for error values,
39 /// i.e. values of type `E` in [`Result<T, E>`]. Errors must describe
40 /// themselves through the [`Display`] and [`Debug`] traits, and may provide
41 /// cause chain information:
43 /// The [`cause`] method is generally used when errors cross "abstraction
44 /// boundaries", i.e. when a one module must report an error that is "caused"
45 /// by an error from a lower-level module. This setup makes it possible for the
46 /// high-level module to provide its own errors that do not commit to any
47 /// particular implementation, but also reveal some of its implementation for
48 /// debugging via [`cause`] chains.
50 /// [`Result<T, E>`]: ../result/enum.Result.html
51 /// [`Display`]: ../fmt/trait.Display.html
52 /// [`Debug`]: ../fmt/trait.Debug.html
53 /// [`cause`]: trait.Error.html#method.cause
54 #[stable(feature = "rust1", since = "1.0.0")]
55 pub trait Error: Debug + Display {
56 /// **This method is soft-deprecated.**
58 /// Although using it won’t cause compilation warning,
59 /// new code should use [`Display`] instead
60 /// and new `impl`s can omit it.
62 /// To obtain error description as a string, use `to_string()`.
64 /// [`Display`]: ../fmt/trait.Display.html
69 /// match "xc".parse::<u32>() {
71 /// // Print `e` itself, not `e.description()`.
72 /// println!("Error: {}", e);
74 /// _ => println!("No error"),
77 #[stable(feature = "rust1", since = "1.0.0")]
78 fn description(&self) -> &str {
79 "description() is deprecated; use Display"
82 /// The lower-level cause of this error, if any.
87 /// use std::error::Error;
91 /// struct SuperError {
92 /// side: SuperErrorSideKick,
95 /// impl fmt::Display for SuperError {
96 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97 /// write!(f, "SuperError is here!")
101 /// impl Error for SuperError {
102 /// fn description(&self) -> &str {
103 /// "I'm the superhero of errors"
106 /// fn cause(&self) -> Option<&Error> {
112 /// struct SuperErrorSideKick;
114 /// impl fmt::Display for SuperErrorSideKick {
115 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116 /// write!(f, "SuperErrorSideKick is here!")
120 /// impl Error for SuperErrorSideKick {
121 /// fn description(&self) -> &str {
122 /// "I'm SuperError side kick"
126 /// fn get_super_error() -> Result<(), SuperError> {
127 /// Err(SuperError { side: SuperErrorSideKick })
131 /// match get_super_error() {
133 /// println!("Error: {}", e.description());
134 /// println!("Caused by: {}", e.cause().unwrap());
136 /// _ => println!("No error"),
140 #[stable(feature = "rust1", since = "1.0.0")]
141 fn cause(&self) -> Option<&dyn Error> { None }
143 /// Get the `TypeId` of `self`
145 #[unstable(feature = "error_type_id",
146 reason = "unclear whether to commit to this public implementation detail",
148 fn type_id(&self) -> TypeId where Self: 'static {
153 #[stable(feature = "rust1", since = "1.0.0")]
154 impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
155 fn from(err: E) -> Box<dyn Error + 'a> {
160 #[stable(feature = "rust1", since = "1.0.0")]
161 impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
162 fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
167 #[stable(feature = "rust1", since = "1.0.0")]
168 impl From<String> for Box<dyn Error + Send + Sync> {
169 fn from(err: String) -> Box<dyn Error + Send + Sync> {
171 struct StringError(String);
173 impl Error for StringError {
174 fn description(&self) -> &str { &self.0 }
177 impl Display for StringError {
178 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
179 Display::fmt(&self.0, f)
183 Box::new(StringError(err))
187 #[stable(feature = "string_box_error", since = "1.6.0")]
188 impl From<String> for Box<dyn Error> {
189 fn from(str_err: String) -> Box<dyn Error> {
190 let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
191 let err2: Box<dyn Error> = err1;
196 #[stable(feature = "rust1", since = "1.0.0")]
197 impl<'a, 'b> From<&'b str> for Box<dyn Error + Send + Sync + 'a> {
198 fn from(err: &'b str) -> Box<dyn Error + Send + Sync + 'a> {
199 From::from(String::from(err))
203 #[stable(feature = "string_box_error", since = "1.6.0")]
204 impl<'a> From<&'a str> for Box<dyn Error> {
205 fn from(err: &'a str) -> Box<dyn Error> {
206 From::from(String::from(err))
210 #[stable(feature = "cow_box_error", since = "1.22.0")]
211 impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
212 fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
213 From::from(String::from(err))
217 #[stable(feature = "cow_box_error", since = "1.22.0")]
218 impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
219 fn from(err: Cow<'a, str>) -> Box<dyn Error> {
220 From::from(String::from(err))
224 #[unstable(feature = "never_type", issue = "35121")]
226 fn description(&self) -> &str { *self }
229 #[unstable(feature = "allocator_api",
230 reason = "the precise API and guarantees it provides may be tweaked.",
232 impl Error for AllocErr {
233 fn description(&self) -> &str {
234 "memory allocation failed"
238 #[unstable(feature = "allocator_api",
239 reason = "the precise API and guarantees it provides may be tweaked.",
241 impl Error for LayoutErr {
242 fn description(&self) -> &str {
243 "invalid parameters to Layout::from_size_align"
247 #[unstable(feature = "allocator_api",
248 reason = "the precise API and guarantees it provides may be tweaked.",
250 impl Error for CannotReallocInPlace {
251 fn description(&self) -> &str {
252 CannotReallocInPlace::description(self)
256 #[stable(feature = "rust1", since = "1.0.0")]
257 impl Error for str::ParseBoolError {
258 fn description(&self) -> &str { "failed to parse bool" }
261 #[stable(feature = "rust1", since = "1.0.0")]
262 impl Error for str::Utf8Error {
263 fn description(&self) -> &str {
264 "invalid utf-8: corrupt contents"
268 #[stable(feature = "rust1", since = "1.0.0")]
269 impl Error for num::ParseIntError {
270 fn description(&self) -> &str {
275 #[unstable(feature = "try_from", issue = "33417")]
276 impl Error for num::TryFromIntError {
277 fn description(&self) -> &str {
282 #[unstable(feature = "try_from", issue = "33417")]
283 impl Error for array::TryFromSliceError {
284 fn description(&self) -> &str {
289 #[stable(feature = "rust1", since = "1.0.0")]
290 impl Error for num::ParseFloatError {
291 fn description(&self) -> &str {
296 #[stable(feature = "rust1", since = "1.0.0")]
297 impl Error for string::FromUtf8Error {
298 fn description(&self) -> &str {
303 #[stable(feature = "rust1", since = "1.0.0")]
304 impl Error for string::FromUtf16Error {
305 fn description(&self) -> &str {
310 #[stable(feature = "str_parse_error2", since = "1.8.0")]
311 impl Error for string::ParseError {
312 fn description(&self) -> &str {
317 #[stable(feature = "decode_utf16", since = "1.9.0")]
318 impl Error for char::DecodeUtf16Error {
319 fn description(&self) -> &str {
320 "unpaired surrogate found"
324 #[stable(feature = "box_error", since = "1.8.0")]
325 impl<T: Error> Error for Box<T> {
326 fn description(&self) -> &str {
327 Error::description(&**self)
330 fn cause(&self) -> Option<&dyn Error> {
331 Error::cause(&**self)
335 #[stable(feature = "fmt_error", since = "1.11.0")]
336 impl Error for fmt::Error {
337 fn description(&self) -> &str {
338 "an error occurred when formatting an argument"
342 #[stable(feature = "try_borrow", since = "1.13.0")]
343 impl Error for cell::BorrowError {
344 fn description(&self) -> &str {
345 "already mutably borrowed"
349 #[stable(feature = "try_borrow", since = "1.13.0")]
350 impl Error for cell::BorrowMutError {
351 fn description(&self) -> &str {
356 #[unstable(feature = "try_from", issue = "33417")]
357 impl Error for char::CharTryFromError {
358 fn description(&self) -> &str {
359 "converted integer out of range for `char`"
363 #[stable(feature = "char_from_str", since = "1.20.0")]
364 impl Error for char::ParseCharError {
365 fn description(&self) -> &str {
370 // copied from any.rs
371 impl dyn Error + 'static {
372 /// Returns true if the boxed type is the same as `T`
373 #[stable(feature = "error_downcast", since = "1.3.0")]
375 pub fn is<T: Error + 'static>(&self) -> bool {
376 // Get TypeId of the type this function is instantiated with
377 let t = TypeId::of::<T>();
379 // Get TypeId of the type in the trait object
380 let boxed = self.type_id();
382 // Compare both TypeIds on equality
386 /// Returns some reference to the boxed value if it is of type `T`, or
387 /// `None` if it isn't.
388 #[stable(feature = "error_downcast", since = "1.3.0")]
390 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
393 Some(&*(self as *const dyn Error as *const T))
400 /// Returns some mutable reference to the boxed value if it is of type `T`, or
401 /// `None` if it isn't.
402 #[stable(feature = "error_downcast", since = "1.3.0")]
404 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
407 Some(&mut *(self as *mut dyn Error as *mut T))
415 impl dyn Error + 'static + Send {
416 /// Forwards to the method defined on the type `Any`.
417 #[stable(feature = "error_downcast", since = "1.3.0")]
419 pub fn is<T: Error + 'static>(&self) -> bool {
420 <dyn Error + 'static>::is::<T>(self)
423 /// Forwards to the method defined on the type `Any`.
424 #[stable(feature = "error_downcast", since = "1.3.0")]
426 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
427 <dyn Error + 'static>::downcast_ref::<T>(self)
430 /// Forwards to the method defined on the type `Any`.
431 #[stable(feature = "error_downcast", since = "1.3.0")]
433 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
434 <dyn Error + 'static>::downcast_mut::<T>(self)
438 impl dyn Error + 'static + Send + Sync {
439 /// Forwards to the method defined on the type `Any`.
440 #[stable(feature = "error_downcast", since = "1.3.0")]
442 pub fn is<T: Error + 'static>(&self) -> bool {
443 <dyn Error + 'static>::is::<T>(self)
446 /// Forwards to the method defined on the type `Any`.
447 #[stable(feature = "error_downcast", since = "1.3.0")]
449 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
450 <dyn Error + 'static>::downcast_ref::<T>(self)
453 /// Forwards to the method defined on the type `Any`.
454 #[stable(feature = "error_downcast", since = "1.3.0")]
456 pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
457 <dyn Error + 'static>::downcast_mut::<T>(self)
463 #[stable(feature = "error_downcast", since = "1.3.0")]
464 /// Attempt to downcast the box to a concrete type.
465 pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
468 let raw: *mut dyn Error = Box::into_raw(self);
469 Ok(Box::from_raw(raw as *mut T))
477 impl dyn Error + Send {
479 #[stable(feature = "error_downcast", since = "1.3.0")]
480 /// Attempt to downcast the box to a concrete type.
481 pub fn downcast<T: Error + 'static>(self: Box<Self>)
482 -> Result<Box<T>, Box<dyn Error + Send>> {
483 let err: Box<dyn Error> = self;
484 <dyn Error>::downcast(err).map_err(|s| unsafe {
485 // reapply the Send marker
486 transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
491 impl dyn Error + Send + Sync {
493 #[stable(feature = "error_downcast", since = "1.3.0")]
494 /// Attempt to downcast the box to a concrete type.
495 pub fn downcast<T: Error + 'static>(self: Box<Self>)
496 -> Result<Box<T>, Box<Self>> {
497 let err: Box<dyn Error> = self;
498 <dyn Error>::downcast(err).map_err(|s| unsafe {
499 // reapply the Send+Sync marker
500 transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
510 #[derive(Debug, PartialEq)]
512 #[derive(Debug, PartialEq)]
515 impl fmt::Display for A {
516 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
520 impl fmt::Display for B {
521 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
527 fn description(&self) -> &str { "A-desc" }
530 fn description(&self) -> &str { "A-desc" }
536 let a = &mut a as &mut (dyn Error + 'static);
537 assert_eq!(a.downcast_ref::<A>(), Some(&A));
538 assert_eq!(a.downcast_ref::<B>(), None);
539 assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
540 assert_eq!(a.downcast_mut::<B>(), None);
542 let a: Box<dyn Error> = Box::new(A);
543 match a.downcast::<B>() {
544 Ok(..) => panic!("expected error"),
545 Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),