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_TAGS, MAX_TAG_LEN, MIN_PACKET_SIZE};
5use diagnostics_log_encoding::parse::ParseError;
6use moniker::MonikerError;
7use thiserror::Error;
8
9#[derive(Debug, Clone, Error)]
10pub enum MessageError {
11    #[error("unrecognized value type encountered")]
12    UnrecognizedValue,
13    #[error("wrong value type encountered, expected integer, found {found} {value}")]
14    ExpectedInteger { value: String, found: &'static str },
15    #[error("couldn't parse message: {parse_error:?}")]
16    ParseError {
17        #[from]
18        parse_error: ParseError,
19    },
20    #[error("incorrect or corrupt metadata indicates to read past end of buffer")]
21    OutOfBounds,
22    #[error("provided tag at position {index} with length {len} (max {})", MAX_TAG_LEN)]
23    TagTooLong { index: usize, len: usize },
24    #[error("provided {} or more tags", MAX_TAGS + 1)]
25    TooManyTags,
26    #[error("message incorrectly terminated, found {terminator}, expected 0")]
27    NotNullTerminated { terminator: u8 },
28    #[error("buffer too small ({len}) to contain a valid log message (min {})", MIN_PACKET_SIZE)]
29    ShortRead { len: usize },
30    #[error("No URL found in extended record")]
31    MissingUrl,
32    #[error("No moniker found in extended record")]
33    MissingMoniker,
34    #[error("couldn't parse moniker: {parse_error:?}")]
35    MonikerParseError {
36        #[from]
37        parse_error: MonikerError,
38    },
39}
40
41impl PartialEq for MessageError {
42    fn eq(&self, other: &Self) -> bool {
43        use MessageError::*;
44        match (self, other) {
45            (ShortRead { len }, ShortRead { len: len2 }) => len == len2,
46            (NotNullTerminated { terminator }, NotNullTerminated { terminator: t2 }) => {
47                terminator == t2
48            }
49            (TooManyTags, TooManyTags) => true,
50            (TagTooLong { index, len }, TagTooLong { index: i2, len: l2 }) => {
51                index == i2 && len == l2
52            }
53            (OutOfBounds, OutOfBounds) => true,
54            _ => false,
55        }
56    }
57}