1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use {
    cm_fidl_validator::error::ErrorList,
    cm_types::ParseError,
    fidl_fuchsia_component_decl as fdecl,
    std::path::Path,
    std::str::Utf8Error,
    std::{error, fmt, io},
};

/// The location in the file where an error was detected.
#[derive(PartialEq, Clone, Debug)]
pub struct Location {
    /// One-based line number of the error.
    pub line: usize,

    /// One-based column number of the error.
    pub column: usize,
}

/// Enum type that can represent any error encountered by a cml operation.
#[derive(Debug)]
pub enum Error {
    DuplicateRights(String),
    InvalidArgs(String),
    Io(io::Error),
    FidlEncoding(fidl::Error),
    MissingRights(String),
    Parse {
        err: String,
        location: Option<Location>,
        filename: Option<String>,
    },
    Validate {
        err: String,
        filename: Option<String>,
    },
    FidlValidator {
        errs: ErrorList,
    },
    Internal(String),
    Utf8(Utf8Error),
    /// A restricted feature was used without opting-in.
    RestrictedFeature(String),
}

impl error::Error for Error {}

impl Error {
    pub fn invalid_args(err: impl Into<String>) -> Self {
        Self::InvalidArgs(err.into())
    }

    pub fn parse(
        err: impl fmt::Display,
        location: Option<Location>,
        filename: Option<&Path>,
    ) -> Self {
        Self::Parse {
            err: err.to_string(),
            location,
            filename: filename.map(|f| f.to_string_lossy().into_owned()),
        }
    }

    pub fn validate(err: impl fmt::Display) -> Self {
        Self::Validate { err: err.to_string(), filename: None }
    }

    pub fn fidl_validator(errs: ErrorList) -> Self {
        Self::FidlValidator { errs }
    }

    pub fn duplicate_rights(err: impl Into<String>) -> Self {
        Self::DuplicateRights(err.into())
    }

    pub fn missing_rights(err: impl Into<String>) -> Self {
        Self::MissingRights(err.into())
    }

    pub fn internal(err: impl Into<String>) -> Self {
        Self::Internal(err.into())
    }

    pub fn json5(err: json5format::Error, file: &Path) -> Self {
        match err {
            json5format::Error::Configuration(errstr) => Error::Internal(errstr),
            json5format::Error::Parse(location, errstr) => match location {
                Some(location) => Error::parse(
                    errstr,
                    Some(Location { line: location.line, column: location.col }),
                    Some(file),
                ),
                None => Error::parse(errstr, None, Some(file)),
            },
            json5format::Error::Internal(location, errstr) => match location {
                Some(location) => Error::Internal(format!("{}: {}", location, errstr)),
                None => Error::Internal(errstr),
            },
            json5format::Error::TestFailure(location, errstr) => match location {
                Some(location) => {
                    Error::Internal(format!("{}: Test failure: {}", location, errstr))
                }
                None => Error::Internal(format!("Test failure: {}", errstr)),
            },
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self {
            Error::DuplicateRights(err) => write!(f, "Duplicate rights: {}", err),
            Error::InvalidArgs(err) => write!(f, "Invalid args: {}", err),
            Error::Io(err) => write!(f, "IO error: {}", err),
            Error::FidlEncoding(err) => write!(f, "Fidl encoding error: {}", err),
            Error::MissingRights(err) => write!(f, "Missing rights: {}", err),
            Error::Parse { err, location, filename } => {
                let mut prefix = String::new();
                if let Some(filename) = filename {
                    prefix.push_str(&format!("{}:", filename));
                }
                if let Some(location) = location {
                    // Check for a syntax error generated by pest. These error messages have
                    // the line and column number embedded in them, so we don't want to
                    // duplicate that.
                    //
                    // TODO: If serde_json5 had an error type for json5 syntax errors, we wouldn't
                    // need to parse the string like this.
                    if !err.starts_with(" -->") {
                        prefix.push_str(&format!("{}:{}:", location.line, location.column));
                    }
                }
                if !prefix.is_empty() {
                    write!(f, "Error at {} {}", prefix, err)
                } else {
                    write!(f, "{}", err)
                }
            }
            Error::Validate {  err, filename } => {
                let mut prefix = String::new();
                if let Some(filename) = filename {
                    prefix.push_str(&format!("{}:", filename));
                }
                if !prefix.is_empty() {
                    write!(f, "Error at {} {}", prefix, err)
                } else {
                    write!(f, "{}", err)
                }
            }
            Error::FidlValidator{ errs} => format_multiple_fidl_validator_errors(errs, f),
            Error::Internal(err) => write!(f, "Internal error: {}", err),
            Error::Utf8(err) => write!(f, "UTF8 error: {}", err),
            Error::RestrictedFeature(feature) => write!(
                f,
                "Use of restricted feature \"{}\". To opt-in, see https://fuchsia.dev/go/components/restricted-features",
                feature
            ),
        }
    }
}

fn format_multiple_fidl_validator_errors(e: &ErrorList, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    // Some errors are caught by `cm_fidl_validator` but not caught by `cml` validation.
    //
    // Our strategy is:
    //
    // - If a `cm_fidl_validator` error can be easily transformed back to be relevant in the context
    //   of `cml`, do that. For example, we should transform the FIDL declaration names
    //   to corresponding cml names.
    //
    // - Otherwise, we consider that a bug and we should add corresponding validation in `cml`.
    //   As such, we will surface this kind of errors as `Internal` as an indication.
    //   That is represented by the `_` match arm here.
    //
    use cm_fidl_validator::error::Error as CmFidlError;
    let mut found_internal_errors = false;
    for e in e.errs.iter() {
        match e {
            CmFidlError::DifferentAvailabilityInAggregation(availability_list) => {
                // Format the availability in `cml` syntax.
                let comma_separated = availability_list
                    .0
                    .iter()
                    .map(|s| match s {
                        fdecl::Availability::Required => "\"required\"".to_string(),
                        fdecl::Availability::Optional => "\"optional\"".to_string(),
                        fdecl::Availability::SameAsTarget => "\"same_as_target\"".to_string(),
                        fdecl::Availability::Transitional => "\"transitional\"".to_string(),
                    })
                    .collect::<Vec<_>>()
                    .join(", ");

                write!(f, "All sources that feed into an aggregation operation should have the same availability. ")?;
                write!(f, "Got [ {comma_separated} ].")?;
            }
            _ => {
                write!(f, "Internal error: {}\n", e)?;
                found_internal_errors = true;
            }
        }
    }

    if found_internal_errors {
        write!(
            f,
            "This reflects error(s) in the `.cml` file. \
Unfortunately, for some of them, cmc cannot provide more details at this time.
Please file a bug at https://bugs.fuchsia.dev/p/fuchsia/issues/entry?template=ComponentFramework \
with the cml in question, so we can work on better error reporting."
        )?;
    }

    Ok(())
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Error::Io(err)
    }
}

