1use std::any::Any;
16
17use common_macro::stack_trace_debug;
18use common_telemetry::common_error::ext::{ErrorExt, RetryHint};
19use common_telemetry::common_error::status_code::StatusCode;
20use snafu::{Location, Snafu};
21
22#[derive(Snafu)]
23#[snafu(visibility(pub))]
24#[stack_trace_debug]
25pub enum Error {
26 #[snafu(display("Failed to init backend"))]
27 InitBackend {
28 #[snafu(source)]
29 error: opendal::Error,
30 #[snafu(implicit)]
31 location: Location,
32 },
33
34 #[snafu(display("Failed to build http client"))]
35 BuildHttpClient {
36 #[snafu(implicit)]
37 location: Location,
38 #[snafu(source)]
39 error: reqwest::Error,
40 },
41
42 #[snafu(display("Invalid entry `{}` in query.remote_allowed_hosts: {}", entry, reason))]
43 InvalidAllowedHost {
44 entry: String,
45 reason: String,
46 #[snafu(implicit)]
47 location: Location,
48 },
49
50 #[snafu(display("Failed to create directory {}", dir))]
51 CreateDir {
52 dir: String,
53 #[snafu(source)]
54 error: std::io::Error,
55 },
56
57 #[snafu(display("Failed to remove directory {}", dir))]
58 RemoveDir {
59 dir: String,
60 #[snafu(source)]
61 error: std::io::Error,
62 },
63}
64
65pub type Result<T> = std::result::Result<T, Error>;
66
67pub fn retry_hint_from_opendal_error(error: &opendal::Error) -> RetryHint {
69 if error.is_temporary() {
70 RetryHint::Retryable
71 } else {
72 RetryHint::NonRetryable
73 }
74}
75
76impl ErrorExt for Error {
77 fn status_code(&self) -> StatusCode {
78 use Error::*;
79 match self {
80 InitBackend { .. } => StatusCode::StorageUnavailable,
81 BuildHttpClient { .. } => StatusCode::Unexpected,
82 InvalidAllowedHost { .. } => StatusCode::InvalidArguments,
83 CreateDir { .. } | RemoveDir { .. } => StatusCode::Internal,
84 }
85 }
86
87 fn as_any(&self) -> &dyn Any {
88 self
89 }
90
91 fn retry_hint(&self) -> RetryHint {
92 match self {
93 Error::InitBackend { error, .. } => retry_hint_from_opendal_error(error),
94 _ => RetryHint::NonRetryable,
95 }
96 }
97}