rustauth_actix_web/
error.rs1use actix_web::http::header;
2use actix_web::{HttpResponse, ResponseError};
3use rustauth::api::ApiErrorResponse;
4
5#[non_exhaustive]
7#[derive(Debug, thiserror::Error)]
8pub enum RustAuthActixWebError {
9 #[error("RustAuth base path must be an absolute literal path mountable by Actix Web: {0}")]
10 InvalidBasePath(String),
11 #[error("RustAuth base_url is not a valid absolute URL: {0}")]
12 InvalidBaseUrl(String),
13 #[error(
14 "RustAuth base_url path `{url_path}` does not match configured base_path `{base_path}`"
15 )]
16 InconsistentBaseUrlPath { url_path: String, base_path: String },
17}
18
19impl ResponseError for RustAuthActixWebError {}
20
21pub(crate) fn bad_request_response() -> HttpResponse {
22 json_error_response(
23 actix_web::http::StatusCode::BAD_REQUEST,
24 "INVALID_REQUEST_BODY",
25 "Invalid request body",
26 None,
27 )
28}
29
30pub(crate) fn payload_too_large_response() -> HttpResponse {
31 json_error_response(
32 actix_web::http::StatusCode::PAYLOAD_TOO_LARGE,
33 "PAYLOAD_TOO_LARGE",
34 "Payload too large",
35 None,
36 )
37}
38
39pub(crate) fn internal_error_response() -> HttpResponse {
40 json_error_response(
41 actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
42 "INTERNAL_SERVER_ERROR",
43 "Internal server error",
44 None,
45 )
46}
47
48pub(crate) fn json_error_response(
49 status: actix_web::http::StatusCode,
50 code: &str,
51 message: &str,
52 original_message: Option<String>,
53) -> HttpResponse {
54 let body = serde_json::to_vec(&ApiErrorResponse {
55 code: code.to_owned(),
56 message: message.to_owned(),
57 original_message,
58 })
59 .unwrap_or_else(|_| {
60 b"{\"code\":\"INTERNAL_SERVER_ERROR\",\"message\":\"Internal server error\"}".to_vec()
61 });
62 HttpResponse::build(status)
63 .insert_header((header::CONTENT_TYPE, "application/json"))
64 .body(body)
65}