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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Turning every failure into the specification's `problem` object.
//!
//! `[Def §Problem]`: on 40x and 500 responses a VTN answers with a `problem` object carrying details
//! of the error. Handlers therefore never build responses by hand — they return [`ApiError`], and
//! the single [`IntoResponse`] impl below decides the status, the body, and what gets logged.
//!
//! That covers everything a *handler* produces. The errors a middleware produces — a body over the
//! limit, a request that timed out, a method the router does not have — never reach an `ApiError`,
//! and are rewritten into the same shape by `layer_problem` in
//! [`api`](super::api), outside the layers whose bare answers it has to catch (D-108).
use axum::{
Json,
http::{HeaderMap, HeaderValue, StatusCode, header},
response::{IntoResponse, Response},
};
use crate::model::Problem;
use crate::schema::PayloadViolation;
use super::auth::{AuthError, Scope};
use super::store::StorageError;
/// Everything that can go wrong while serving a request.
#[derive(Debug, thiserror::Error)]
pub enum ApiError {
/// The request body or parameters were malformed.
#[error("{0}")]
BadRequest(String),
/// The body was labelled with a media type this endpoint does not accept.
///
/// Separate from [`ApiError::BadRequest`] because the two are different mistakes with
/// different fixes: a `415` says "your bytes were never going to be read", a `400` says "they
/// were read and they were wrong". Answering `400` to a form-encoded body sends the reader
/// looking for a typo in JSON they did not send.
#[error("{0}")]
UnsupportedMediaType(String),
/// The body exceeded [`VtnConfig::max_body_bytes`](crate::vtn::VtnConfig::max_body_bytes).
///
/// Reachable two ways — the layer refuses an oversized `Content-Length` outright, and the
/// extractor's read runs off the end of a limited stream when there is none — and both must
/// answer `413`. They did not while the extractor mapped every read failure to `400` `[D-104]`.
#[error("{0}")]
PayloadTooLarge(String),
/// A payload contradicted its enumeration under a strict policy.
#[error("payload validation failed")]
InvalidPayload(Vec<PayloadViolation>),
/// No credential, or an unusable one.
#[error(transparent)]
Unauthorized(#[from] AuthError),
/// Authenticated, but not permitted.
#[error("the {0} scope is required")]
MissingScope(Scope),
/// Authenticated and scoped, but this object is not the caller's to touch.
#[error("{0}")]
Forbidden(String),
/// The object does not exist, or the caller may not know that it does.
///
/// Object privacy and genuine absence deliberately share this: a `403` on a hidden object would
/// confirm it exists, which is what targeting conceals.
#[error("{object_type} {id} not found")]
NotFound {
/// The kind of object.
object_type: crate::model::ObjectType,
/// The id that was looked up.
id: crate::model::ObjectId,
},
/// No route matched the request path.
#[error("no such endpoint")]
NoSuchRoute,
/// A storage-level failure, which carries its own status.
#[error(transparent)]
Storage(#[from] StorageError),
/// Something the VTN does not implement.
#[error("{0}")]
NotImplemented(String),
/// An unexpected internal failure.
#[error("{0}")]
Internal(String),
/// The VTN could not attempt the request, but the request itself was fine.
///
/// A `503`, so a client knows to retry. Distinct from [`ApiError::Internal`], which is a `500`
/// and means something went wrong rather than something was busy.
#[error("{0}")]
Unavailable(String),
}
impl ApiError {
/// The HTTP status this error maps to.
pub fn status(&self) -> StatusCode {
match self {
ApiError::BadRequest(_) | ApiError::InvalidPayload(_) => StatusCode::BAD_REQUEST,
ApiError::UnsupportedMediaType(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE,
ApiError::PayloadTooLarge(_) => StatusCode::PAYLOAD_TOO_LARGE,
ApiError::Unauthorized(_) => StatusCode::UNAUTHORIZED,
ApiError::MissingScope(_) | ApiError::Forbidden(_) => StatusCode::FORBIDDEN,
ApiError::NotFound { .. } | ApiError::NoSuchRoute => StatusCode::NOT_FOUND,
ApiError::NotImplemented(_) => StatusCode::NOT_IMPLEMENTED,
ApiError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
ApiError::Unavailable(_) => StatusCode::SERVICE_UNAVAILABLE,
ApiError::Storage(e) => match e {
StorageError::NotFound { .. } => StatusCode::NOT_FOUND,
StorageError::Conflict { .. } => StatusCode::CONFLICT,
// A body that references a non-existent object is a bad request, not a 404: the
// URL resolved fine, the payload did not.
StorageError::DanglingReference { .. } => StatusCode::BAD_REQUEST,
StorageError::Unavailable(_) => StatusCode::INTERNAL_SERVER_ERROR,
},
}
}
/// The slug used in the problem's `type` URI.
fn slug(&self) -> &'static str {
match self {
ApiError::BadRequest(_) => "bad-request",
ApiError::UnsupportedMediaType(_) => "unsupported-media-type",
ApiError::PayloadTooLarge(_) => "payload-too-large",
ApiError::InvalidPayload(_) => "invalid-payload",
ApiError::Unauthorized(_) => "unauthorized",
ApiError::MissingScope(_) => "missing-scope",
ApiError::Forbidden(_) => "forbidden",
ApiError::NotFound { .. } => "not-found",
ApiError::NoSuchRoute => "no-such-route",
ApiError::NotImplemented(_) => "not-implemented",
ApiError::Internal(_) => "internal-server-error",
ApiError::Unavailable(_) => "unavailable",
ApiError::Storage(e) => match e {
StorageError::NotFound { .. } => "not-found",
StorageError::Conflict { .. } => "conflict",
StorageError::DanglingReference { .. } => "dangling-reference",
StorageError::Unavailable(_) => "storage-unavailable",
},
}
}
/// Build the problem body.
pub fn problem(&self) -> Problem {
let status = self.status();
let title = status.canonical_reason().unwrap_or("Error");
let detail = match self {
// Internal failures are logged in full but described in general terms: the client
// cannot act on a database message, and it may reveal internals.
ApiError::Internal(_) => "an unexpected internal error occurred".to_string(),
ApiError::InvalidPayload(violations) => violations
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join("; "),
other => other.to_string(),
};
Problem::new(status.as_u16(), self.slug(), title).with_detail(detail)
}
}
impl From<serde_json::Error> for ApiError {
fn from(e: serde_json::Error) -> Self {
ApiError::BadRequest(format!("malformed JSON: {e}"))
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
let status = self.status();
if status.is_server_error() {
tracing::error!(error = %self, "request failed");
} else {
tracing::debug!(error = %self, status = %status, "request rejected");
}
let mut headers = HeaderMap::new();
// RFC 6750: a 401 must say how to authenticate.
if status == StatusCode::UNAUTHORIZED {
headers.insert(
header::WWW_AUTHENTICATE,
HeaderValue::from_static("Bearer realm=\"openadr\""),
);
}
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/problem+json"),
);
(status, headers, Json(self.problem())).into_response()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{ObjectId, ObjectType};
#[test]
fn storage_errors_map_to_the_documented_statuses() {
let not_found = ApiError::Storage(StorageError::NotFound {
object_type: ObjectType::Event,
id: ObjectId::new("e1").unwrap(),
});
assert_eq!(not_found.status(), StatusCode::NOT_FOUND);
let conflict = ApiError::Storage(StorageError::Conflict {
object_type: ObjectType::Program,
field: "programName",
value: "tou".into(),
});
assert_eq!(conflict.status(), StatusCode::CONFLICT);
// A dangling reference is the body's fault, so 400 rather than 404.
let dangling = ApiError::Storage(StorageError::DanglingReference {
field: "programID",
value: "nope".into(),
});
assert_eq!(dangling.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn internal_errors_do_not_leak_their_detail() {
let e = ApiError::Internal("connection string: postgres://user:hunter2@db".into());
let problem = e.problem();
assert_eq!(problem.status, Some(500));
assert!(!problem.detail.unwrap().contains("hunter2"));
}
#[test]
fn problems_carry_a_dereferenceable_type() {
let p = ApiError::MissingScope(Scope::WriteEvents).problem();
assert_eq!(
p.r#type.as_deref(),
Some("https://hupe1980.github.io/openadr/problems/missing-scope")
);
assert!(p.detail.unwrap().contains("write_events"));
}
/// One of every variant, so the registry and the code cannot drift apart silently.
///
/// A `type` URI is only worth minting if it resolves, and it resolves because
/// `PROBLEM_TYPES` is published as a redirect per slug. A variant whose slug is missing here
/// would serve clients a URI that documents nothing.
#[test]
fn every_variant_mints_a_published_type() {
use crate::model::{ObjectId, ObjectType, problem::PROBLEM_TYPES};
let id = ObjectId::new("object-0000000000").unwrap();
let errors = [
ApiError::BadRequest("x".into()),
ApiError::UnsupportedMediaType("x".into()),
ApiError::PayloadTooLarge("x".into()),
ApiError::InvalidPayload(Vec::new()),
ApiError::Unauthorized(AuthError::Missing),
ApiError::MissingScope(Scope::WriteEvents),
ApiError::Forbidden("x".into()),
ApiError::NotFound {
object_type: ObjectType::Event,
id: id.clone(),
},
ApiError::NoSuchRoute,
ApiError::NotImplemented("x".into()),
ApiError::Internal("x".into()),
ApiError::Unavailable("x".into()),
ApiError::Storage(StorageError::NotFound {
object_type: ObjectType::Event,
id,
}),
ApiError::Storage(StorageError::Conflict {
object_type: ObjectType::Event,
field: "f",
value: "v".into(),
}),
ApiError::Storage(StorageError::DanglingReference {
field: "f",
value: "v".into(),
}),
ApiError::Storage(StorageError::Unavailable("x".into())),
];
for error in &errors {
let slug = error.slug();
assert!(
PROBLEM_TYPES.contains(&slug),
"{slug} is minted but not published; add it to PROBLEM_TYPES and the registry page"
);
}
}
}