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