actix_cors/
error.rs

1use actix_web::{http::StatusCode, HttpResponse, ResponseError};
2use derive_more::{Display, Error};
3
4/// Errors that can occur when processing CORS guarded requests.
5#[derive(Debug, Clone, Display, Error)]
6#[non_exhaustive]
7pub enum CorsError {
8    /// Allowed origin argument must not be wildcard (`*`).
9    #[display(fmt = "`allowed_origin` argument must not be wildcard (`*`)")]
10    WildcardOrigin,
11
12    /// Request header `Origin` is required but was not provided.
13    #[display(fmt = "Request header `Origin` is required but was not provided")]
14    MissingOrigin,
15
16    /// Request header `Access-Control-Request-Method` is required but is missing.
17    #[display(fmt = "Request header `Access-Control-Request-Method` is required but is missing")]
18    MissingRequestMethod,
19
20    /// Request header `Access-Control-Request-Method` has an invalid value.
21    #[display(fmt = "Request header `Access-Control-Request-Method` has an invalid value")]
22    BadRequestMethod,
23
24    /// Request header `Access-Control-Request-Headers` has an invalid value.
25    #[display(fmt = "Request header `Access-Control-Request-Headers` has an invalid value")]
26    BadRequestHeaders,
27
28    /// Origin is not allowed to make this request.
29    #[display(fmt = "Origin is not allowed to make this request")]
30    OriginNotAllowed,
31
32    /// Request method is not allowed.
33    #[display(fmt = "Requested method is not allowed")]
34    MethodNotAllowed,
35
36    /// One or more request headers are not allowed.
37    #[display(fmt = "One or more request headers are not allowed")]
38    HeadersNotAllowed,
39}
40
41impl ResponseError for CorsError {
42    fn status_code(&self) -> StatusCode {
43        StatusCode::BAD_REQUEST
44    }
45
46    fn error_response(&self) -> HttpResponse {
47        HttpResponse::with_body(self.status_code(), self.to_string()).map_into_boxed_body()
48    }
49}