Skip to main content

fuchsia_inspect/writer/
error.rs

1// Copyright 2020 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 inspect_format::{BlockIndex, BlockType, Error as FormatError};
6
7/// Errors that Inspect API functions can return.
8#[derive(Clone, Debug, PartialEq, thiserror::Error)]
9pub enum Error {
10    #[error("FIDL error: {0}")]
11    Fidl(String),
12
13    #[error("Failed to allocate vmo")]
14    #[cfg(target_os = "fuchsia")]
15    AllocateVmo(#[source] zx::Status),
16
17    #[error("Failed to get vmo size")]
18    #[cfg(target_os = "fuchsia")]
19    GetVmoSize(#[source] zx::Status),
20
21    #[error("VMO is not frozen")]
22    #[cfg(target_os = "fuchsia")]
23    VmoNotFrozen,
24
25    #[error("Failed to duplicate vmo")]
26    #[cfg(target_os = "fuchsia")]
27    DuplicateVmoFailed(#[source] zx::Status),
28
29    #[error("Failed to get frozen vmo; transaction_count={0}")]
30    #[cfg(target_os = "fuchsia")]
31    ConcurrentTransaction(usize),
32
33    #[error("Failed to create child vmo")]
34    #[cfg(target_os = "fuchsia")]
35    CreateChildVmo(#[source] zx::Status),
36
37    #[error("Expected a backing VMO for this operation, but found none")]
38    VmoMissing,
39
40    #[error("Failed to free {value_type} index={index}")]
41    Free {
42        value_type: &'static str,
43        index: BlockIndex,
44        #[source]
45        error: Box<Error>,
46    },
47
48    #[error("Failed to create {value_type}")]
49    Create {
50        value_type: &'static str,
51        #[source]
52        error: Box<Error>,
53    },
54
55    #[error("{0} is no-op")]
56    NoOp(&'static str),
57
58    #[error("Failed to create the internal heap")]
59    CreateHeap(#[source] Box<Error>),
60
61    #[error("Failed to create the internal state")]
62    CreateState(#[source] Box<Error>),
63
64    #[error("Attempted to free a FREE block at index {0}")]
65    BlockAlreadyFree(BlockIndex),
66
67    #[error("Invalid index {0}: {1}")]
68    InvalidIndex(BlockIndex, &'static str),
69
70    #[error("Heap already at its maximum size")]
71    HeapMaxSizeReached,
72
73    #[error("Cannot allocate block of size {0}. Exceeds maximum.")]
74    BlockSizeTooBig(usize),
75
76    #[error("Invalid block type at index {0}: {1:?}")]
77    InvalidBlockType(BlockIndex, u8),
78
79    #[error("Invalid block type at index {0}: {1}")]
80    InvalidBlockTypeNumber(BlockIndex, u8),
81
82    #[error("Invalid block type. Expected: {0}, actual: {1}")]
83    UnexpectedBlockType(BlockType, BlockType),
84
85    #[error("Invalid block type. Expected: {0}, got: {1}")]
86    UnexpectedBlockTypeRepr(&'static str, BlockType),
87
88    #[error("Expected lock state locked={0}")]
89    ExpectedLockState(bool),
90
91    #[error("Invalid order {0}")]
92    InvalidBlockOrder(usize),
93
94    #[error("Invalid order {0} at index {1}")]
95    InvalidBlockOrderAtIndex(u8, BlockIndex),
96
97    #[error("Cannot swap blocks of different order or container")]
98    InvalidBlockSwap,
99
100    #[error("Expected a valid entry type for the array at index {0}")]
101    InvalidArrayType(BlockIndex),
102
103    #[error("Invalid array index: {0}")]
104    InvalidArrayIndex(usize),
105
106    #[error("{slots} exceeds the maximum number of slots for order {order}: {max_capacity}")]
107    ArrayCapacityExceeded { slots: usize, order: u8, max_capacity: usize },
108
109    #[error("Invalid {value_type} flags={flags} at index {index}")]
110    InvalidFlags { value_type: &'static str, flags: u8, index: BlockIndex },
111
112    #[error("Name is not utf8")]
113    NameNotUtf8,
114
115    #[error("Failed to convert array slots to usize")]
116    FailedToConvertArraySlotsToUsize,
117
118    #[error("Format error")]
119    VmoFormat(#[source] FormatError),
120
121    #[error("Cannot adopt into different VMO")]
122    AdoptionIntoWrongVmo,
123
124    #[error("Cannot adopt ancestor")]
125    AdoptAncestor,
126}
127
128impl From<FormatError> for Error {
129    fn from(error: FormatError) -> Self {
130        Self::VmoFormat(error)
131    }
132}
133
134impl Error {
135    pub fn fidl(err: anyhow::Error) -> Self {
136        Self::Fidl(format!("{err}"))
137    }
138
139    pub fn free(value_type: &'static str, index: BlockIndex, error: Error) -> Self {
140        Self::Free { value_type, index, error: Box::new(error) }
141    }
142
143    pub fn create(value_type: &'static str, error: Error) -> Self {
144        Self::Create { value_type, error: Box::new(error) }
145    }
146
147    pub fn invalid_index(index: BlockIndex, reason: &'static str) -> Self {
148        Self::InvalidIndex(index, reason)
149    }
150
151    pub fn invalid_flags(value_type: &'static str, flags: u8, index: BlockIndex) -> Self {
152        Self::InvalidFlags { value_type, flags, index }
153    }
154
155    pub fn array_capacity_exceeded(slots: usize, order: u8, max_capacity: usize) -> Self {
156        Self::ArrayCapacityExceeded { slots, order, max_capacity }
157    }
158}