web: fix return status for error page with localization

This commit is contained in:
Vincent Breitmoser 2021-02-20 14:22:40 +01:00
parent 1022e1797b
commit 726d04aca7
3 changed files with 20 additions and 4 deletions

View File

@ -66,6 +66,16 @@ pub enum Query {
Invalid(),
}
impl Query {
pub fn is_invalid(&self) -> bool {
match self {
Query::Invalid() => true,
Query::InvalidShort() => true,
_ => false,
}
}
}
impl FromStr for Query {
type Err = anyhow::Error;

View File

@ -281,6 +281,10 @@ pub fn key_to_response_plain(
i18n: I18n,
query: Query,
) -> MyResponse {
if query.is_invalid() {
return MyResponse::bad_request_plain(describe_query_error(&i18n, &query));
}
let fp = if let Some(fp) = db.lookup_primary_fingerprint(&query) {
fp
} else {
@ -979,13 +983,13 @@ pub mod tests {
fn search_invalid() {
let (_tmpdir, client) = client().unwrap();
check_response(&client, "/search?q=0x1234abcd",
Status::BadRequest, "not supported, sorry!");
Status::BadRequest, "not supported");
check_response(&client, "/search?q=1234abcd",
Status::BadRequest, "not supported, sorry!");
Status::BadRequest, "not supported");
check_response(&client, "/pks/lookup?op=get&search=0x1234abcd",
Status::BadRequest, "not supported, sorry!");
Status::BadRequest, "not supported");
check_response(&client, "/pks/lookup?op=get&search=1234abcd",
Status::BadRequest, "not supported, sorry!");
Status::BadRequest, "not supported");
}

View File

@ -243,6 +243,8 @@ fn key_to_response(
) -> MyResponse {
let fp = if let Some(fp) = db.lookup_primary_fingerprint(&query) {
fp
} else if query.is_invalid() {
return MyResponse::bad_request("index", anyhow!(describe_query_error(&i18n, &query)));
} else {
return MyResponse::not_found(None, describe_query_error(&i18n, &query));
};