Much nicer error pages

This commit is contained in:
Tomasz Drwięga
2016-08-31 16:53:22 +02:00
parent 25e6a4e45f
commit 2789824a51
7 changed files with 108 additions and 40 deletions

View File

@@ -22,7 +22,7 @@ use std::path::{PathBuf, Path};
use std::sync::Arc;
use std::str::FromStr;
use jsonrpc_core::IoHandler;
use util::H256;
use util::{H256, version};
#[cfg(feature = "ui")]
mod signer {
@@ -112,7 +112,12 @@ impl ws::Handler for Session {
if !self.skip_origin_validation {
if !origin_is_allowed(&self.self_origin, origin) && !(origin.is_none() && origin_is_allowed(&self.self_origin, host)) {
warn!(target: "signer", "Blocked connection to Signer API from untrusted origin.");
return Ok(ws::Response::forbidden(format!("You are not allowed to access system ui. Use: http://{}", self.self_origin)));
return Ok(error(
ErrorType::Forbidden,
"URL Blocked",
"You are not allowed to access Trusted Signer using this URL.",
Some(&format!("Use: http://{}", self.self_origin)),
));
}
}
@@ -121,7 +126,7 @@ impl ws::Handler for Session {
// Check authorization
if !auth_is_valid(&self.authcodes_path, req.protocols()) {
info!(target: "signer", "Unauthorized connection to Signer API blocked.");
return Ok(ws::Response::forbidden("You are not authorized.".into()));
return Ok(error(ErrorType::Forbidden, "Not Authorized", "Request to this API was not authorized.", None));
}
let protocols = req.protocols().expect("Existence checked by authorization.");
@@ -137,7 +142,7 @@ impl ws::Handler for Session {
Ok(signer::handle(req.resource())
.map_or_else(
// return 404 not found
|| add_headers(ws::Response::not_found("Not found".into()), "text/plain"),
|| error(ErrorType::NotFound, "Not found", "Requested file was not found.", None),
// or serve the file
|f| add_headers(ws::Response::ok(f.content.into()), &f.mime)
))
@@ -185,3 +190,24 @@ impl ws::Factory for Factory {
}
}
}
enum ErrorType {
NotFound,
Forbidden,
}
fn error(error: ErrorType, title: &str, message: &str, details: Option<&str>) -> ws::Response {
let content = format!(
include_str!("../../../dapps/src/error_tpl.html"),
title=title,
meta="",
message=message,
details=details.unwrap_or(""),
version=version(),
);
let res = match error {
ErrorType::NotFound => ws::Response::not_found(content),
ErrorType::Forbidden => ws::Response::forbidden(content),
};
add_headers(res, "text/html")
}