conjure_runtime/
errors.rs

1// Copyright 2020 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! Error types.
15
16use conjure_error::SerializableError;
17use http::StatusCode;
18use std::error::Error;
19use std::fmt;
20use std::time::Duration;
21
22/// An error received from a remote service.
23#[derive(Debug)]
24pub struct RemoteError {
25    pub(crate) status: StatusCode,
26    pub(crate) error: Option<SerializableError>,
27}
28
29impl fmt::Display for RemoteError {
30    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self.error() {
32            Some(error) => write!(
33                fmt,
34                "remote error: {} ({}) with instance ID {}",
35                error.error_code(),
36                error.error_name(),
37                error.error_instance_id()
38            ),
39            None => write!(fmt, "remote error: {}", self.status),
40        }
41    }
42}
43
44impl Error for RemoteError {}
45
46impl RemoteError {
47    /// Returns the status code of the response.
48    pub fn status(&self) -> &StatusCode {
49        &self.status
50    }
51
52    /// Returns the serialized Conjure error information in the response, if available.
53    pub fn error(&self) -> Option<&SerializableError> {
54        self.error.as_ref()
55    }
56}
57
58/// An 429 error received from a remote service.
59#[derive(Debug)]
60pub struct ThrottledError {
61    pub(crate) retry_after: Option<Duration>,
62}
63
64impl fmt::Display for ThrottledError {
65    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
66        fmt.write_str("got a 429 from the remote service")
67    }
68}
69
70impl Error for ThrottledError {}
71
72/// A 503 error received from a remote service.
73#[derive(Debug)]
74pub struct UnavailableError(pub(crate) ());
75
76impl fmt::Display for UnavailableError {
77    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
78        fmt.write_str("got a 503 from the remote service")
79    }
80}
81
82impl Error for UnavailableError {}