]> git.lizzy.rs Git - rust.git/blob - src/libstd/io/error.rs
Rollup merge of #59262 - timvermeulen:iterator_cmp_dedup, r=scottmcm
[rust.git] / src / libstd / io / error.rs
1 use crate::error;
2 use crate::fmt;
3 use crate::result;
4 use crate::sys;
5 use crate::convert::From;
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 {
224             repr: Repr::Simple(kind)
225         }
226     }
227 }
228
229 impl Error {
230     /// Creates a new I/O error from a known kind of error as well as an
231     /// arbitrary error payload.
232     ///
233     /// This function is used to generically create I/O errors which do not
234     /// originate from the OS itself. The `error` argument is an arbitrary
235     /// payload which will be contained in this `Error`.
236     ///
237     /// # Examples
238     ///
239     /// ```
240     /// use std::io::{Error, ErrorKind};
241     ///
242     /// // errors can be created from strings
243     /// let custom_error = Error::new(ErrorKind::Other, "oh no!");
244     ///
245     /// // errors can also be created from other errors
246     /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
247     /// ```
248     #[stable(feature = "rust1", since = "1.0.0")]
249     pub fn new<E>(kind: ErrorKind, error: E) -> Error
250         where E: Into<Box<dyn error::Error+Send+Sync>>
251     {
252         Self::_new(kind, error.into())
253     }
254
255     fn _new(kind: ErrorKind, error: Box<dyn error::Error+Send+Sync>) -> Error {
256         Error {
257             repr: Repr::Custom(Box::new(Custom {
258                 kind,
259                 error,
260             }))
261         }
262     }
263
264     /// Returns an error representing the last OS error which occurred.
265     ///
266     /// This function reads the value of `errno` for the target platform (e.g.
267     /// `GetLastError` on Windows) and will return a corresponding instance of
268     /// `Error` for the error code.
269     ///
270     /// # Examples
271     ///
272     /// ```
273     /// use std::io::Error;
274     ///
275     /// println!("last OS error: {:?}", Error::last_os_error());
276     /// ```
277     #[stable(feature = "rust1", since = "1.0.0")]
278     pub fn last_os_error() -> Error {
279         Error::from_raw_os_error(sys::os::errno() as i32)
280     }
281
282     /// Creates a new instance of an `Error` from a particular OS error code.
283     ///
284     /// # Examples
285     ///
286     /// On Linux:
287     ///
288     /// ```
289     /// # if cfg!(target_os = "linux") {
290     /// use std::io;
291     ///
292     /// let error = io::Error::from_raw_os_error(22);
293     /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
294     /// # }
295     /// ```
296     ///
297     /// On Windows:
298     ///
299     /// ```
300     /// # if cfg!(windows) {
301     /// use std::io;
302     ///
303     /// let error = io::Error::from_raw_os_error(10022);
304     /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
305     /// # }
306     /// ```
307     #[stable(feature = "rust1", since = "1.0.0")]
308     pub fn from_raw_os_error(code: i32) -> Error {
309         Error { repr: Repr::Os(code) }
310     }
311
312     /// Returns the OS error that this error represents (if any).
313     ///
314     /// If this `Error` was constructed via `last_os_error` or
315     /// `from_raw_os_error`, then this function will return `Some`, otherwise
316     /// it will return `None`.
317     ///
318     /// # Examples
319     ///
320     /// ```
321     /// use std::io::{Error, ErrorKind};
322     ///
323     /// fn print_os_error(err: &Error) {
324     ///     if let Some(raw_os_err) = err.raw_os_error() {
325     ///         println!("raw OS error: {:?}", raw_os_err);
326     ///     } else {
327     ///         println!("Not an OS error");
328     ///     }
329     /// }
330     ///
331     /// fn main() {
332     ///     // Will print "raw OS error: ...".
333     ///     print_os_error(&Error::last_os_error());
334     ///     // Will print "Not an OS error".
335     ///     print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
336     /// }
337     /// ```
338     #[stable(feature = "rust1", since = "1.0.0")]
339     pub fn raw_os_error(&self) -> Option<i32> {
340         match self.repr {
341             Repr::Os(i) => Some(i),
342             Repr::Custom(..) => None,
343             Repr::Simple(..) => None,
344         }
345     }
346
347     /// Returns a reference to the inner error wrapped by this error (if any).
348     ///
349     /// If this `Error` was constructed via `new` then this function will
350     /// return `Some`, otherwise it will return `None`.
351     ///
352     /// # Examples
353     ///
354     /// ```
355     /// use std::io::{Error, ErrorKind};
356     ///
357     /// fn print_error(err: &Error) {
358     ///     if let Some(inner_err) = err.get_ref() {
359     ///         println!("Inner error: {:?}", inner_err);
360     ///     } else {
361     ///         println!("No inner error");
362     ///     }
363     /// }
364     ///
365     /// fn main() {
366     ///     // Will print "No inner error".
367     ///     print_error(&Error::last_os_error());
368     ///     // Will print "Inner error: ...".
369     ///     print_error(&Error::new(ErrorKind::Other, "oh no!"));
370     /// }
371     /// ```
372     #[stable(feature = "io_error_inner", since = "1.3.0")]
373     pub fn get_ref(&self) -> Option<&(dyn error::Error+Send+Sync+'static)> {
374         match self.repr {
375             Repr::Os(..) => None,
376             Repr::Simple(..) => None,
377             Repr::Custom(ref c) => Some(&*c.error),
378         }
379     }
380
381     /// Returns a mutable reference to the inner error wrapped by this error
382     /// (if any).
383     ///
384     /// If this `Error` was constructed via `new` then this function will
385     /// return `Some`, otherwise it will return `None`.
386     ///
387     /// # Examples
388     ///
389     /// ```
390     /// use std::io::{Error, ErrorKind};
391     /// use std::{error, fmt};
392     /// use std::fmt::Display;
393     ///
394     /// #[derive(Debug)]
395     /// struct MyError {
396     ///     v: String,
397     /// }
398     ///
399     /// impl MyError {
400     ///     fn new() -> MyError {
401     ///         MyError {
402     ///             v: "oh no!".to_string()
403     ///         }
404     ///     }
405     ///
406     ///     fn change_message(&mut self, new_message: &str) {
407     ///         self.v = new_message.to_string();
408     ///     }
409     /// }
410     ///
411     /// impl error::Error for MyError {
412     ///     fn description(&self) -> &str { &self.v }
413     /// }
414     ///
415     /// impl Display for MyError {
416     ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
417     ///         write!(f, "MyError: {}", &self.v)
418     ///     }
419     /// }
420     ///
421     /// fn change_error(mut err: Error) -> Error {
422     ///     if let Some(inner_err) = err.get_mut() {
423     ///         inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
424     ///     }
425     ///     err
426     /// }
427     ///
428     /// fn print_error(err: &Error) {
429     ///     if let Some(inner_err) = err.get_ref() {
430     ///         println!("Inner error: {}", inner_err);
431     ///     } else {
432     ///         println!("No inner error");
433     ///     }
434     /// }
435     ///
436     /// fn main() {
437     ///     // Will print "No inner error".
438     ///     print_error(&change_error(Error::last_os_error()));
439     ///     // Will print "Inner error: ...".
440     ///     print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
441     /// }
442     /// ```
443     #[stable(feature = "io_error_inner", since = "1.3.0")]
444     pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error+Send+Sync+'static)> {
445         match self.repr {
446             Repr::Os(..) => None,
447             Repr::Simple(..) => None,
448             Repr::Custom(ref mut c) => Some(&mut *c.error),
449         }
450     }
451
452     /// Consumes the `Error`, returning its inner error (if any).
453     ///
454     /// If this `Error` was constructed via `new` then this function will
455     /// return `Some`, otherwise it will return `None`.
456     ///
457     /// # Examples
458     ///
459     /// ```
460     /// use std::io::{Error, ErrorKind};
461     ///
462     /// fn print_error(err: Error) {
463     ///     if let Some(inner_err) = err.into_inner() {
464     ///         println!("Inner error: {}", inner_err);
465     ///     } else {
466     ///         println!("No inner error");
467     ///     }
468     /// }
469     ///
470     /// fn main() {
471     ///     // Will print "No inner error".
472     ///     print_error(Error::last_os_error());
473     ///     // Will print "Inner error: ...".
474     ///     print_error(Error::new(ErrorKind::Other, "oh no!"));
475     /// }
476     /// ```
477     #[stable(feature = "io_error_inner", since = "1.3.0")]
478     pub fn into_inner(self) -> Option<Box<dyn error::Error+Send+Sync>> {
479         match self.repr {
480             Repr::Os(..) => None,
481             Repr::Simple(..) => None,
482             Repr::Custom(c) => Some(c.error)
483         }
484     }
485
486     /// Returns the corresponding `ErrorKind` for this error.
487     ///
488     /// # Examples
489     ///
490     /// ```
491     /// use std::io::{Error, ErrorKind};
492     ///
493     /// fn print_error(err: Error) {
494     ///     println!("{:?}", err.kind());
495     /// }
496     ///
497     /// fn main() {
498     ///     // Will print "No inner error".
499     ///     print_error(Error::last_os_error());
500     ///     // Will print "Inner error: ...".
501     ///     print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
502     /// }
503     /// ```
504     #[stable(feature = "rust1", since = "1.0.0")]
505     pub fn kind(&self) -> ErrorKind {
506         match self.repr {
507             Repr::Os(code) => sys::decode_error_kind(code),
508             Repr::Custom(ref c) => c.kind,
509             Repr::Simple(kind) => kind,
510         }
511     }
512 }
513
514 impl fmt::Debug for Repr {
515     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
516         match *self {
517             Repr::Os(code) =>
518                 fmt.debug_struct("Os")
519                     .field("code", &code)
520                     .field("kind", &sys::decode_error_kind(code))
521                     .field("message", &sys::os::error_string(code)).finish(),
522             Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
523             Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
524         }
525     }
526 }
527
528 #[stable(feature = "rust1", since = "1.0.0")]
529 impl fmt::Display for Error {
530     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
531         match self.repr {
532             Repr::Os(code) => {
533                 let detail = sys::os::error_string(code);
534                 write!(fmt, "{} (os error {})", detail, code)
535             }
536             Repr::Custom(ref c) => c.error.fmt(fmt),
537             Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
538         }
539     }
540 }
541
542 #[stable(feature = "rust1", since = "1.0.0")]
543 impl error::Error for Error {
544     fn description(&self) -> &str {
545         match self.repr {
546             Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(),
547             Repr::Custom(ref c) => c.error.description(),
548         }
549     }
550
551     #[allow(deprecated)]
552     fn cause(&self) -> Option<&dyn error::Error> {
553         match self.repr {
554             Repr::Os(..) => None,
555             Repr::Simple(..) => None,
556             Repr::Custom(ref c) => c.error.cause(),
557         }
558     }
559
560     fn source(&self) -> Option<&(dyn error::Error + 'static)> {
561         match self.repr {
562             Repr::Os(..) => None,
563             Repr::Simple(..) => None,
564             Repr::Custom(ref c) => c.error.source(),
565         }
566     }
567 }
568
569 fn _assert_error_is_sync_send() {
570     fn _is_sync_send<T: Sync+Send>() {}
571     _is_sync_send::<Error>();
572 }
573
574 #[cfg(test)]
575 mod test {
576     use super::{Error, ErrorKind, Repr, Custom};
577     use crate::error;
578     use crate::fmt;
579     use crate::sys::os::error_string;
580     use crate::sys::decode_error_kind;
581
582     #[test]
583     fn test_debug_error() {
584         let code = 6;
585         let msg = error_string(code);
586         let kind = decode_error_kind(code);
587         let err = Error {
588             repr: Repr::Custom(box Custom {
589                 kind: ErrorKind::InvalidInput,
590                 error: box Error {
591                     repr: super::Repr::Os(code)
592                 },
593             })
594         };
595         let expected = format!(
596             "Custom {{ \
597                 kind: InvalidInput, \
598                 error: Os {{ \
599                     code: {:?}, \
600                     kind: {:?}, \
601                     message: {:?} \
602                 }} \
603             }}",
604             code, kind, msg
605         );
606         assert_eq!(format!("{:?}", err), expected);
607     }
608
609     #[test]
610     fn test_downcasting() {
611         #[derive(Debug)]
612         struct TestError;
613
614         impl fmt::Display for TestError {
615             fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
616                 Ok(())
617             }
618         }
619
620         impl error::Error for TestError {
621             fn description(&self) -> &str {
622                 "asdf"
623             }
624         }
625
626         // we have to call all of these UFCS style right now since method
627         // resolution won't implicitly drop the Send+Sync bounds
628         let mut err = Error::new(ErrorKind::Other, TestError);
629         assert!(err.get_ref().unwrap().is::<TestError>());
630         assert_eq!("asdf", err.get_ref().unwrap().description());
631         assert!(err.get_mut().unwrap().is::<TestError>());
632         let extracted = err.into_inner().unwrap();
633         extracted.downcast::<TestError>().unwrap();
634     }
635 }