cargo_metadata/errors.rs
1use std::fmt;
2use std::io;
3use std::str::Utf8Error;
4use std::string::FromUtf8Error;
5
6/// Custom result type for `cargo_metadata::Error`
7pub type Result<T> = ::std::result::Result<T, Error>;
8
9/// Error returned when executing/parsing `cargo metadata` fails.
10///
11/// # Note about Backtraces
12///
13/// This error type does not contain backtraces, but each error variant
14/// comes from _one_ specific place, so it's not really needed for the
15/// inside of this crate. If you need a backtrace down to, but not inside
16/// of, a failed call of `cargo_metadata` you can do one of multiple thinks:
17///
18/// 1. Convert it to a `failure::Error` (possible using the `?` operator),
19/// which is similar to a `Box<::std::error::Error + 'static + Send + Sync>`.
20/// 2. Have appropriate variants in your own error type. E.g. you could wrap
21/// a `failure::Context<Error>` or add a `failure::Backtrace` field (which
22/// is empty if `RUST_BACKTRACE` is not set, so it's simple to use).
23/// 3. You still can place a failure based error into a `error_chain` if you
24/// really want to. (Either through foreign_links or by making it a field
25/// value of a `ErrorKind` variant).
26///
27#[derive(Debug)]
28pub enum Error {
29 /// Error during execution of `cargo metadata`
30 CargoMetadata {
31 /// stderr returned by the `cargo metadata` command
32 stderr: String,
33 },
34
35 /// IO Error during execution of `cargo metadata`
36 Io(io::Error),
37
38 /// Output of `cargo metadata` was not valid utf8
39 Utf8(Utf8Error),
40
41 /// Error output of `cargo metadata` was not valid utf8
42 ErrUtf8(FromUtf8Error),
43
44 /// Deserialization error (structure of json did not match expected structure)
45 Json(::serde_json::Error),
46
47 /// The output did not contain any json
48 NoJson,
49}
50
51impl From<io::Error> for Error {
52 fn from(v: io::Error) -> Self {
53 Error::Io(v)
54 }
55}
56
57impl From<Utf8Error> for Error {
58 fn from(v: Utf8Error) -> Self {
59 Error::Utf8(v)
60 }
61}
62
63impl From<FromUtf8Error> for Error {
64 fn from(v: FromUtf8Error) -> Self {
65 Error::ErrUtf8(v)
66 }
67}
68
69impl From<::serde_json::Error> for Error {
70 fn from(v: ::serde_json::Error) -> Self {
71 Error::Json(v)
72 }
73}
74
75impl fmt::Display for Error {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 match self {
78 Error::CargoMetadata { stderr } => {
79 write!(
80 f,
81 "`cargo metadata` exited with an error: {}",
82 stderr.trim_end()
83 )
84 }
85 Error::Io(err) => write!(f, "failed to start `cargo metadata`: {}", err),
86 Error::Utf8(err) => write!(f, "cannot convert the stdout of `cargo metadata`: {}", err),
87 Error::ErrUtf8(err) => {
88 write!(f, "cannot convert the stderr of `cargo metadata`: {}", err)
89 }
90 Error::Json(err) => write!(f, "failed to interpret `cargo metadata`'s json: {}", err),
91 Error::NoJson => write!(
92 f,
93 "could not find any json in the output of `cargo metadata`"
94 ),
95 }
96 }
97}
98
99impl ::std::error::Error for Error {
100 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
101 match self {
102 Error::CargoMetadata { .. } => None,
103 Error::Io(err) => Some(err),
104 Error::Utf8(err) => Some(err),
105 Error::ErrUtf8(err) => Some(err),
106 Error::Json(err) => Some(err),
107 Error::NoJson => None,
108 }
109 }
110}