impl From<Utf8Error> for Error {
    fn from(err: Utf8Error) -> Self {
        Error::Utf8(err)
    }
}

impl From<serde_json::error::Error> for Error {
    fn from(err: serde_json::error::Error) -> Self {
        use serde_json::error::Category;
        match err.classify() {
            Category::Io | Category::Eof => Error::Io(err.into()),
            Category::Syntax => {
                let line = err.line();
                let column = err.column();
                Error::parse(err, Some(Location { line, column }), None)
            }
            Category::Data => Error::validate(err),
        }
    }
}

impl From<fidl::Error> for Error {
    fn from(err: fidl::Error) -> Self {
        Error::FidlEncoding(err)
    }
}

impl From<ParseError> for Error {
    fn from(err: ParseError) -> Self {
        match err {
            ParseError::InvalidValue => Self::internal("invalid value"),
            ParseError::InvalidComponentUrl { details } => {
                Self::internal(&format!("invalid component url: {details}"))
            }
            ParseError::TooLong => Self::internal("too long"),
            ParseError::Empty => Self::internal("empty"),
            ParseError::InvalidSegment => Self::internal("invalid path segment"),
            ParseError::NoLeadingSlash => Self::internal("missing leading slash"),
        }
    }
}

impl TryFrom<serde_json5::Error> for Location {
    type Error = &'static str;
    fn try_from(e: serde_json5::Error) -> Result<Self, Self::Error> {
        match e {
            serde_json5::Error::Message { location: Some(l), .. } => {
                Ok(Location { line: l.line, column: l.column })
            }
            _ => Err("location unavailable"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::format_err;
    use assert_matches::assert_matches;
    use cm_types as cm;

    #[test]
    fn test_syntax_error_message() {
        let result = serde_json::from_str::<cm::Name>("foo").map_err(Error::from);
        assert_matches!(result, Err(Error::Parse { .. }));
    }

    #[test]
    fn test_validation_error_message() {
        let result = serde_json::from_str::<cm::Name>("\"foo$\"").map_err(Error::from);
        assert_matches!(result, Err(Error::Validate { .. }));
    }

    #[test]
    fn test_parse_error() {
        let result = Error::parse(format_err!("oops"), None, None);
        assert_eq!(format!("{}", result), "oops");

        let result = Error::parse(format_err!("oops"), Some(Location { line: 2, column: 3 }), None);
        assert_eq!(format!("{}", result), "Error at 2:3: oops");

        let result = Error::parse(
            format_err!("oops"),
            Some(Location { line: 2, column: 3 }),
            Some(&Path::new("test.cml")),
        );
        assert_eq!(format!("{}", result), "Error at test.cml:2:3: oops");

        let result = Error::parse(
            format_err!(" --> pest error"),
            Some(Location { line: 42, column: 42 }),
            Some(&Path::new("test.cml")),
        );
        assert_eq!(format!("{}", result), "Error at test.cml:  --> pest error");
    }

    #[test]
    fn test_validation_error() {
        let mut result = Error::validate(format_err!("oops"));
        assert_eq!(format!("{}", result), "oops");

        if let Error::Validate { filename, .. } = &mut result {
            *filename = Some("test.cml".to_string());
        }
        assert_eq!(format!("{}", result), "Error at test.cml: oops");
    }

    #[test]
    fn test_format_multiple_fidl_validator_errors() {
        use cm_fidl_validator::error::AvailabilityList;
        use cm_fidl_validator::error::Error as CmFidlError;

        let error = Error::FidlValidator {
            errs: ErrorList {
                errs: vec![CmFidlError::DifferentAvailabilityInAggregation(AvailabilityList(
                    vec![fdecl::Availability::Required, fdecl::Availability::Optional],
                ))],
            },
        };
        assert_eq!(
            format!("{error}"),
            "All sources that feed into an aggregation operation should \
            have the same availability. Got [ \"required\", \"optional\" ]."
        );
    }
}