]> git.lizzy.rs Git - rust.git/blob - src/libstd/io/error.rs
c20bd3097b27dea93720d637d91db3a814ac85e1
[rust.git] / src / libstd / io / error.rs
1 use crate::convert::From;
2 use crate::error;
3 use crate::fmt;
4 use crate::result;
5 use crate::sys;
6
7 /// A specialized [`Result`](../result/enum.Result.html) type for I/O
8 /// operations.
9 ///
10 /// This type is broadly used across [`std::io`] for any operation which may
11 /// produce an error.
12 ///
13 /// This typedef is generally used to avoid writing out [`io::Error`] directly and
14 /// is otherwise a direct mapping to [`Result`].
15 ///
16 /// While usual Rust style is to import types directly, aliases of [`Result`]
17 /// often are not, to make it easier to distinguish between them. [`Result`] is
18 /// generally assumed to be [`std::result::Result`][`Result`], and so users of this alias
19 /// will generally use `io::Result` instead of shadowing the prelude's import
20 /// of [`std::result::Result`][`Result`].
21 ///
22 /// [`std::io`]: ../io/index.html
23 /// [`io::Error`]: ../io/struct.Error.html
24 /// [`Result`]: ../result/enum.Result.html
25 ///
26 /// # Examples
27 ///
28 /// A convenience function that bubbles an `io::Result` to its caller:
29 ///
30 /// ```
31 /// use std::io;
32 ///
33 /// fn get_string() -> io::Result<String> {
34 ///     let mut buffer = String::new();
35 ///
36 ///     io::stdin().read_line(&mut buffer)?;
37 ///
38 ///     Ok(buffer)
39 /// }
40 /// ```
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub type Result<T> = result::Result<T, Error>;
43
44 /// The error type for I/O operations of the [`Read`], [`Write`], [`Seek`], and
45 /// associated traits.
46 ///
47 /// Errors mostly originate from the underlying OS, but custom instances of
48 /// `Error` can be created with crafted error messages and a particular value of
49 /// [`ErrorKind`].
50 ///
51 /// [`Read`]: ../io/trait.Read.html
52 /// [`Write`]: ../io/trait.Write.html
53 /// [`Seek`]: ../io/trait.Seek.html
54 /// [`ErrorKind`]: enum.ErrorKind.html
55 #[stable(feature = "rust1", since = "1.0.0")]
56 pub struct Error {
57     repr: Repr,
58 }
59
60 #[stable(feature = "rust1", since = "1.0.0")]
61 impl fmt::Debug for Error {
62     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63         fmt::Debug::fmt(&self.repr, f)
64     }
65 }
66
67 enum Repr {
68     Os(i32),
69     Simple(ErrorKind),
70     Custom(Box<Custom>),
71 }
72
73 #[derive(Debug)]
74 struct Custom {
75     kind: ErrorKind,
76     error: Box<dyn error::Error + Send + Sync>,
77 }
78
79 /// A list specifying general categories of I/O error.
80 ///
81 /// This list is intended to grow over time and it is not recommended to
82 /// exhaustively match against it.
83 ///
84 /// It is used with the [`io::Error`] type.
85 ///
86 /// [`io::Error`]: struct.Error.html
87 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
88 #[stable(feature = "rust1", since = "1.0.0")]
89 #[allow(deprecated)]
90 #[non_exhaustive]
91 pub enum ErrorKind {
92     /// An entity was not found, often a file.
93     #[stable(feature = "rust1", since = "1.0.0")]
94     NotFound,
95     /// The operation lacked the necessary privileges to complete.
96     #[stable(feature = "rust1", since = "1.0.0")]
97     PermissionDenied,
98     /// The connection was refused by the remote server.
99     #[stable(feature = "rust1", since = "1.0.0")]
100     ConnectionRefused,
101     /// The connection was reset by the remote server.
102     #[stable(feature = "rust1", since = "1.0.0")]
103     ConnectionReset,
104     /// The connection was aborted (terminated) by the remote server.
105     #[stable(feature = "rust1", since = "1.0.0")]
106     ConnectionAborted,
107     /// The network operation failed because it was not connected yet.
108     #[stable(feature = "rust1", since = "1.0.0")]
109     NotConnected,
110     /// A socket address could not be bound because the address is already in
111     /// use elsewhere.
112     #[stable(feature = "rust1", since = "1.0.0")]
113     AddrInUse,
114     /// A nonexistent interface was requested or the requested address was not
115     /// local.
116     #[stable(feature = "rust1", since = "1.0.0")]
117     AddrNotAvailable,
118     /// The operation failed because a pipe was closed.
119     #[stable(feature = "rust1", since = "1.0.0")]
120     BrokenPipe,
121     /// An entity already exists, often a file.
122     #[stable(feature = "rust1", since = "1.0.0")]
123     AlreadyExists,
124     /// The operation needs to block to complete, but the blocking operation was
125     /// requested to not occur.
126     #[stable(feature = "rust1", since = "1.0.0")]
127     WouldBlock,
128     /// A parameter was incorrect.
129     #[stable(feature = "rust1", since = "1.0.0")]
130     InvalidInput,
131     /// Data not valid for the operation were encountered.
132     ///
133     /// Unlike [`InvalidInput`], this typically means that the operation
134     /// parameters were valid, however the error was caused by malformed
135     /// input data.
136     ///
137     /// For example, a function that reads a file into a string will error with
138     /// `InvalidData` if the file's contents are not valid UTF-8.
139     ///
140     /// [`InvalidInput`]: #variant.InvalidInput
141     #[stable(feature = "io_invalid_data", since = "1.2.0")]
142     InvalidData,
143     /// The I/O operation's timeout expired, causing it to be canceled.
144     #[stable(feature = "rust1", since = "1.0.0")]
145     TimedOut,
146     /// An error returned when an operation could not be completed because a
147     /// call to [`write`] returned [`Ok(0)`].
148     ///
149     /// This typically means that an operation could only succeed if it wrote a
150     /// particular number of bytes but only a smaller number of bytes could be
151     /// written.
152     ///
153     /// [`write`]: ../../std/io/trait.Write.html#tymethod.write
154     /// [`Ok(0)`]: ../../std/io/type.Result.html
155     #[stable(feature = "rust1", since = "1.0.0")]
156     WriteZero,
157     /// This operation was interrupted.
158     ///
159     /// Interrupted operations can typically be retried.
160     #[stable(feature = "rust1", since = "1.0.0")]
161     Interrupted,
162     /// Any I/O error not part of this list.
163     #[stable(feature = "rust1", since = "1.0.0")]
164     Other,
165
166     /// An error returned when an operation could not be completed because an
167     /// "end of file" was reached prematurely.
168     ///
169     /// This typically means that an operation could only succeed if it read a
170     /// particular number of bytes but only a smaller number of bytes could be
171     /// read.
172     #[stable(feature = "read_exact", since = "1.6.0")]
173     UnexpectedEof,
174 }
175
176 impl ErrorKind {
177     pub(crate) fn as_str(&self) -> &'static str {
178         match *self {
179             ErrorKind::NotFound => "entity not found",
180             ErrorKind::PermissionDenied => "permission denied",
181             ErrorKind::ConnectionRefused => "connection refused",
182             ErrorKind::ConnectionReset => "connection reset",
183             ErrorKind::ConnectionAborted => "connection aborted",
184             ErrorKind::NotConnected => "not connected",
185             ErrorKind::AddrInUse => "address in use",
186             ErrorKind::AddrNotAvailable => "address not available",
187             ErrorKind::BrokenPipe => "broken pipe",
188             ErrorKind::AlreadyExists => "entity already exists",
189             ErrorKind::WouldBlock => "operation would block",
190             ErrorKind::InvalidInput => "invalid input parameter",
191             ErrorKind::InvalidData => "invalid data",
192             ErrorKind::TimedOut => "timed out",
193             ErrorKind::WriteZero => "write zero",
194             ErrorKind::Interrupted => "operation interrupted",
195             ErrorKind::Other => "other os error",
196             ErrorKind::UnexpectedEof => "unexpected end of file",
197         }
198     }
199 }
200
201 /// Intended for use for errors not exposed to the user, where allocating onto
202 /// the heap (for normal construction via Error::new) is too costly.
203 #[stable(feature = "io_error_from_errorkind", since = "1.14.0")]
204 impl From<ErrorKind> for Error {
205     /// Converts an [`ErrorKind`] into an [`Error`].
206     ///
207     /// This conversion allocates a new error with a simple representation of error kind.
208     ///
209     /// # Examples
210     ///
211     /// ```
212     /// use std::io::{Error, ErrorKind};
213     ///
214     /// let not_found = ErrorKind::NotFound;
215     /// let error = Error::from(not_found);
216     /// assert_eq!("entity not found", format!("{}", error));
217     /// ```
218     ///
219     /// [`ErrorKind`]: ../../std/io/enum.ErrorKind.html
220     /// [`Error`]: ../../std/io/struct.Error.html
221     #[inline]
222     fn from(kind: ErrorKind) -> Error {
223         Error { repr: Repr::Simple(kind) }
224     }
225 }
226
227 impl Error {
228     /// Creates a new I/O error from a known kind of error as well as an
229     /// arbitrary error payload.
230     ///
231     /// This function is used to generically create I/O errors which do not
232     /// originate from the OS itself. The `error` argument is an arbitrary
233     /// payload which will be contained in this `Error`.
234     ///
235     /// # Examples
236     ///
237     /// ```
238     /// use std::io::{Error, ErrorKind};
239     ///
240     /// // errors can be created from strings
241     /// let custom_error = Error::new(ErrorKind::Other, "oh no!");
242     ///
243     /// // errors can also be created from other errors
244     /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
245     /// ```
246     #[stable(feature = "rust1", since = "1.0.0")]
247     pub fn new<E>(kind: ErrorKind, error: E) -> Error
248     where
249         E: Into<Box<dyn error::Error + Send + Sync>>,
250     {
251         Self::_new(kind, error.into())
252     }
253
254     fn _new(kind: ErrorKind, error: Box<dyn error::Error + Send + Sync>) -> Error {
255         Error { repr: Repr::Custom(Box::new(Custom { kind, error })) }
256     }
257
258     /// Returns an error representing the last OS error which occurred.
259     ///
260     /// This function reads the value of `errno` for the target platform (e.g.
261     /// `GetLastError` on Windows) and will return a corresponding instance of
262     /// `Error` for the error code.
263     ///
264     /// # Examples
265     ///
266     /// ```
267     /// use std::io::Error;
268     ///
269     /// println!("last OS error: {:?}", Error::last_os_error());
270     /// ```
271     #[stable(feature = "rust1", since = "1.0.0")]
272     pub fn last_os_error() -> Error {
273         Error::from_raw_os_error(sys::os::errno() as i32)
274     }
275
276     /// Creates a new instance of an `Error` from a particular OS error code.
277     ///
278     /// # Examples
279     ///
280     /// On Linux:
281     ///
282     /// ```
283     /// # if cfg!(target_os = "linux") {
284     /// use std::io;
285     ///
286     /// let error = io::Error::from_raw_os_error(22);
287     /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
288     /// # }
289     /// ```
290     ///
291     /// On Windows:
292     ///
293     /// ```
294     /// # if cfg!(windows) {
295     /// use std::io;
296     ///
297     /// let error = io::Error::from_raw_os_error(10022);
298     /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
299     /// # }
300     /// ```
301     #[stable(feature = "rust1", since = "1.0.0")]
302     pub fn from_raw_os_error(code: i32) -> Error {
303         Error { repr: Repr::Os(code) }
304     }
305
306     /// Returns the OS error that this error represents (if any).
307     ///
308     /// If this `Error` was constructed via `last_os_error` or
309     /// `from_raw_os_error`, then this function will return `Some`, otherwise
310     /// it will return `None`.
311     ///
312     /// # Examples
313     ///
314     /// ```
315     /// use std::io::{Error, ErrorKind};
316     ///
317     /// fn print_os_error(err: &Error) {
318     ///     if let Some(raw_os_err) = err.raw_os_error() {
319     ///         println!("raw OS error: {:?}", raw_os_err);
320     ///     } else {
321     ///         println!("Not an OS error");
322     ///     }
323     /// }
324     ///
325     /// fn main() {
326     ///     // Will print "raw OS error: ...".
327     ///     print_os_error(&Error::last_os_error());
328     ///     // Will print "Not an OS error".
329     ///     print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
330     /// }
331     /// ```
332     #[stable(feature = "rust1", since = "1.0.0")]
333     pub fn raw_os_error(&self) -> Option<i32> {
334         match self.repr {
335             Repr::Os(i) => Some(i),
336             Repr::Custom(..) => None,
337             Repr::Simple(..) => None,
338         }
339     }
340
341     /// Returns a reference to the inner error wrapped by this error (if any).
342     ///
343     /// If this `Error` was constructed via `new` then this function will
344     /// return `Some`, otherwise it will return `None`.
345     ///
346     /// # Examples
347     ///
348     /// ```
349     /// use std::io::{Error, ErrorKind};
350     ///
351     /// fn print_error(err: &Error) {
352     ///     if let Some(inner_err) = err.get_ref() {
353     ///         println!("Inner error: {:?}", inner_err);
354     ///     } else {
355     ///         println!("No inner error");
356     ///     }
357     /// }
358     ///
359     /// fn main() {
360     ///     // Will print "No inner error".
361     ///     print_error(&Error::last_os_error());
362     ///     // Will print "Inner error: ...".
363     ///     print_error(&Error::new(ErrorKind::Other, "oh no!"));
364     /// }
365     /// ```
366     #[stable(feature = "io_error_inner", since = "1.3.0")]
367     pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
368         match self.repr {
369             Repr::Os(..) => None,
370             Repr::Simple(..) => None,
371             Repr::Custom(ref c) => Some(&*c.error),
372         }
373     }
374
375     /// Returns a mutable reference to the inner error wrapped by this error
376     /// (if any).
377     ///
378     /// If this `Error` was constructed via `new` then this function will
379     /// return `Some`, otherwise it will return `None`.
380     ///
381     /// # Examples
382     ///
383     /// ```
384     /// use std::io::{Error, ErrorKind};
385     /// use std::{error, fmt};
386     /// use std::fmt::Display;
387     ///
388     /// #[derive(Debug)]
389     /// struct MyError {
390     ///     v: String,
391     /// }
392     ///
393     /// impl MyError {
394     ///     fn new() -> MyError {
395     ///         MyError {
396     ///             v: "oh no!".to_string()
397     ///         }
398     ///     }
399     ///
400     ///     fn change_message(&mut self, new_message: &str) {
401     ///         self.v = new_message.to_string();
402     ///     }
403     /// }
404     ///
405     /// impl error::Error for MyError {
406     ///     fn description(&self) -> &str { &self.v }
407     /// }
408     ///
409     /// impl Display for MyError {
410     ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
411     ///         write!(f, "MyError: {}", &self.v)
412     ///     }
413     /// }
414     ///
415     /// fn change_error(mut err: Error) -> Error {
416     ///     if let Some(inner_err) = err.get_mut() {
417     ///         inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
418     ///     }
419     ///     err
420     /// }
421     ///
422     /// fn print_error(err: &Error) {
423     ///     if let Some(inner_err) = err.get_ref() {
424     ///         println!("Inner error: {}", inner_err);
425     ///     } else {
426     ///         println!("No inner error");
427     ///     }
428     /// }
429     ///
430     /// fn main() {
431     ///     // Will print "No inner error".
432     ///     print_error(&change_error(Error::last_os_error()));
433     ///     // Will print "Inner error: ...".
434     ///     print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
435     /// }
436     /// ```
437     #[stable(feature = "io_error_inner", since = "1.3.0")]
438     pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
439         match self.repr {
440             Repr::Os(..) => None,
441             Repr::Simple(..) => None,
442             Repr::Custom(ref mut c) => Some(&mut *c.error),
443         }
444     }
445
446     /// Consumes the `Error`, returning its inner error (if any).
447     ///
448     /// If this `Error` was constructed via `new` then this function will
449     /// return `Some`, otherwise it will return `None`.
450     ///
451     /// # Examples
452     ///
453     /// ```
454     /// use std::io::{Error, ErrorKind};
455     ///
456     /// fn print_error(err: Error) {
457     ///     if let Some(inner_err) = err.into_inner() {
458     ///         println!("Inner error: {}", inner_err);
459     ///     } else {
460     ///         println!("No inner error");
461     ///     }
462     /// }
463     ///
464     /// fn main() {
465     ///     // Will print "No inner error".
466     ///     print_error(Error::last_os_error());
467     ///     // Will print "Inner error: ...".
468     ///     print_error(Error::new(ErrorKind::Other, "oh no!"));
469     /// }
470     /// ```
471     #[stable(feature = "io_error_inner", since = "1.3.0")]
472     pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
473         match self.repr {
474             Repr::Os(..) => None,
475             Repr::Simple(..) => None,
476             Repr::Custom(c) => Some(c.error),
477         }
478     }
479
480     /// Returns the corresponding `ErrorKind` for this error.
481     ///
482     /// # Examples
483     ///
484     /// ```
485     /// use std::io::{Error, ErrorKind};
486     ///
487     /// fn print_error(err: Error) {
488     ///     println!("{:?}", err.kind());
489     /// }
490     ///
491     /// fn main() {
492     ///     // Will print "No inner error".
493     ///     print_error(Error::last_os_error());
494     ///     // Will print "Inner error: ...".
495     ///     print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
496     /// }
497     /// ```
498     #[stable(feature = "rust1", since = "1.0.0")]
499     pub fn kind(&self) -> ErrorKind {
500         match self.repr {
501             Repr::Os(code) => sys::decode_error_kind(code),
502             Repr::Custom(ref c) => c.kind,
503             Repr::Simple(kind) => kind,
504         }
505     }
506 }
507
508 impl fmt::Debug for Repr {
509     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
510         match *self {
511             Repr::Os(code) => fmt
512                 .debug_struct("Os")
513                 .field("code", &code)
514                 .field("kind", &sys::decode_error_kind(code))
515                 .field("message", &sys::os::error_string(code))
516                 .finish(),
517             Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
518             Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
519         }
520     }
521 }
522
523 #[stable(feature = "rust1", since = "1.0.0")]
524 impl fmt::Display for Error {
525     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
526         match self.repr {
527             Repr::Os(code) => {
528                 let detail = sys::os::error_string(code);
529                 write!(fmt, "{} (os error {})", detail, code)
530             }
531             Repr::Custom(ref c) => c.error.fmt(fmt),
532             Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
533         }
534     }
535 }
536
537 #[stable(feature = "rust1", since = "1.0.0")]
538 impl error::Error for Error {
539     fn description(&self) -> &str {
540         match self.repr {
541             Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(),
542             Repr::Custom(ref c) => c.error.description(),
543         }
544     }
545
546     #[allow(deprecated)]
547     fn cause(&self) -> Option<&dyn error::Error> {
548         match self.repr {
549             Repr::Os(..) => None,
550             Repr::Simple(..) => None,
551             Repr::Custom(ref c) => c.error.cause(),
552         }
553     }
554
555     fn source(&self) -> Option<&(dyn error::Error + 'static)> {
556         match self.repr {
557             Repr::Os(..) => None,
558             Repr::Simple(..) => None,
559             Repr::Custom(ref c) => c.error.source(),
560         }
561     }
562 }
563
564 fn _assert_error_is_sync_send() {
565     fn _is_sync_send<T: Sync + Send>() {}
566     _is_sync_send::<Error>();
567 }
568
569 #[cfg(test)]
570 mod test {
571     use super::{Custom, Error, ErrorKind, Repr};
572     use crate::error;
573     use crate::fmt;
574     use crate::sys::decode_error_kind;
575     use crate::sys::os::error_string;
576
577     #[test]
578     fn test_debug_error() {
579         let code = 6;
580         let msg = error_string(code);
581         let kind = decode_error_kind(code);
582         let err = Error {
583             repr: Repr::Custom(box Custom {
584                 kind: ErrorKind::InvalidInput,
585                 error: box Error { repr: super::Repr::Os(code) },
586             }),
587         };
588         let expected = format!(
589             "Custom {{ \
590              kind: InvalidInput, \
591              error: Os {{ \
592              code: {:?}, \
593              kind: {:?}, \
594              message: {:?} \
595              }} \
596              }}",
597             code, kind, msg
598         );
599         assert_eq!(format!("{:?}", err), expected);
600     }
601
602     #[test]
603     fn test_downcasting() {
604         #[derive(Debug)]
605         struct TestError;
606
607         impl fmt::Display for TestError {
608             fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
609                 Ok(())
610             }
611         }
612
613         impl error::Error for TestError {
614             fn description(&self) -> &str {
615                 "asdf"
616             }
617         }
618
619         // we have to call all of these UFCS style right now since method
620         // resolution won't implicitly drop the Send+Sync bounds
621         let mut err = Error::new(ErrorKind::Other, TestError);
622         assert!(err.get_ref().unwrap().is::<TestError>());
623         assert_eq!("asdf", err.get_ref().unwrap().description());
624         assert!(err.get_mut().unwrap().is::<TestError>());
625         let extracted = err.into_inner().unwrap();
626         extracted.downcast::<TestError>().unwrap();
627     }
628 }