|
| 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 | +#![stable(feature = "rust1", since = "1.0.0")] |
| 38 | + |
| 39 | +// A note about crates and the facade: |
| 40 | +// |
| 41 | +// Originally, the `Error` trait was defined in libcore, and the impls |
| 42 | +// were scattered about. However, coherence objected to this |
| 43 | +// arrangement, because to create the blanket impls for `Box` required |
| 44 | +// knowing that `&str: !Error`, and we have no means to deal with that |
| 45 | +// sort of conflict just now. Therefore, for the time being, we have |
| 46 | +// moved the `Error` trait into libstd. As we evolve a sol'n to the |
| 47 | +// coherence challenge (e.g., specialization, neg impls, etc) we can |
| 48 | +// reconsider what crate these items belong in. |
| 49 | + |
| 50 | +use boxed::Box; |
| 51 | +use convert::From; |
| 52 | +use fmt::{self, Debug, Display}; |
| 53 | +use marker::Send; |
| 54 | +use num; |
| 55 | +use option::Option; |
| 56 | +use option::Option::None; |
| 57 | +use str; |
| 58 | +use string::{self, String}; |
| 59 | + |
| 60 | +/// Base functionality for all errors in Rust. |
| 61 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 62 | +pub trait Error: Debug + Display { |
| 63 | + /// A short description of the error. |
| 64 | + /// |
| 65 | + /// The description should not contain newlines or sentence-ending |
| 66 | + /// punctuation, to facilitate embedding in larger user-facing |
| 67 | + /// strings. |
| 68 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 69 | + fn description(&self) -> &str; |
| 70 | + |
| 71 | + /// The lower-level cause of this error, if any. |
| 72 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 73 | + fn cause(&self) -> Option<&Error> { None } |
| 74 | +} |
| 75 | + |
| 76 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 77 | +impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> { |
| 78 | + fn from(err: E) -> Box<Error + 'a> { |
| 79 | + Box::new(err) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 84 | +impl<'a, E: Error + Send + 'a> From<E> for Box<Error + Send + 'a> { |
| 85 | + fn from(err: E) -> Box<Error + Send + 'a> { |
| 86 | + Box::new(err) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 91 | +impl<'a, 'b> From<&'b str> for Box<Error + Send + 'a> { |
| 92 | + fn from(err: &'b str) -> Box<Error + Send + 'a> { |
| 93 | + #[derive(Debug)] |
| 94 | + struct StringError(String); |
| 95 | + |
| 96 | + impl Error for StringError { |
| 97 | + fn description(&self) -> &str { &self.0 } |
| 98 | + } |
| 99 | + |
| 100 | + impl Display for StringError { |
| 101 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 102 | + Display::fmt(&self.0, f) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Box::new(StringError(String::from_str(err))) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 111 | +impl Error for str::ParseBoolError { |
| 112 | + fn description(&self) -> &str { "failed to parse bool" } |
| 113 | +} |
| 114 | + |
| 115 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 116 | +impl Error for str::Utf8Error { |
| 117 | + fn description(&self) -> &str { |
| 118 | + match *self { |
| 119 | + str::Utf8Error::TooShort => "invalid utf-8: not enough bytes", |
| 120 | + str::Utf8Error::InvalidByte(..) => "invalid utf-8: corrupt contents", |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 126 | +impl Error for num::ParseIntError { |
| 127 | + fn description(&self) -> &str { |
| 128 | + self.description() |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 133 | +impl Error for num::ParseFloatError { |
| 134 | + fn description(&self) -> &str { |
| 135 | + self.description() |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 140 | +impl Error for string::FromUtf8Error { |
| 141 | + fn description(&self) -> &str { |
| 142 | + "invalid utf-8" |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 147 | +impl Error for string::FromUtf16Error { |
| 148 | + fn description(&self) -> &str { |
| 149 | + "invalid utf-16" |
| 150 | + } |
| 151 | +} |
| 152 | + |
0 commit comments