diagnostics_message/
error.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4use crate::{MAX_TAG_LEN, MAX_TAGS, MIN_PACKET_SIZE};
5use diagnostics_log_encoding::parse::ParseError;
6use moniker::MonikerError;
7use thiserror::Error;
8
9use std::str::Utf8Error;
10
11#[derive(Debug, Error)]
12pub enum MessageError {
13    #[error("unrecognized value type encountered")]
14    UnrecognizedValue,
15    #[error("wrong value type encountered, expected integer, found {found} {value}")]
16    ExpectedInteger { value: String, found: &'static str },
17    #[error("couldn't parse message: {parse_error:?}")]
18    ParseError {
19        #[from]
20        parse_error: ParseError,
21    },
22    #[error("incorrect or corrupt metadata indicates to read past end of buffer")]
23    OutOfBounds,
24    #[error("provided tag at position {index} with length {len} (max {})", MAX_TAG_LEN)]
25    TagTooLong { index: usize, len: usize },
26    #[error("provided {} or more tags", MAX_TAGS + 1)]
27    TooManyTags,
28    #[error("message incorrectly terminated, found {terminator}, expected 0")]
29    NotNullTerminated { terminator: u8 },
30    #[error("buffer too small ({len}) to contain a valid log message (min {})", MIN_PACKET_SIZE)]
31    ShortRead { len: usize },
32    #[error("No URL found in extended record")]
33    MissingUrl,
34    #[error("No moniker found in extended record")]
35    MissingMoniker,
36    #[error("couldn't parse moniker: {parse_error:?}")]
37    MonikerParseError {
38        #[from]
39        parse_error: MonikerError,
40    },
41    #[error("string is not valid utf8: {source:?}")]
42    Utf8Error {
43        #[from]
44        source: Utf8Error,
45    },
46}
47
48impl PartialEq for MessageError {
49    fn eq(&self, other: &Self) -> bool {
50        use MessageError::*;
51        match (self, other) {
52            (ShortRead { len }, ShortRead { len: len2 }) => len == len2,
53            (NotNullTerminated { terminator }, NotNullTerminated { terminator: t2 }) => {
54                terminator == t2
55            }
56            (TooManyTags, TooManyTags) => true,
57            (TagTooLong { index, len }, TagTooLong { index: i2, len: l2 }) => {
58                index == i2 && len == l2
59            }
60            (OutOfBounds, OutOfBounds) => true,
61            _ => false,
62        }
63    }
64}