surrealdb_core/api/
err.rs1use http::StatusCode;
2use thiserror::Error;
3
4use crate::sql::Bytesize;
5
6#[derive(Error, Debug)]
7#[non_exhaustive]
8pub enum ApiError {
9 #[error("Invalid request body: Expected data frame but received another frame type")]
10 InvalidRequestBody,
11
12 #[error("Invalid request body: The body exceeded the max payload size of {0}")]
13 RequestBodyTooLarge(Bytesize),
14
15 #[error("Failed to decode the request body")]
16 BodyDecodeFailure,
17
18 #[error("Failed to encode the response body")]
19 BodyEncodeFailure,
20
21 #[error("Invalid API response: {0}")]
22 InvalidApiResponse(String),
23
24 #[error("Invalid Accept or Content-Type header")]
25 InvalidFormat,
26
27 #[error("Missing Accept or Content-Type header")]
28 MissingFormat,
29
30 #[error("An unreachable error occured: {0}")]
31 Unreachable(String),
32}
33
34impl From<ApiError> for String {
35 fn from(e: ApiError) -> String {
36 e.to_string()
37 }
38}
39
40impl ApiError {
41 pub fn status_code(&self) -> StatusCode {
42 match self {
43 Self::InvalidRequestBody => StatusCode::BAD_REQUEST,
44 Self::RequestBodyTooLarge(_) => StatusCode::PAYLOAD_TOO_LARGE,
45 Self::BodyDecodeFailure => StatusCode::BAD_REQUEST,
46 Self::BodyEncodeFailure => StatusCode::INTERNAL_SERVER_ERROR,
47 Self::InvalidApiResponse(_) => StatusCode::INTERNAL_SERVER_ERROR,
48 Self::InvalidFormat => StatusCode::BAD_REQUEST,
49 Self::MissingFormat => StatusCode::BAD_REQUEST,
50 Self::Unreachable(_) => StatusCode::INTERNAL_SERVER_ERROR,
51 }
52 }
53}