|
| 1 | +// Copyright 2015 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 | +use boxed::Box; |
| 12 | +use clone::Clone; |
| 13 | +use error::Error as StdError; |
| 14 | +use fmt; |
| 15 | +use option::Option::{self, Some, None}; |
| 16 | +use result; |
| 17 | +use string::String; |
| 18 | +use sys; |
| 19 | + |
| 20 | +/// A type for results generated by I/O related functions where the `Err` type |
| 21 | +/// is hard-wired to `io::Error`. |
| 22 | +/// |
| 23 | +/// This typedef is generally used to avoid writing out `io::Error` directly and |
| 24 | +/// is otherwise a direct mapping to `std::result::Result`. |
| 25 | +pub type Result<T> = result::Result<T, Error>; |
| 26 | + |
| 27 | +/// The error type for I/O operations of the `Read`, `Write`, `Seek`, and |
| 28 | +/// associated traits. |
| 29 | +/// |
| 30 | +/// Errors mostly originate from the underlying OS, but custom instances of |
| 31 | +/// `Error` can be created with crafted error messages and a particular value of |
| 32 | +/// `ErrorKind`. |
| 33 | +#[derive(PartialEq, Eq, Clone, Debug)] |
| 34 | +pub struct Error { |
| 35 | + repr: Repr, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(PartialEq, Eq, Clone, Debug)] |
| 39 | +enum Repr { |
| 40 | + Os(i32), |
| 41 | + Custom(Box<Custom>), |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(PartialEq, Eq, Clone, Debug)] |
| 45 | +struct Custom { |
| 46 | + kind: ErrorKind, |
| 47 | + desc: &'static str, |
| 48 | + detail: Option<String> |
| 49 | +} |
| 50 | + |
| 51 | +/// A list specifying general categories of I/O error. |
| 52 | +#[derive(Copy, PartialEq, Eq, Clone, Debug)] |
| 53 | +pub enum ErrorKind { |
| 54 | + /// The file was not found. |
| 55 | + FileNotFound, |
| 56 | + /// The file permissions disallowed access to this file. |
| 57 | + PermissionDenied, |
| 58 | + /// The connection was refused by the remote server. |
| 59 | + ConnectionRefused, |
| 60 | + /// The connection was reset by the remote server. |
| 61 | + ConnectionReset, |
| 62 | + /// The connection was aborted (terminated) by the remote server. |
| 63 | + ConnectionAborted, |
| 64 | + /// The network operation failed because it was not connected yet. |
| 65 | + NotConnected, |
| 66 | + /// The operation failed because a pipe was closed. |
| 67 | + BrokenPipe, |
| 68 | + /// A file already existed with that name. |
| 69 | + PathAlreadyExists, |
| 70 | + /// No file exists at that location. |
| 71 | + PathDoesntExist, |
| 72 | + /// The path did not specify the type of file that this operation required. |
| 73 | + /// For example, attempting to copy a directory with the `fs::copy()` |
| 74 | + /// operation will fail with this error. |
| 75 | + MismatchedFileTypeForOperation, |
| 76 | + /// The operation temporarily failed (for example, because a signal was |
| 77 | + /// received), and retrying may succeed. |
| 78 | + ResourceUnavailable, |
| 79 | + /// A parameter was incorrect in a way that caused an I/O error not part of |
| 80 | + /// this list. |
| 81 | + InvalidInput, |
| 82 | + /// The I/O operation's timeout expired, causing it to be canceled. |
| 83 | + TimedOut, |
| 84 | + /// An error returned when an operation could not be completed because a |
| 85 | + /// call to `write` returned `Ok(0)`. |
| 86 | + /// |
| 87 | + /// This typically means that an operation could only succeed if it wrote a |
| 88 | + /// particular number of bytes but only a smaller number of bytes could be |
| 89 | + /// written. |
| 90 | + WriteZero, |
| 91 | + /// This operation was interrupted |
| 92 | + Interrupted, |
| 93 | + /// Any I/O error not part of this list. |
| 94 | + Other, |
| 95 | +} |
| 96 | + |
| 97 | +impl Error { |
| 98 | + /// Creates a new custom error from a specified kind/description/detail. |
| 99 | + pub fn new(kind: ErrorKind, |
| 100 | + description: &'static str, |
| 101 | + detail: Option<String>) -> Error { |
| 102 | + Error { |
| 103 | + repr: Repr::Custom(Box::new(Custom { |
| 104 | + kind: kind, |
| 105 | + desc: description, |
| 106 | + detail: detail, |
| 107 | + })) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// Returns an error representing the last OS error which occurred. |
| 112 | + /// |
| 113 | + /// This function reads the value of `errno` for the target platform (e.g. |
| 114 | + /// `GetLastError` on Windows) and will return a corresponding instance of |
| 115 | + /// `Error` for the error code. |
| 116 | + pub fn last_os_error() -> Error { |
| 117 | + Error::from_os_error(sys::os::errno() as i32) |
| 118 | + } |
| 119 | + |
| 120 | + /// Creates a new instance of an `Error` from a particular OS error code. |
| 121 | + pub fn from_os_error(code: i32) -> Error { |
| 122 | + Error { repr: Repr::Os(code) } |
| 123 | + } |
| 124 | + |
| 125 | + /// Return the corresponding `ErrorKind` for this error. |
| 126 | + pub fn kind(&self) -> ErrorKind { |
| 127 | + match self.repr { |
| 128 | + Repr::Os(code) => sys::decode_error_kind(code), |
| 129 | + Repr::Custom(ref c) => c.kind, |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /// Returns a short description for this error message |
| 134 | + pub fn description(&self) -> &str { |
| 135 | + match self.repr { |
| 136 | + Repr::Os(..) => "os error", |
| 137 | + Repr::Custom(ref c) => c.desc, |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /// Returns a detailed error message for this error (if one is available) |
| 142 | + pub fn detail(&self) -> Option<String> { |
| 143 | + match self.repr { |
| 144 | + Repr::Os(code) => Some(sys::os::error_string(code)), |
| 145 | + Repr::Custom(ref s) => s.detail.clone(), |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +impl fmt::Display for Error { |
| 151 | + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 152 | + match self.repr { |
| 153 | + Repr::Os(code) => { |
| 154 | + let detail = sys::os::error_string(code); |
| 155 | + write!(fmt, "{} (os error {})", detail, code) |
| 156 | + } |
| 157 | + Repr::Custom(ref c) => { |
| 158 | + match **c { |
| 159 | + Custom { |
| 160 | + kind: ErrorKind::Other, |
| 161 | + desc: "unknown error", |
| 162 | + detail: Some(ref detail) |
| 163 | + } => { |
| 164 | + write!(fmt, "{}", detail) |
| 165 | + } |
| 166 | + Custom { detail: None, desc, .. } => |
| 167 | + write!(fmt, "{}", desc), |
| 168 | + Custom { detail: Some(ref detail), desc, .. } => |
| 169 | + write!(fmt, "{} ({})", desc, detail) |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +impl StdError for Error { |
| 177 | + fn description(&self) -> &str { |
| 178 | + match self.repr { |
| 179 | + Repr::Os(..) => "os error", |
| 180 | + Repr::Custom(ref c) => c.desc, |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments