1use base64::DecodeError;
6use thiserror::Error;
7use wasm_bindgen::prelude::*;
8use workflow_wasm::jserror::*;
9
10#[derive(Error, Debug)]
11pub enum Error {
12 #[error("{0}")]
13 Custom(String),
14
15 #[error("I/O error: {0}")]
16 IoError(#[from] std::io::Error),
17
18 #[error("Chrome error: {0}")]
19 ChromeError(#[from] workflow_chrome::error::Error),
20
21 #[error("{0}")]
24 JsValue(JsErrorData),
25
26 #[error("Base64 decode error: {0}")]
27 DecodeError(DecodeError),
28
29 #[error("Not found: {0}")]
30 NotFound(String),
31
32 #[error("Not a text data: {0}")]
33 DataIsNotAString(String),
34
35 #[error("Not a buffer data: {0}")]
36 DataIsNotABuffer(String),
37
38 #[error(transparent)]
39 SerdeJson(#[from] serde_json::Error),
40
41 #[error("invalid path: {0}")]
42 InvalidPath(String),
43
44 #[error("Unable to obtain user home directory")]
45 HomeDir(String),
46
47 #[error("No file metadata")]
48 Metadata,
49
50 #[error(transparent)]
51 FasterHex(#[from] faster_hex::Error),
52
53 #[error("This operation is not supported")]
54 NotSupported,
55}
56
57impl From<Error> for JsValue {
58 fn from(err: Error) -> Self {
59 match err {
60 Error::JsValue(js_err) => js_err.into(),
61 _ => err.into(),
62 }
63 }
64}
65
66impl From<JsValue> for Error {
76 fn from(error: JsValue) -> Error {
77 Error::JsValue(error.into())
78 }
79}
80
81impl From<JsErrorData> for Error {
82 fn from(error: JsErrorData) -> Error {
83 Error::JsValue(error)
84 }
85}
86
87impl From<DecodeError> for Error {
88 fn from(error: DecodeError) -> Error {
89 Error::DecodeError(error)
90 }
91}
92
93impl From<String> for Error {
94 fn from(error: String) -> Error {
95 Error::Custom(error)
96 }
97}
98
99impl Error {
100 pub fn code(&self) -> Option<&str> {
101 match self {
102 Error::JsValue(js_err) => js_err.code().as_deref(),
103 _ => None,
104 }
105 }
106}