Render HTML for application errors
This commit is contained in:
parent
2d1f1b4e05
commit
809e6d7b15
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1201,6 +1201,7 @@ dependencies = [
|
|||
"actix-rt",
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
"awc",
|
||||
"dotenv",
|
||||
"env_logger",
|
||||
"futures",
|
||||
|
@ -1209,6 +1210,7 @@ dependencies = [
|
|||
"ructe",
|
||||
"serde",
|
||||
"structopt",
|
||||
"thiserror",
|
||||
"url",
|
||||
]
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ build = "src/build.rs"
|
|||
actix-rt = "1.1.1"
|
||||
actix-web = "3.0.0-alpha.2"
|
||||
anyhow = "1.0"
|
||||
awc = "2.0.0-alpha.2"
|
||||
dotenv = "0.13.0"
|
||||
env_logger = "0.7"
|
||||
futures = "0.3"
|
||||
|
@ -22,6 +23,7 @@ mime = "0.3"
|
|||
once_cell = "1.4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
structopt = "0.3.14"
|
||||
thiserror = "1.0"
|
||||
url = "2.1"
|
||||
|
||||
[build-dependencies]
|
||||
|
|
49
src/main.rs
49
src/main.rs
|
@ -6,7 +6,7 @@ use actix_web::{
|
|||
StatusCode,
|
||||
},
|
||||
middleware::Logger,
|
||||
web, App, HttpRequest, HttpResponse, HttpServer,
|
||||
web, App, HttpRequest, HttpResponse, HttpServer, ResponseError,
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{io::Cursor, net::SocketAddr, time::Duration};
|
||||
|
@ -148,7 +148,40 @@ fn statics(file: &str) -> String {
|
|||
format!("/static/{}", file)
|
||||
}
|
||||
|
||||
async fn index() -> Result<HttpResponse, actix_web::Error> {
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
enum Error {
|
||||
#[error("{0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("{0}")]
|
||||
SendRequest(#[from] actix_web::client::SendRequestError),
|
||||
|
||||
#[error("{0}")]
|
||||
JsonPayload(#[from] awc::error::JsonPayloadError),
|
||||
}
|
||||
|
||||
impl ResponseError for Error {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
let mut builder = HttpResponse::build(self.status_code());
|
||||
|
||||
let mut cursor = Cursor::new(vec![]);
|
||||
if let Err(e) = self::templates::error(&mut cursor, &self.to_string()) {
|
||||
return builder
|
||||
.content_type(mime::TEXT_PLAIN.essence_str())
|
||||
.body(e.to_string());
|
||||
}
|
||||
|
||||
builder
|
||||
.content_type(mime::TEXT_HTML.essence_str())
|
||||
.body(cursor.into_inner())
|
||||
}
|
||||
}
|
||||
|
||||
async fn index() -> Result<HttpResponse, Error> {
|
||||
let mut cursor = Cursor::new(vec![]);
|
||||
self::templates::index(&mut cursor, "/upload", "images[]")?;
|
||||
Ok(HttpResponse::Ok()
|
||||
|
@ -160,7 +193,7 @@ async fn upload(
|
|||
req: HttpRequest,
|
||||
body: web::Payload,
|
||||
client: web::Data<Client>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let mut res = client
|
||||
.request_from(CONFIG.upstream_upload_url(), req.head())
|
||||
.if_some(req.head().peer_addr, |addr, req| {
|
||||
|
@ -183,7 +216,7 @@ async fn image(
|
|||
url: String,
|
||||
req: HttpRequest,
|
||||
client: web::Data<Client>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let res = client
|
||||
.request_from(url, req.head())
|
||||
.if_some(req.head().peer_addr, |addr, req| {
|
||||
|
@ -210,7 +243,7 @@ async fn thumbnail(
|
|||
parts: web::Path<(u64, String)>,
|
||||
req: HttpRequest,
|
||||
client: web::Data<Client>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let (size, file) = parts.into_inner();
|
||||
|
||||
if (size % 100) == 0 {
|
||||
|
@ -226,7 +259,7 @@ async fn full_res(
|
|||
filename: web::Path<String>,
|
||||
req: HttpRequest,
|
||||
client: web::Data<Client>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let url = CONFIG.upstream_image_url(&filename.into_inner());
|
||||
|
||||
image(url, req, client).await
|
||||
|
@ -253,7 +286,7 @@ async fn delete(
|
|||
components: web::Path<(String, String)>,
|
||||
req: HttpRequest,
|
||||
client: web::Data<Client>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let (token, file) = components.into_inner();
|
||||
|
||||
let url = CONFIG.upstream_delete_url(&token, &file);
|
||||
|
@ -275,7 +308,7 @@ fn to_404() -> HttpResponse {
|
|||
.finish()
|
||||
}
|
||||
|
||||
async fn not_found() -> Result<HttpResponse, actix_web::Error> {
|
||||
async fn not_found() -> Result<HttpResponse, Error> {
|
||||
let mut cursor = Cursor::new(vec![]);
|
||||
|
||||
self::templates::not_found(&mut cursor)?;
|
||||
|
|
17
templates/error.rs.html
Normal file
17
templates/error.rs.html
Normal file
|
@ -0,0 +1,17 @@
|
|||
@use super::{layout_html, return_home_html};
|
||||
|
||||
@(msg: &str)
|
||||
|
||||
@:layout_html({
|
||||
<title>Error</title>
|
||||
}, {
|
||||
<section>
|
||||
<article>
|
||||
<h3>There was an error processing your request</h3>
|
||||
</article>
|
||||
<article>
|
||||
<p>@msg</p>
|
||||
</article>
|
||||
@:return_home_html()
|
||||
</section>
|
||||
})
|
|
@ -1,4 +1,4 @@
|
|||
@use super::{layout_html, statics::images_css};
|
||||
@use super::{layout_html, return_home_html, statics::images_css};
|
||||
@use crate::Images;
|
||||
|
||||
@(images: Images)
|
||||
|
@ -36,8 +36,6 @@
|
|||
<p>@images.msg()</p>
|
||||
</article>
|
||||
}
|
||||
<article>
|
||||
<p><a href="/">Return Home</a></p>
|
||||
</article>
|
||||
@:return_home_html()
|
||||
</section>
|
||||
})
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
@use super::{layout_html, statics::not_found_css};
|
||||
@use super::{layout_html, return_home_html, statics::not_found_css};
|
||||
|
||||
@()
|
||||
|
||||
|
@ -13,8 +13,6 @@
|
|||
<article>
|
||||
<p>The page you are looking for could not be found</p>
|
||||
</article>
|
||||
<article>
|
||||
<p><a href="/">Return Home</a></p>
|
||||
</article>
|
||||
@:return_home_html()
|
||||
</section>
|
||||
})
|
||||
|
|
5
templates/return_home.rs.html
Normal file
5
templates/return_home.rs.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
@()
|
||||
|
||||
<article>
|
||||
<p><a href="/">Return Home</a></p>
|
||||
</article>
|
Loading…
Reference in a new issue