Skip to main content

rightkit_daemon/contract/
status.rs

1use crate::error::*;
2use crate::ServiceError;
3
4/// Error code -> HTTP-style status, for transports that carry one. Local transport ignores it
5/// and sends the [`ServiceError`] as is. Unknown codes are 500, or 503 when retryable.
6pub const STATUS_TABLE: &[(&str, u16)] = &[
7    ("bad_request", 400),
8    (ERROR_UNAUTHENTICATED, 401),
9    (ERROR_RATE_LIMITED, 429),
10    (ERROR_FORBIDDEN, 403),
11    (ERROR_NOT_ALLOWED, 403),
12    (ERROR_REVISION_CONFLICT, 409),
13    (ERROR_RESPONSE_TOO_LARGE, 502),
14    (ERROR_REQUEST_TOO_LARGE, 413),
15    (ERROR_BAD_CHUNK, 400),
16    (ERROR_CONNECTION_CLOSED, 502),
17    (ERROR_HOST_HANDOFF, 503),
18    (ERROR_UNAVAILABLE, 503),
19    (ERROR_TIMEOUT, 504),
20    (ERROR_HOST_IDENTITY, 502),
21    (ERROR_SUITE_UNRESOLVED, 500),
22];
23
24/// Status for a code; `None` when the table does not know it.
25pub fn status_for_code(code: &str) -> Option<u16> {
26    STATUS_TABLE
27        .iter()
28        .find(|(c, _)| *c == code)
29        .map(|(_, s)| *s)
30}
31
32/// Status for an error: the table, else 503 when retryable, else 500.
33pub fn status_for_error(e: &ServiceError) -> u16 {
34    status_for_code(&e.code).unwrap_or(if e.retryable { 503 } else { 500 })
35}