-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathapi_error.rs
More file actions
96 lines (87 loc) · 2.71 KB
/
api_error.rs
File metadata and controls
96 lines (87 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::fmt;
#[derive(Debug, thiserror::Error)]
pub struct ApiError {
inner: ApiErrorKind,
#[source]
source: Option<anyhow::Error>,
}
/// Represents API errors.
#[derive(Copy, Clone, Eq, PartialEq, Debug, thiserror::Error)]
pub(in crate::api) enum ApiErrorKind {
#[error("could not serialize value as JSON")]
CannotSerializeAsJson,
#[error("could not serialize envelope")]
CannotSerializeEnvelope,
#[error("could not parse JSON response")]
BadJson,
#[error("not a JSON response")]
NotJson,
#[error("request failed because API URL was incorrectly formatted")]
BadApiUrl,
#[error("organization not found")]
OrganizationNotFound,
#[error("resource not found")]
ResourceNotFound,
#[error("Project not found. Ensure that you configured the correct project and organization.")]
ProjectNotFound,
#[error("Release not found. Ensure that you configured the correct release, project, and organization.")]
ReleaseNotFound,
#[error("API request failed")]
RequestFailed,
#[error("could not compress data")]
CompressionFailed,
#[error("region overrides cannot be applied to absolute urls")]
InvalidRegionRequest,
#[error(
"Auth token is required for this request. Please run `sentry-cli login` and try again!"
)]
AuthMissing,
#[error(
"DSN missing. Please set the `SENTRY_DSN` environment variable to your project's DSN."
)]
DsnMissing,
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
impl ApiError {
pub(in crate::api) fn with_source<E>(kind: ApiErrorKind, source: E) -> ApiError
where
E: Into<anyhow::Error>,
{
ApiError {
inner: kind,
source: Some(source.into()),
}
}
// This method is currently only used in the macOS binary, there is no reason
// why not to expose it on other platforms, if we ever need it.
#[cfg(target_os = "macos")]
pub(in crate::api) fn kind(&self) -> ApiErrorKind {
self.inner
}
fn set_source<E: Into<anyhow::Error>>(mut self, source: E) -> ApiError {
self.source = Some(source.into());
self
}
}
impl From<ApiErrorKind> for ApiError {
fn from(kind: ApiErrorKind) -> ApiError {
ApiError {
inner: kind,
source: None,
}
}
}
impl From<curl::Error> for ApiError {
fn from(err: curl::Error) -> ApiError {
ApiError::from(ApiErrorKind::RequestFailed).set_source(err)
}
}
impl From<curl::FormError> for ApiError {
fn from(err: curl::FormError) -> ApiError {
ApiError::from(ApiErrorKind::RequestFailed).set_source(err)
}
}