opentalk_client_shared/
api_error.rs

1// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
2// SPDX-FileCopyrightText: Kitware, Inc
3//
4// SPDX-License-Identifier: EUPL-1.2
5
6use std::error::Error;
7
8use bytes::Bytes;
9use snafu::prelude::*;
10
11/// Errors which may occur when using API endpoints.
12#[derive(Debug, Snafu)]
13#[snafu(visibility(pub))]
14#[non_exhaustive]
15pub enum ApiError<E>
16where
17    E: Error + Send + Sync + 'static,
18{
19    /// The client encountered an error.
20    #[snafu(display("client error: {}", source))]
21    Client {
22        /// The source of the error.
23        source: E,
24    },
25
26    /// The URL failed to parse.
27    #[snafu(context(false), display("failed to parse url: {}", source))]
28    UrlParse {
29        /// The source of the error.
30        source: url::ParseError,
31    },
32
33    /// The URI failed to parse.
34    #[snafu(context(false), display("failed to parse uri: {}", source))]
35    UriParse {
36        /// The source of the error.
37        source: http::uri::InvalidUri,
38    },
39
40    /// JSON deserialization from OpenTalk failed.
41    #[snafu(context(false), display("could not parse JSON response: {}", source))]
42    Json {
43        /// The source of the error.
44        source: serde_json::Error,
45    },
46
47    /// OpenTalk returned an error message.
48    #[snafu(display("opentalk server error: {}", msg))]
49    OpenTalk {
50        /// The error message from OpenTalk.
51        msg: String,
52    },
53
54    /// OpenTalk returned an error without JSON information.
55    #[snafu(display("opentalk internal server error {}", status))]
56    OpenTalkService {
57        /// The status code for the return.
58        status: http::StatusCode,
59        /// The error data from OpenTalk.
60        data: Bytes,
61    },
62
63    /// Failed to parse an expected data type from JSON.
64    #[snafu(display("could not parse {} data from JSON: {}", typename, source))]
65    DataType {
66        /// The source of the error.
67        source: serde_json::Error,
68        /// The name of the type that could not be deserialized.
69        typename: &'static str,
70    },
71
72    /// Error from the http-request-derive crate.
73    #[snafu(
74        context(false),
75        display("error in http-request-derive crate: {}", source)
76    )]
77    HttpRequestDerive {
78        /// The source of the error.
79        source: http_request_derive::Error,
80    },
81
82    /// Couldn't build a HTTP request, probably a bug.
83    #[snafu(context(false), display("could not build HTTP request: {}", source))]
84    Request {
85        /// The source of the error.
86        source: http::Error,
87    },
88
89    /// Trying to perform an unauthorized request.
90    #[snafu(display("trying to perfom an unauthorized request"))]
91    Unauthorized,
92
93    /// Custom error
94    #[snafu(whatever)]
95    Custom {
96        /// The custom error message
97        message: String,
98
99        /// The source of the error.
100        #[snafu(source(from(Box<dyn Error + Send + Sync>, Some)))]
101        source: Option<Box<dyn Error + Send + Sync>>,
102    },
103}