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