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(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    /// An optional value was marked absent but its size was non-zero
81    #[error("optional value is absent but has a non-zero size; expected 0, found {0}")]
82    InvalidOptionalSize(u64),
83
84    /// A vector had a length greater than its allowed limit
85    #[error(
86        "vector has a length greater than the allowed limit; expected no more than {limit} \
87        elements, found {size} elements"
88    )]
89    VectorTooLong {
90        /// The actual size of the vector
91        size: u64,
92        /// The maximum allowed size of the vector
93        limit: u64,
94    },
95
96    /// A string contained non-UTF8 data
97    #[error("string has non-UTF8 content; {0}")]
98    InvalidUtf8(#[from] core::str::Utf8Error),
99
100    /// A union was marked absent, but its envelope was not set to zero
101    #[error("union is absent but has a non-zero envelope")]
102    InvalidUnionEnvelope,
103
104    /// A framework error contained an unrecognized error code.
105    #[error("framework error has an unrecognized error code")]
106    InvalidFrameworkError(i32),
107
108    /// The decoder ran out of data before decoding finished
109    #[error("reached the end of the buffer before decoding finished")]
110    InsufficientData,
111
112    /// The decoder ran out of handles before decoding finished
113    #[error("consumed all handles before decoding finished")]
114    InsufficientHandles,
115
116    /// Attempted to decode a driver handle with an decoder that does not support them.
117    #[error("cannot decode driver handles with this decoder")]
118    DriverHandlesUnsupported,
119
120    /// The decoder ran out of driver handles before decoding finished.
121    #[error("consumed all driver handles before decoding finished")]
122    InsufficientDriverHandles,
123
124    /// Decoding finished without consuming all of the bytes
125    #[error(
126        "finished decoding before all bytes were consumed; completed with {num_extra} bytes left \
127        over"
128    )]
129    ExtraBytes {
130        /// The number of bytes left over after decoding finished
131        num_extra: usize,
132    },
133
134    /// Decoding finished without consuming all of the handles
135    #[error(
136        "finished decoding before all handles were consumed; completed with {num_extra} handles \
137        left over"
138    )]
139    ExtraHandles {
140        /// The number of handles left over after decoding finished
141        num_extra: usize,
142    },
143}