typed_store/rocks/
errors.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{fmt, fmt::Display};
6
7use bincode::ErrorKind as BincodeErrorKind;
8use rocksdb::Error as RocksError;
9use serde::{Deserialize, Serialize};
10use thiserror::Error;
11use typed_store_error::TypedStoreError;
12
13#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash, Debug, Error)]
14pub(crate) struct RocksErrorDef {
15    message: String,
16}
17
18impl Display for RocksErrorDef {
19    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
20        self.message.fmt(formatter)
21    }
22}
23
24#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug, Error)]
25pub(crate) enum BincodeErrorDef {
26    Io(String),
27    InvalidUtf8Encoding(String),
28    InvalidBoolEncoding(u8),
29    InvalidCharEncoding,
30    InvalidTagEncoding(usize),
31    DeserializeAnyNotSupported,
32    SizeLimit,
33    SequenceMustHaveLength,
34    Custom(String),
35}
36
37impl fmt::Display for BincodeErrorDef {
38    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match *self {
40            BincodeErrorDef::Io(ref ioerr) => write!(fmt, "io error: {ioerr}"),
41            BincodeErrorDef::InvalidUtf8Encoding(ref e) => {
42                write!(fmt, "{e}")
43            }
44            BincodeErrorDef::InvalidBoolEncoding(b) => {
45                write!(fmt, "expected 0 or 1, found {b}")
46            }
47            BincodeErrorDef::InvalidCharEncoding => write!(fmt, "{self:?}"),
48            BincodeErrorDef::InvalidTagEncoding(tag) => {
49                write!(fmt, "found {tag}")
50            }
51            BincodeErrorDef::SequenceMustHaveLength => write!(fmt, "{self:?}"),
52            BincodeErrorDef::SizeLimit => write!(fmt, "{self:?}"),
53            BincodeErrorDef::DeserializeAnyNotSupported => write!(
54                fmt,
55                "Bincode does not support the serde::Deserializer::deserialize_any method"
56            ),
57            BincodeErrorDef::Custom(ref s) => s.fmt(fmt),
58        }
59    }
60}
61
62impl From<bincode::Error> for BincodeErrorDef {
63    fn from(err: bincode::Error) -> Self {
64        match err.as_ref() {
65            BincodeErrorKind::Io(ioerr) => BincodeErrorDef::Io(ioerr.to_string()),
66            BincodeErrorKind::InvalidUtf8Encoding(utf8err) => {
67                BincodeErrorDef::InvalidUtf8Encoding(utf8err.to_string())
68            }
69            BincodeErrorKind::InvalidBoolEncoding(byte) => {
70                BincodeErrorDef::InvalidBoolEncoding(*byte)
71            }
72            BincodeErrorKind::InvalidCharEncoding => BincodeErrorDef::InvalidCharEncoding,
73            BincodeErrorKind::InvalidTagEncoding(tag) => BincodeErrorDef::InvalidTagEncoding(*tag),
74            BincodeErrorKind::DeserializeAnyNotSupported => {
75                BincodeErrorDef::DeserializeAnyNotSupported
76            }
77            BincodeErrorKind::SizeLimit => BincodeErrorDef::SizeLimit,
78            BincodeErrorKind::SequenceMustHaveLength => BincodeErrorDef::SequenceMustHaveLength,
79            BincodeErrorKind::Custom(str) => BincodeErrorDef::Custom(str.to_owned()),
80        }
81    }
82}
83
84pub fn typed_store_err_from_bincode_err(err: bincode::Error) -> TypedStoreError {
85    TypedStoreError::Serialization(format!("{err}"))
86}
87
88pub fn typed_store_err_from_bcs_err(err: bcs::Error) -> TypedStoreError {
89    TypedStoreError::Serialization(format!("{err}"))
90}
91
92pub fn typed_store_err_from_rocks_err(err: RocksError) -> TypedStoreError {
93    TypedStoreError::RocksDB(format!("{err}"))
94}