Skip to main content

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