mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
Demonstrate how to render error pages using templates.
This commit is contained in:
@ -8,5 +8,6 @@ edition = "2018"
|
||||
[dependencies]
|
||||
env_logger = "0.7"
|
||||
tera = "1.0"
|
||||
actix-http = "1.0.1"
|
||||
actix-web = "2.0.0"
|
||||
actix-rt = "1.0.0"
|
||||
|
@ -15,3 +15,4 @@ cargo run (or ``cargo watch -x run``)
|
||||
### web client
|
||||
|
||||
- [http://localhost:8080](http://localhost:8080)
|
||||
- [http://localhost:8080/non-existing-page](http://localhost:8080/non-existing-page) - 404 page rendered using template
|
||||
|
@ -1,6 +1,10 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer};
|
||||
use actix_http::{body::Body, Response};
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers};
|
||||
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result};
|
||||
use tera::Tera;
|
||||
|
||||
// store tera template in application state
|
||||
@ -35,8 +39,53 @@ async fn main() -> std::io::Result<()> {
|
||||
.data(tera)
|
||||
.wrap(middleware::Logger::default()) // enable logger
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(web::scope("").wrap(error_handlers()))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
// Custom error handlers, to return HTML responses when an error occurs.
|
||||
fn error_handlers() -> ErrorHandlers<Body> {
|
||||
ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)
|
||||
}
|
||||
|
||||
// Error handler for a 404 Page not found error.
|
||||
fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
|
||||
let response = get_error_response(&res, "Page not found");
|
||||
Ok(ErrorHandlerResponse::Response(
|
||||
res.into_response(response.into_body()),
|
||||
))
|
||||
}
|
||||
|
||||
// Generic error handler.
|
||||
fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> Response<Body> {
|
||||
let request = res.request();
|
||||
|
||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||
// rendering of the error page.
|
||||
let fallback = |e: &str| {
|
||||
Response::build(res.status())
|
||||
.content_type("text/plain")
|
||||
.body(e.to_string())
|
||||
};
|
||||
|
||||
let tera = request.app_data::<web::Data<Tera>>().map(|t| t.get_ref());
|
||||
match tera {
|
||||
Some(tera) => {
|
||||
let mut context = tera::Context::new();
|
||||
context.insert("error", error);
|
||||
context.insert("status_code", res.status().as_str());
|
||||
let body = tera.render("error.html", &context);
|
||||
|
||||
match body {
|
||||
Ok(body) => Response::build(res.status())
|
||||
.content_type("text/html")
|
||||
.body(body),
|
||||
Err(_) => fallback(error),
|
||||
}
|
||||
}
|
||||
None => fallback(error),
|
||||
}
|
||||
}
|
||||
|
10
template_tera/templates/error.html
Normal file
10
template_tera/templates/error.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>{{ error }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ status_code }} {{ error }}</h1>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user