1use actix_web::{http::StatusCode, HttpResponse, ResponseError};
2use derive_more::{Display, Error};
3
4#[derive(Debug, Clone, Display, Error)]
6#[non_exhaustive]
7pub enum CorsError {
8 #[display(fmt = "`allowed_origin` argument must not be wildcard (`*`)")]
10 WildcardOrigin,
11
12 #[display(fmt = "Request header `Origin` is required but was not provided")]
14 MissingOrigin,
15
16 #[display(fmt = "Request header `Access-Control-Request-Method` is required but is missing")]
18 MissingRequestMethod,
19
20 #[display(fmt = "Request header `Access-Control-Request-Method` has an invalid value")]
22 BadRequestMethod,
23
24 #[display(fmt = "Request header `Access-Control-Request-Headers` has an invalid value")]
26 BadRequestHeaders,
27
28 #[display(fmt = "Origin is not allowed to make this request")]
30 OriginNotAllowed,
31
32 #[display(fmt = "Requested method is not allowed")]
34 MethodNotAllowed,
35
36 #[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}