actix_error_mapper_middleware/
lib.rs

1//! # Actix-error-mapper-middleware
2
3//! This simple crate allows you to remap actix-web errors to your own custom error type. You could for example return a html wrapped error. 
4//!
5//! ## Example
6//!
7//! Your custom error trait has to implement the 
8//! ```std::convert::From<actix_web::Error>``` and the ```actix_web::ResponseError``` traits. 
9//!
10//! ```rust 
11//! use actix_error_mapper_middleware::MapperService;
12//! use actix_jwt_auth_middleware::{AuthService, Authority};
13//! use actix_web::{web, App, Error as ActixError, HttpResponse, HttpServer, ResponseError};
14//! use rusty_html::{html, HTMLify};
15//!
16//! #[actix_web::main]
17//! async fn main() -> std::io::Result<()> {
18//!     HttpServer::new(move || {
19//!         App::new()
20//!             // this wrap will catch every error thrown in the Apps scope an convert it to the MyError type
21//!             .wrap(MapperService::<MyError>::new())
22//!             .service(
23//!                 web::scope("")
24//!                     // this service will throw an error if you are not logged in
25//!                     .wrap(AuthService::new(Authority::<u32>::default())),
26//!             )
27//!     })
28//!     .bind("127.0.0.1:8080")?
29//!     .run()
30//!     .await
31//! }
32//!
33//! #[derive(Debug)]
34//! struct MyError {
35//!     error: ActixError,
36//! }
37//!
38//! impl std::convert::From<ActixError> for MyError {
39//!     fn from(error: ActixError) -> Self {
40//!         Self { error }
41//!     }
42//! }
43//!
44//! impl std::fmt::Display for MyError {
45//!     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46//!         f.write_fmt(format_args!(
47//!             "An error occurred: \"{}\"",
48//!             self.error.to_string()
49//!         ))
50//!     }
51//! }
52//!
53//! impl ResponseError for MyError {
54//!     fn status_code(&self) -> actix_web::http::StatusCode {
55//!         self.error.as_response_error().status_code()
56//!     }
57//!
58//!     fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
59//!         HttpResponse::build(self.status_code()).body(html!(
60//!         <html style="
61//!                     margin: 0;
62//!                     height: 100%;
63//!                 ">
64//!             <head>
65//!                 <title>Error</title>
66//!             </head>
67//!             <body style="
68//!                     display: grid;
69//!                     margin: 0;
70//!                     height: 100%;
71//!                     width: 100%;
72//!                     font-family: 'Raleway';
73//!                     place-items: center;
74//!                     color: #fff;
75//!                     overflow-y: hidden;
76//!                     background: #000;
77//!                 ">
78//!                 <section style="
79//!                         padding: 2rem;
80//!                         background: #289dcc;
81//!                     ">
82//!                     <h1>Ups . . .</h1>
83//!                     <p>{self.to_string()}</p>
84//!                 </section>
85//!             </body>
86//!         </html>
87//!         ))
88//!     }
89//! }
90//! ```
91
92mod middleware;
93mod service;
94
95use middleware::*;
96pub use service::MapperService;