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.
45use crate::block_index::BlockIndex;
6use crate::block_type::BlockType;
78/// 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}")]
12ArrayCapacityExceeded { slots: usize, order: u8, max_capacity: usize },
1314#[error("Array index out of bounds: {0}")]
15ArrayIndexOutOfBounds(usize),
1617#[error("Expected lock state locked={0}")]
18ExpectedLockState(bool),
1920#[error("Invalid order {0}")]
21InvalidBlockOrder(u8),
2223#[error("Cannot swap blocks of different order or container")]
24InvalidBlockSwap,
2526#[error("Invalid block type at index {0}: {1}")]
27InvalidBlockTypeNumber(BlockIndex, u8),
2829#[error("Invalid {value_type} flags={flags} at index {index}")]
30InvalidFlags { value_type: &'static str, flags: u8, index: BlockIndex },
3132#[error("Failed to convert array slots to usize")]
33FailedToConvertArraySlotsToUsize,
3435#[error("Name is not utf8")]
36NameNotUtf8,
3738#[error("Expected a valid entry type for the array at index {0}")]
39InvalidArrayType(BlockIndex),
4041#[error("Invalid block type. Expected: {0}, actual: {1}")]
42UnexpectedBlockType(BlockType, BlockType),
4344#[error("Invalid block type. Expected: {0}, got: {1}")]
45UnexpectedBlockTypeRepr(String, BlockType),
4647#[error("Invalid reference count. Reference count must be in range (0, 2^32)")]
48InvalidReferenceCount,
4950#[error("invalid buffer format: {0}")]
51InvalidBufferFormat(u8),
5253#[error("Size (={0}) of the inspect VMO could not be written to the header")]
54SizeNotWritten(u32),
5556#[error("Attempted to read a slice at an invalid offset: {0}")]
57InvalidOffset(usize),
58}
5960impl Error {
61pub fn array_capacity_exceeded(slots: usize, order: u8, max_capacity: usize) -> Self {
62Self::ArrayCapacityExceeded { slots, order, max_capacity }
63 }
6465pub fn invalid_flags(value_type: &'static str, flags: u8, index: BlockIndex) -> Self {
66Self::InvalidFlags { value_type, flags, index }
67 }
68}