Add custom error pages
This commit is contained in:
parent
685b92a654
commit
f832e10fac
11
src/error.rs
11
src/error.rs
@ -1,4 +1,6 @@
|
||||
use crate::P500;
|
||||
use actix_web::{HttpResponse, ResponseError};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Error {
|
||||
@ -9,8 +11,9 @@ pub(crate) enum Error {
|
||||
Serial(serde_json::Error),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
// write!(fmt, "{}", P500)
|
||||
match self {
|
||||
Error::Badge(s) => write!(fmt, "Badge({})", s),
|
||||
Error::Git(e) => write!(fmt, "Git({})", e),
|
||||
@ -23,7 +26,9 @@ impl std::fmt::Display for Error {
|
||||
|
||||
impl ResponseError for Error {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
HttpResponse::InternalServerError().finish()
|
||||
HttpResponse::InternalServerError()
|
||||
.content_type("text/html")
|
||||
.body(P500)
|
||||
}
|
||||
}
|
||||
|
||||
|
19
src/main.rs
19
src/main.rs
@ -35,6 +35,8 @@ struct State {
|
||||
}
|
||||
|
||||
const INDEX: &str = include_str!("../static/index.html");
|
||||
const P404: &str = include_str!("../static/404.html");
|
||||
const P500: &str = include_str!("../static/500.html");
|
||||
const CSS: &str = include_str!("../static/tacit-css.min.css");
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
@ -194,22 +196,18 @@ fn overview(_: web::Path<(String, String)>) -> HttpResponse {
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> HttpResponse {
|
||||
let (tx, rx_body) = mpsc::unbounded();
|
||||
let _ = tx.unbounded_send(Bytes::from(INDEX.as_bytes()));
|
||||
HttpResponse::Ok().content_type("text/html").body(INDEX)
|
||||
}
|
||||
|
||||
HttpResponse::Ok()
|
||||
fn p404() -> HttpResponse {
|
||||
HttpResponse::NotFound()
|
||||
.content_type("text/html")
|
||||
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request")))
|
||||
.body(P404)
|
||||
}
|
||||
|
||||
#[get("/tacit-css.min.css")]
|
||||
fn css() -> HttpResponse {
|
||||
let (tx, rx_body) = mpsc::unbounded();
|
||||
let _ = tx.unbounded_send(Bytes::from(CSS.as_bytes()));
|
||||
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/css")
|
||||
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request")))
|
||||
HttpResponse::Ok().content_type("text/css").body(CSS)
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
@ -234,6 +232,7 @@ fn main() -> std::io::Result<()> {
|
||||
.service(web::resource("/view/github/{user}/{repo}").to(overview))
|
||||
.service(web::resource("/view/gitlab/{user}/{repo}").to(overview))
|
||||
.service(web::resource("/view/github/{user}/{repo}").to(overview))
|
||||
.default_service(web::resource("").route(web::get().to(p404)))
|
||||
})
|
||||
.bind(interface)?
|
||||
.run()
|
||||
|
46
static/404.html
Normal file
46
static/404.html
Normal file
@ -0,0 +1,46 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="keywords" content="Hits-of-Code, GitHub, Badge" />
|
||||
<meta name="description" content="Hits-of-Code Badges for Git repositories" />
|
||||
<link rel="stylesheet" href="/tacit-css.min.css" />
|
||||
<title>Page Not Found - Hits-of-Code Badges</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<section>
|
||||
<header>
|
||||
<h1>404 - Page Not Found</h1>
|
||||
</header>
|
||||
|
||||
<article>
|
||||
<p>
|
||||
<big>Sorry</big>. I couldn't find the page you are looking for. Please go <a href="/">back to the homepage</a>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
If you think, this is a mistake on my side, please <a href="mailto:mail+hoc@vbrandl.net">drop me a mail</a>.
|
||||
</p>
|
||||
</article>
|
||||
|
||||
<footer>
|
||||
<nav>
|
||||
<ul>
|
||||
<li>
|
||||
<small>Created by <a href="https://www.vbrandl.net">Valentin Brandl</a>.</small>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><small><a href="https://github.com/vbrandl/hoc">GitHub</a></small></li>
|
||||
<li><small><a href="https://opensource.org/licenses/MIT">MIT License</a></small></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</footer>
|
||||
</section>
|
||||
|
||||
</body>
|
||||
</html>
|
46
static/500.html
Normal file
46
static/500.html
Normal file
@ -0,0 +1,46 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="keywords" content="Hits-of-Code, GitHub, Badge" />
|
||||
<meta name="description" content="Hits-of-Code Badges for Git repositories" />
|
||||
<link rel="stylesheet" href="/tacit-css.min.css" />
|
||||
<title>Internal Server Error - Hits-of-Code Badges</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<section>
|
||||
<header>
|
||||
<h1>500 - Internal Server Error</h1>
|
||||
</header>
|
||||
|
||||
<article>
|
||||
<p>
|
||||
<big>Oops</big>. Looks like a made a mistake. Please go <a href="/">back to the homepage</a> and try again.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
If you think, this is a bug, please <a href="mailto:mail+hoc@vbrandl.net">drop me a mail</a>.
|
||||
</p>
|
||||
</article>
|
||||
|
||||
<footer>
|
||||
<nav>
|
||||
<ul>
|
||||
<li>
|
||||
<small>Created by <a href="https://www.vbrandl.net">Valentin Brandl</a>.</small>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><small><a href="https://github.com/vbrandl/hoc">GitHub</a></small></li>
|
||||
<li><small><a href="https://opensource.org/licenses/MIT">MIT License</a></small></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</footer>
|
||||
</section>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user