fluence_app_service/
errors.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use marine_wasm_backend_traits::WasmBackendError;
18use marine::MarineError;
19
20use std::io::Error as IOError;
21use std::error::Error;
22use std::path::PathBuf;
23
24#[derive(Debug)]
25pub enum AppServiceError {
26    /// An error related to config parsing.
27    InvalidConfig(String),
28
29    /// Various errors related to file i/o.
30    IOError(IOError),
31
32    /// Marine errors.
33    MarineError(MarineError),
34
35    // Wasm backend errors
36    WasmBackendError(WasmBackendError),
37
38    /// Directory creation failed
39    CreateDir {
40        err: IOError,
41        path: PathBuf,
42    },
43
44    /// Errors related to malformed config.
45    ConfigParseError(String),
46}
47
48impl Error for AppServiceError {}
49
50impl std::fmt::Display for AppServiceError {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
52        match self {
53            AppServiceError::InvalidConfig(err_msg) => write!(f, "{}", err_msg),
54            AppServiceError::IOError(err) => write!(f, "{}", err),
55            AppServiceError::MarineError(err) => write!(f, "{}", err),
56            AppServiceError::CreateDir { err, path } => {
57                write!(f, "Failed to create dir {:?}: {:?}", path, err)
58            }
59            AppServiceError::ConfigParseError(err_msg) => write!(f, "{}", err_msg),
60            AppServiceError::WasmBackendError(err) => {
61                write!(f, "{}", err)
62            }
63        }
64    }
65}
66
67impl From<MarineError> for AppServiceError {
68    fn from(err: MarineError) -> Self {
69        AppServiceError::MarineError(err)
70    }
71}
72
73impl From<IOError> for AppServiceError {
74    fn from(err: IOError) -> Self {
75        AppServiceError::IOError(err)
76    }
77}
78
79impl From<toml::de::Error> for AppServiceError {
80    fn from(err: toml::de::Error) -> Self {
81        AppServiceError::InvalidConfig(format!("{}", err))
82    }
83}
84
85impl From<std::convert::Infallible> for AppServiceError {
86    fn from(_: std::convert::Infallible) -> Self {
87        unreachable!()
88    }
89}