fidl_next_codec/decode/
error.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use thiserror::Error;
6
7/// Errors that can be produced when decoding FIDL messages.
8#[derive(Clone, Error, Debug)]
9pub enum DecodeError {
10    /// A required handle was absent
11    #[error("required handle is absent")]
12    RequiredHandleAbsent,
13
14    /// A required value was absent
15    #[error("required value is absent")]
16    RequiredValueAbsent,
17
18    /// A boolean was set to a value other than 0 or 1
19    #[error("`bool` field has an invalid value; expected 0 or 1, found {0}")]
20    InvalidBool(u8),
21
22    /// A handle was set to a value other than 0 or u32::MAX
23    #[error("handle has an invalid presence marker; expected 0 or u32::MAX, found {0}")]
24    InvalidHandlePresence(u32),
25
26    /// A pointer was set to a value other than 0 or u64::MAX
27    #[error("pointer has an invalid presence marker; expected 0 or u64::MAX, found {0}.")]
28    InvalidPointerPresence(u64),
29
30    /// An envelope had an invalid size
31    #[error("invalid envelope size; expected a multiple of 8, found {0}")]
32    InvalidEnvelopeSize(u32),
33
34    /// An enum had an invalid ordinal
35    #[error("invalid enum ordinal; expected a valid ordinal, found {0}")]
36    InvalidEnumOrdinal(i128),
37
38    /// A union had an invalid ordinal
39    #[error("invalid union ordinal; expected a valid ordinal, found {0}")]
40    InvalidUnionOrdinal(usize),
41
42    /// A bit set had an invalid set of bits
43    #[error("invalid bits; expected a subset of {expected:b}, found {actual:b}")]
44    InvalidBits {
45        /// The expected set of bits
46        expected: usize,
47        /// The actual set of bits
48        actual: usize,
49    },
50
51    /// An envelope was out-of-line, but the out-of-line data was too small
52    #[error(
53        "envelope has out-of-line data which is too small; expected more than 4 bytes out-of-line, \
54        found {0} bytes"
55    )]
56    OutOfLineValueTooSmall(u32),
57
58    /// An envelope had inline data that was too big
59    #[error(
60        "envelope has inline data which is too big; expected 4 bytes or fewer, found {0} bytes"
61    )]
62    InlineValueTooBig(usize),
63
64    /// An envelope should always be inline, but it contained out-of-line data
65    #[error("envelope should always be inline, but it contained {0} out-of-line bytes")]
66    ExpectedInline(usize),
67
68    /// An envelope consumed a different number of handles than it indicated in its metadata
69    #[error(
70        "envelope consumed a different number of handles than it claimed that it would; expected \
71        {expected} to be consumed, found {actual} were consumed"
72    )]
73    IncorrectNumberOfHandlesConsumed {
74        /// The number of handles the envelope was expected to consume
75        expected: usize,
76        /// The number of handles actually consumed by the envelope
77        actual: usize,
78    },
79
80    /// Envelope flags other than the inline bit were set.
81    #[error("invalid envelope flags: {0:#b}")]
82    InvalidEnvelopeFlags(u16),
83
84    /// An optional value was marked absent but its size was non-zero
85    #[error("optional value is absent but has a non-zero size; expected 0, found {0}")]
86    InvalidOptionalSize(u64),
87
88    /// A vector had a length greater than its allowed limit
89    #[error(
90        "vector has a length greater than the allowed limit; expected no more than {limit} \
91        elements, found {size} elements"
92    )]
93    VectorTooLong {
94        /// The actual size of the vector
95        size: u64,
96        /// The maximum allowed size of the vector
97        limit: u64,
98    },
99
100    /// A string contained non-UTF8 data
101    #[error("string has non-UTF8 content; {0}")]
102    InvalidUtf8(#[from] core::str::Utf8Error),
103
104    /// A union was marked absent, but its envelope was not set to zero
105    #[error("union is absent but has a non-zero envelope")]
106    InvalidUnionEnvelope,
107
108    /// A framework error contained an unrecognized error code.
109    #[error("framework error has an unrecognized error code")]
110    InvalidFrameworkError(i32),
111
112    /// The decoder ran out of data before decoding finished
113    #[error("reached the end of the buffer before decoding finished")]
114    InsufficientData,
115
116    /// The decoder ran out of handles before decoding finished
117    #[error("consumed all handles before decoding finished")]
118    InsufficientHandles,
119
120    /// Attempted to decode a driver handle with an decoder that does not support them.
121    #[error("cannot decode driver handles with this decoder")]
122    DriverHandlesUnsupported,
123
124    /// The next available handle was a zircon handle but expected a driver handle
125    #[error("expected next handle to be a driver handle")]
126    ExpectedDriverHandle,
127
128    /// The next available handle was a driver handle but expected a zircon handle
129    #[error("expected next handle to be a zircon handle")]
130    ExpectedZirconHandle,
131
132    /// Decoding finished without consuming all of the bytes
133    #[error(
134        "finished decoding before all bytes were consumed; completed with {num_extra} bytes left \
135        over"
136    )]
137    ExtraBytes {
138        /// The number of bytes left over after decoding finished
139        num_extra: usize,
140    },
141
142    /// Decoding finished without consuming all of the handles
143    #[error(
144        "finished decoding before all handles were consumed; completed with {num_extra} handles \
145        left over"
146    )]
147    ExtraHandles {
148        /// The number of handles left over after decoding finished
149        num_extra: usize,
150    },
151
152    /// Invalid empty struct
153    #[error("invalid empty struct")]
154    InvalidEmptyStruct,
155
156    /// Invalid padding
157    #[error("invalid padding")]
158    InvalidPadding,
159
160    /// Validation Failed
161    #[error("validation failed")]
162    Validation(#[from] crate::ValidationError),
163}