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