1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use bincode::ErrorKind as BincodeErrorKind;

use rocksdb::Error as RocksError;
use serde::{Deserialize, Serialize};
use std::{fmt, fmt::Display};
use thiserror::Error;

#[non_exhaustive]
#[derive(Error, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
pub enum TypedStoreError {
    #[error("rocksdb error: {0}")]
    RocksDBError(String),
    #[error("(de)serialization error: {0}")]
    SerializationError(String),
    #[error("the column family {0} was not registered with the database")]
    UnregisteredColumn(String),
    #[error("a batch operation can't operate across databases")]
    CrossDBBatch,
}

#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash, Debug, Error)]
pub(crate) struct RocksErrorDef {
    message: String,
}

impl From<RocksError> for RocksErrorDef {
    fn from(err: RocksError) -> Self {
        RocksErrorDef {
            message: err.as_ref().to_string(),
        }
    }
}

impl From<RocksError> for TypedStoreError {
    fn from(err: RocksError) -> Self {
        TypedStoreError::RocksDBError(format!("{err}"))
    }
}

impl Display for RocksErrorDef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        self.message.fmt(formatter)
    }
}

#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug, Error)]
pub(crate) enum BincodeErrorDef {
    Io(String),
    InvalidUtf8Encoding(String),
    InvalidBoolEncoding(u8),
    InvalidCharEncoding,
    InvalidTagEncoding(usize),
    DeserializeAnyNotSupported,
    SizeLimit,
    SequenceMustHaveLength,
    Custom(String),
}

impl fmt::Display for BincodeErrorDef {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            BincodeErrorDef::Io(ref ioerr) => write!(fmt, "io error: {}", ioerr),
            BincodeErrorDef::InvalidUtf8Encoding(ref e) => {
                write!(fmt, "{}", e)
            }
            BincodeErrorDef::InvalidBoolEncoding(b) => {
                write!(fmt, "expected 0 or 1, found {}", b)
            }
            BincodeErrorDef::InvalidCharEncoding => write!(fmt, "{:?}", self),
            BincodeErrorDef::InvalidTagEncoding(tag) => {
                write!(fmt, "found {}", tag)
            }
            BincodeErrorDef::SequenceMustHaveLength => write!(fmt, "{:?}", self),
            BincodeErrorDef::SizeLimit => write!(fmt, "{:?}", self),
            BincodeErrorDef::DeserializeAnyNotSupported => write!(
                fmt,
                "Bincode does not support the serde::Deserializer::deserialize_any method"
            ),
            BincodeErrorDef::Custom(ref s) => s.fmt(fmt),
        }
    }
}

impl From<bincode::Error> for BincodeErrorDef {
    fn from(err: bincode::Error) -> Self {
        match err.as_ref() {
            BincodeErrorKind::Io(ioerr) => BincodeErrorDef::Io(ioerr.to_string()),
            BincodeErrorKind::InvalidUtf8Encoding(utf8err) => {
                BincodeErrorDef::InvalidUtf8Encoding(utf8err.to_string())
            }
            BincodeErrorKind::InvalidBoolEncoding(byte) => {
                BincodeErrorDef::InvalidBoolEncoding(*byte)
            }
            BincodeErrorKind::InvalidCharEncoding => BincodeErrorDef::InvalidCharEncoding,
            BincodeErrorKind::InvalidTagEncoding(tag) => BincodeErrorDef::InvalidTagEncoding(*tag),
            BincodeErrorKind::DeserializeAnyNotSupported => {
                BincodeErrorDef::DeserializeAnyNotSupported
            }
            BincodeErrorKind::SizeLimit => BincodeErrorDef::SizeLimit,
            BincodeErrorKind::SequenceMustHaveLength => BincodeErrorDef::SequenceMustHaveLength,
            BincodeErrorKind::Custom(str) => BincodeErrorDef::Custom(str.to_owned()),
        }
    }
}

impl From<bincode::Error> for TypedStoreError {
    fn from(err: bincode::Error) -> Self {
        TypedStoreError::SerializationError(format!("{err}"))
    }
}