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.
57 use fmt
::{self, Debug, Display}
;
63 /// Base functionality for all errors in Rust.
64 #[stable(feature = "rust1", since = "1.0.0")]
65 pub trait Error
: Debug
+ Display
{
66 /// A short description of the error.
68 /// The description should only be used for a simple message.
69 /// It should not contain newlines or sentence-ending punctuation,
70 /// to facilitate embedding in larger user-facing strings.
71 /// For showing formatted error messages with more information see
74 /// [`Display`]: ../fmt/trait.Display.html
79 /// use std::error::Error;
81 /// match "xc".parse::<u32>() {
83 /// println!("Error: {}", e.description());
85 /// _ => println!("No error"),
88 #[stable(feature = "rust1", since = "1.0.0")]
89 fn description(&self) -> &str;
91 /// The lower-level cause of this error, if any.
96 /// use std::error::Error;
100 /// struct SuperError {
101 /// side: SuperErrorSideKick,
104 /// impl fmt::Display for SuperError {
105 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106 /// write!(f, "SuperError is here!")
110 /// impl Error for SuperError {
111 /// fn description(&self) -> &str {
112 /// "I'm the superhero of errors"
115 /// fn cause(&self) -> Option<&Error> {
121 /// struct SuperErrorSideKick;
123 /// impl fmt::Display for SuperErrorSideKick {
124 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125 /// write!(f, "SuperErrorSideKick is here!")
129 /// impl Error for SuperErrorSideKick {
130 /// fn description(&self) -> &str {
131 /// "I'm SuperError side kick"
135 /// fn get_super_error() -> Result<(), SuperError> {
136 /// Err(SuperError { side: SuperErrorSideKick })
140 /// match get_super_error() {
142 /// println!("Error: {}", e.description());
143 /// println!("Caused by: {}", e.cause().unwrap());
145 /// _ => println!("No error"),
149 #[stable(feature = "rust1", since = "1.0.0")]
150 fn cause(&self) -> Option
<&Error
> { None }
152 /// Get the `TypeId` of `self`
154 #[unstable(feature = "error_type_id",
155 reason
= "unclear whether to commit to this public implementation detail",
157 fn type_id(&self) -> TypeId
where Self: '
static {
162 #[stable(feature = "rust1", since = "1.0.0")]
163 impl<'a
, E
: Error
+ 'a
> From
<E
> for Box
<Error
+ 'a
> {
164 fn from(err
: E
) -> Box
<Error
+ 'a
> {
169 #[stable(feature = "rust1", since = "1.0.0")]
170 impl<'a
, E
: Error
+ Send
+ Sync
+ 'a
> From
<E
> for Box
<Error
+ Send
+ Sync
+ 'a
> {
171 fn from(err
: E
) -> Box
<Error
+ Send
+ Sync
+ 'a
> {
176 #[stable(feature = "rust1", since = "1.0.0")]
177 impl From
<String
> for Box
<Error
+ Send
+ Sync
> {
178 fn from(err
: String
) -> Box
<Error
+ Send
+ Sync
> {
180 struct StringError(String
);
182 impl Error
for StringError
{
183 fn description(&self) -> &str { &self.0 }
186 impl Display
for StringError
{
187 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
188 Display
::fmt(&self.0, f
)
192 Box
::new(StringError(err
))
196 #[stable(feature = "string_box_error", since = "1.6.0")]
197 impl From
<String
> for Box
<Error
> {
198 fn from(str_err
: String
) -> Box
<Error
> {
199 let err1
: Box
<Error
+ Send
+ Sync
> = From
::from(str_err
);
200 let err2
: Box
<Error
> = err1
;
205 #[stable(feature = "rust1", since = "1.0.0")]
206 impl<'a
, 'b
> From
<&'b
str> for Box
<Error
+ Send
+ Sync
+ 'a
> {
207 fn from(err
: &'b
str) -> Box
<Error
+ Send
+ Sync
+ 'a
> {
208 From
::from(String
::from(err
))
212 #[stable(feature = "string_box_error", since = "1.6.0")]
213 impl<'a
> From
<&'a
str> for Box
<Error
> {
214 fn from(err
: &'a
str) -> Box
<Error
> {
215 From
::from(String
::from(err
))
219 #[unstable(feature = "never_type_impls", issue = "35121")]
221 fn description(&self) -> &str { *self }
224 #[stable(feature = "rust1", since = "1.0.0")]
225 impl Error
for str::ParseBoolError
{
226 fn description(&self) -> &str { "failed to parse bool" }
229 #[stable(feature = "rust1", since = "1.0.0")]
230 impl Error
for str::Utf8Error
{
231 fn description(&self) -> &str {
232 "invalid utf-8: corrupt contents"
236 #[stable(feature = "rust1", since = "1.0.0")]
237 impl Error
for num
::ParseIntError
{
238 fn description(&self) -> &str {
243 #[unstable(feature = "try_from", issue = "33417")]
244 impl Error
for num
::TryFromIntError
{
245 fn description(&self) -> &str {
250 #[stable(feature = "rust1", since = "1.0.0")]
251 impl Error
for num
::ParseFloatError
{
252 fn description(&self) -> &str {
257 #[stable(feature = "rust1", since = "1.0.0")]
258 impl Error
for string
::FromUtf8Error
{
259 fn description(&self) -> &str {
264 #[stable(feature = "rust1", since = "1.0.0")]
265 impl Error
for string
::FromUtf16Error
{
266 fn description(&self) -> &str {
271 #[stable(feature = "str_parse_error2", since = "1.8.0")]
272 impl Error
for string
::ParseError
{
273 fn description(&self) -> &str {
278 #[stable(feature = "decode_utf16", since = "1.9.0")]
279 impl Error
for char::DecodeUtf16Error
{
280 fn description(&self) -> &str {
281 "unpaired surrogate found"
285 #[stable(feature = "box_error", since = "1.8.0")]
286 impl<T
: Error
> Error
for Box
<T
> {
287 fn description(&self) -> &str {
288 Error
::description(&**self)
291 fn cause(&self) -> Option
<&Error
> {
292 Error
::cause(&**self)
296 #[stable(feature = "fmt_error", since = "1.11.0")]
297 impl Error
for fmt
::Error
{
298 fn description(&self) -> &str {
299 "an error occurred when formatting an argument"
303 #[stable(feature = "try_borrow", since = "1.13.0")]
304 impl Error
for cell
::BorrowError
{
305 fn description(&self) -> &str {
306 "already mutably borrowed"
310 #[stable(feature = "try_borrow", since = "1.13.0")]
311 impl Error
for cell
::BorrowMutError
{
312 fn description(&self) -> &str {
317 #[unstable(feature = "try_from", issue = "33417")]
318 impl Error
for char::CharTryFromError
{
319 fn description(&self) -> &str {
320 "converted integer out of range for `char`"
324 // copied from any.rs
325 impl Error
+ '
static {
326 /// Returns true if the boxed type is the same as `T`
327 #[stable(feature = "error_downcast", since = "1.3.0")]
329 pub fn is
<T
: Error
+ '
static>(&self) -> bool
{
330 // Get TypeId of the type this function is instantiated with
331 let t
= TypeId
::of
::<T
>();
333 // Get TypeId of the type in the trait object
334 let boxed
= self.type_id();
336 // Compare both TypeIds on equality
340 /// Returns some reference to the boxed value if it is of type `T`, or
341 /// `None` if it isn't.
342 #[stable(feature = "error_downcast", since = "1.3.0")]
344 pub fn downcast_ref
<T
: Error
+ '
static>(&self) -> Option
<&T
> {
347 Some(&*(self as *const Error
as *const T
))
354 /// Returns some mutable reference to the boxed value if it is of type `T`, or
355 /// `None` if it isn't.
356 #[stable(feature = "error_downcast", since = "1.3.0")]
358 pub fn downcast_mut
<T
: Error
+ '
static>(&mut self) -> Option
<&mut T
> {
361 Some(&mut *(self as *mut Error
as *mut T
))
369 impl Error
+ '
static + Send
{
370 /// Forwards to the method defined on the type `Any`.
371 #[stable(feature = "error_downcast", since = "1.3.0")]
373 pub fn is
<T
: Error
+ '
static>(&self) -> bool
{
374 <Error
+ '
static>::is
::<T
>(self)
377 /// Forwards to the method defined on the type `Any`.
378 #[stable(feature = "error_downcast", since = "1.3.0")]
380 pub fn downcast_ref
<T
: Error
+ '
static>(&self) -> Option
<&T
> {
381 <Error
+ '
static>::downcast_ref
::<T
>(self)
384 /// Forwards to the method defined on the type `Any`.
385 #[stable(feature = "error_downcast", since = "1.3.0")]
387 pub fn downcast_mut
<T
: Error
+ '
static>(&mut self) -> Option
<&mut T
> {
388 <Error
+ '
static>::downcast_mut
::<T
>(self)
392 impl Error
+ '
static + Send
+ Sync
{
393 /// Forwards to the method defined on the type `Any`.
394 #[stable(feature = "error_downcast", since = "1.3.0")]
396 pub fn is
<T
: Error
+ '
static>(&self) -> bool
{
397 <Error
+ '
static>::is
::<T
>(self)
400 /// Forwards to the method defined on the type `Any`.
401 #[stable(feature = "error_downcast", since = "1.3.0")]
403 pub fn downcast_ref
<T
: Error
+ '
static>(&self) -> Option
<&T
> {
404 <Error
+ '
static>::downcast_ref
::<T
>(self)
407 /// Forwards to the method defined on the type `Any`.
408 #[stable(feature = "error_downcast", since = "1.3.0")]
410 pub fn downcast_mut
<T
: Error
+ '
static>(&mut self) -> Option
<&mut T
> {
411 <Error
+ '
static>::downcast_mut
::<T
>(self)
417 #[stable(feature = "error_downcast", since = "1.3.0")]
418 /// Attempt to downcast the box to a concrete type.
419 pub fn downcast
<T
: Error
+ '
static>(self: Box
<Self>) -> Result
<Box
<T
>, Box
<Error
>> {
422 let raw
: *mut Error
= Box
::into_raw(self);
423 Ok(Box
::from_raw(raw
as *mut T
))
433 #[stable(feature = "error_downcast", since = "1.3.0")]
434 /// Attempt to downcast the box to a concrete type.
435 pub fn downcast
<T
: Error
+ '
static>(self: Box
<Self>)
436 -> Result
<Box
<T
>, Box
<Error
+ Send
>> {
437 let err
: Box
<Error
> = self;
438 <Error
>::downcast(err
).map_err(|s
| unsafe {
439 // reapply the Send marker
440 transmute
::<Box
<Error
>, Box
<Error
+ Send
>>(s
)
445 impl Error
+ Send
+ Sync
{
447 #[stable(feature = "error_downcast", since = "1.3.0")]
448 /// Attempt to downcast the box to a concrete type.
449 pub fn downcast
<T
: Error
+ '
static>(self: Box
<Self>)
450 -> Result
<Box
<T
>, Box
<Self>> {
451 let err
: Box
<Error
> = self;
452 <Error
>::downcast(err
).map_err(|s
| unsafe {
453 // reapply the Send+Sync marker
454 transmute
::<Box
<Error
>, Box
<Error
+ Send
+ Sync
>>(s
)
464 #[derive(Debug, PartialEq)]
466 #[derive(Debug, PartialEq)]
469 impl fmt
::Display
for A
{
470 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
474 impl fmt
::Display
for B
{
475 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
481 fn description(&self) -> &str { "A-desc" }
484 fn description(&self) -> &str { "A-desc" }
490 let mut a
= &mut a
as &mut (Error
+ '
static);
491 assert_eq
!(a
.downcast_ref
::<A
>(), Some(&A
));
492 assert_eq
!(a
.downcast_ref
::<B
>(), None
);
493 assert_eq
!(a
.downcast_mut
::<A
>(), Some(&mut A
));
494 assert_eq
!(a
.downcast_mut
::<B
>(), None
);
496 let a
: Box
<Error
> = Box
::new(A
);
497 match a
.downcast
::<B
>() {
498 Ok(..) => panic
!("expected error"),
499 Err(e
) => assert_eq
!(*e
.downcast
::<A
>().unwrap(), A
),