api_response/error_code/
ety_grpc.rs

1//! Error type standard designed with reference to [gRPC status codes](https://grpc.io/docs/guides/status-codes/).
2
3use http::StatusCode;
4
5use super::ErrType;
6
7pub const CANCELLED: ErrType = ErrType::T1000("The operation was cancelled.");
8pub const UNKNOWN: ErrType = ErrType::T1001("Server internal exception or client-side parsing status error.");
9pub const INVALID_ARGUMENT: ErrType = ErrType::T1002("Invalid request argument.");
10pub const DEADLINE_EXCEEDED: ErrType = ErrType::T1003("No response received before Deadline expires.");
11pub const NOT_FOUND: ErrType = ErrType::T1004("Some requested entity was not found.");
12pub const ALREADY_EXISTS: ErrType = ErrType::T1005("The entity that is attempting to be created already exists.");
13pub const PERMISSION_DENIED: ErrType = ErrType::T1006("No permission to execute the request.");
14pub const RESOURCE_EXHAUSTED: ErrType = ErrType::T1007("Insufficient memory or message size exceeds the limit.");
15pub const FAILED_PRECONDITION: ErrType = ErrType::T1008("Operation rejected, system not in required state.");
16pub const ABORTED: ErrType = ErrType::T1009("Operation aborted due to concurrency issues");
17pub const OUT_OF_RANGE: ErrType = ErrType::T1010("The operation was attempted past the valid range.");
18pub const UNIMPLEMENTED: ErrType = ErrType::T1011("The received request/response is not supported.");
19pub const INTERNAL: ErrType = ErrType::T1012("Internal errors indicate broken invariants.");
20pub const UNAVAILABLE: ErrType = ErrType::T1013("The service is currently unavailable or there is a connection error.");
21pub const DATA_LOSS: ErrType = ErrType::T1014("Unrecoverable data loss or corruption.");
22pub const UNAUTHENTICATED: ErrType =
23    ErrType::T1015("The request does not have valid authentication credentials for the operation.");
24
25impl From<ErrType> for StatusCode {
26    fn from(value: ErrType) -> Self {
27        match value.flag() {
28            v if v == CANCELLED.flag() => unsafe { StatusCode::from_u16(499).unwrap_unchecked() },
29            v if v == INVALID_ARGUMENT.flag() => StatusCode::BAD_REQUEST,
30            v if v == DEADLINE_EXCEEDED.flag() => StatusCode::GATEWAY_TIMEOUT,
31            v if v == NOT_FOUND.flag() => StatusCode::NOT_FOUND,
32            v if v == ALREADY_EXISTS.flag() => StatusCode::CONFLICT,
33            v if v == PERMISSION_DENIED.flag() => StatusCode::FORBIDDEN,
34            v if v == RESOURCE_EXHAUSTED.flag() => StatusCode::INSUFFICIENT_STORAGE,
35            v if v == FAILED_PRECONDITION.flag() => StatusCode::PRECONDITION_FAILED,
36            v if v == ABORTED.flag() => StatusCode::CONFLICT,
37            v if v == OUT_OF_RANGE.flag() => StatusCode::RANGE_NOT_SATISFIABLE,
38            v if v == UNIMPLEMENTED.flag() => StatusCode::NOT_IMPLEMENTED,
39            v if v == INTERNAL.flag() => StatusCode::INTERNAL_SERVER_ERROR,
40            v if v == UNAVAILABLE.flag() => StatusCode::SERVICE_UNAVAILABLE,
41            v if v == DATA_LOSS.flag() => StatusCode::GONE,
42            v if v == UNAUTHENTICATED.flag() => StatusCode::UNAUTHORIZED,
43            v if v == UNKNOWN.flag() => unsafe { StatusCode::from_u16(520).unwrap_unchecked() },
44            _ => StatusCode::INTERNAL_SERVER_ERROR,
45        }
46    }
47}
48
49impl From<StatusCode> for ErrType {
50    fn from(value: StatusCode) -> Self {
51        match value.as_u16() {
52            499 => CANCELLED,
53            520 => UNKNOWN,
54            400 => INVALID_ARGUMENT,
55            504 => DEADLINE_EXCEEDED,
56            404 => NOT_FOUND,
57            409 => ALREADY_EXISTS,
58            403 => PERMISSION_DENIED,
59            507 => RESOURCE_EXHAUSTED,
60            412 => FAILED_PRECONDITION,
61            416 => OUT_OF_RANGE,
62            501 => UNIMPLEMENTED,
63            500 => INTERNAL,
64            503 => UNAVAILABLE,
65            410 => DATA_LOSS,
66            401 => UNAUTHENTICATED,
67            _ => INTERNAL,
68        }
69    }
70}