inspect_format/
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 crate::block_index::BlockIndex;
6use crate::block_type::BlockType;
7
8/// Errors that Inspect API functions can return.
9#[derive(Clone, Debug, thiserror::Error, PartialEq, Eq)]
10pub enum Error {
11    #[error("{slots} exceeds the maximum number of slots for order {order}: {max_capacity}")]
12    ArrayCapacityExceeded { slots: usize, order: u8, max_capacity: usize },
13
14    #[error("Array index out of bounds: {0}")]
15    ArrayIndexOutOfBounds(usize),
16
17    #[error("Expected lock state locked={0}")]
18    ExpectedLockState(bool),
19
20    #[error("Invalid order {0}")]
21    InvalidBlockOrder(u8),
22
23    #[error("Cannot swap blocks of different order or container")]
24    InvalidBlockSwap,
25
26    #[error("Invalid block type at index {0}: {1}")]
27    InvalidBlockTypeNumber(BlockIndex, u8),
28
29    #[error("Invalid {value_type} flags={flags} at index {index}")]
30    InvalidFlags { value_type: &'static str, flags: u8, index: BlockIndex },
31
32    #[error("Failed to convert array slots to usize")]
33    FailedToConvertArraySlotsToUsize,
34
35    #[error("Name is not utf8")]
36    NameNotUtf8,
37
38    #[error("Expected a valid entry type for the array at index {0}")]
39    InvalidArrayType(BlockIndex),
40
41    #[error("Invalid block type. Expected: {0}, actual: {1}")]
42    UnexpectedBlockType(BlockType, BlockType),
43
44    #[error("Invalid block type. Expected: {0}, got: {1}")]
45    UnexpectedBlockTypeRepr(String, BlockType),
46
47    #[error("Invalid reference count. Reference count must be in range (0, 2^32)")]
48    InvalidReferenceCount,
49
50    #[error("invalid buffer format: {0}")]
51    InvalidBufferFormat(u8),
52
53    #[error("Size (={0}) of the inspect VMO could not be written to the header")]
54    SizeNotWritten(u32),
55
56    #[error("Attempted to read a slice at an invalid offset: {0}")]
57    InvalidOffset(usize),
58}
59
60impl Error {
61    pub fn array_capacity_exceeded(slots: usize, order: u8, max_capacity: usize) -> Self {
62        Self::ArrayCapacityExceeded { slots, order, max_capacity }
63    }
64
65    pub fn invalid_flags(value_type: &'static str, flags: u8, index: BlockIndex) -> Self {
66        Self::InvalidFlags { value_type, flags, index }
67    }
68}