rust_bert/common/
error.rs

1#[cfg(feature = "onnx")]
2use ndarray::ShapeError;
3#[cfg(feature = "onnx")]
4use ort::OrtError;
5use rust_tokenizers::error::TokenizerError;
6use tch::TchError;
7use thiserror::Error;
8
9#[derive(Error, Debug)]
10pub enum RustBertError {
11    #[cfg(feature = "remote")]
12    #[error("Endpoint not available error: {0}")]
13    FileDownloadError(#[from] cached_path::Error),
14
15    #[error("IO error: {0}")]
16    IOError(String),
17
18    #[error("Tch tensor error: {0}")]
19    TchError(String),
20
21    #[error("Tokenizer error: {0}")]
22    TokenizerError(String),
23
24    #[error("Invalid configuration error: {0}")]
25    InvalidConfigurationError(String),
26
27    #[error("Value error: {0}")]
28    ValueError(String),
29
30    #[error("Value error: {0}")]
31    #[cfg(feature = "onnx")]
32    OrtError(String),
33
34    #[error("Value error: {0}")]
35    #[cfg(feature = "onnx")]
36    NdArrayError(String),
37
38    #[error("Unsupported operation")]
39    UnsupportedError,
40}
41
42impl From<std::io::Error> for RustBertError {
43    fn from(error: std::io::Error) -> Self {
44        RustBertError::IOError(error.to_string())
45    }
46}
47
48impl From<TokenizerError> for RustBertError {
49    fn from(error: TokenizerError) -> Self {
50        RustBertError::TokenizerError(error.to_string())
51    }
52}
53
54impl From<TchError> for RustBertError {
55    fn from(error: TchError) -> Self {
56        RustBertError::TchError(error.to_string())
57    }
58}
59
60#[cfg(feature = "onnx")]
61impl From<OrtError> for RustBertError {
62    fn from(error: OrtError) -> Self {
63        RustBertError::OrtError(error.to_string())
64    }
65}
66#[cfg(feature = "onnx")]
67impl From<ShapeError> for RustBertError {
68    fn from(error: ShapeError) -> Self {
69        RustBertError::NdArrayError(error.to_string())
70    }
71}