Better responses for unknown methods vs resources in the API. (fixes: #146)

This commit is contained in:
Tim Bruijnzeels
2019-11-24 09:55:22 +01:00
parent 1219c40764
commit df66d8b403
3 changed files with 27 additions and 3 deletions
+11 -1
View File
@@ -280,6 +280,12 @@ pub enum ErrorCode {
#[display(fmt = "Out of sync with server, please send requests for instances sequentially")]
ConcurrentModification,
#[display(fmt = "unknown api method")]
UnknownMethod,
#[display(fmt = "unknown resource")]
UnknownResource,
#[display(fmt = "Unknown publisher")]
UnknownPublisher,
@@ -362,6 +368,8 @@ impl From<usize> for ErrorCode {
// 2000s -> General client issues
2001 => ErrorCode::CmsValidation,
2002 => ErrorCode::ConcurrentModification,
2003 => ErrorCode::UnknownMethod,
2004 => ErrorCode::UnknownResource,
// 2100s -> Pub Admin issues
2101 => ErrorCode::InvalidBaseUri,
@@ -412,6 +420,8 @@ impl Into<ErrorResponse> for ErrorCode {
// general errors
ErrorCode::CmsValidation => 2001,
ErrorCode::ConcurrentModification => 2002,
ErrorCode::UnknownMethod => 2003,
ErrorCode::UnknownResource => 2004,
// pub admin errors
ErrorCode::InvalidBaseUri => 2101,
@@ -470,7 +480,7 @@ mod tests {
test_code(n)
}
for n in 2001..2003 {
for n in 2001..2005 {
test_code(n)
}
+13 -1
View File
@@ -47,7 +47,19 @@ fn render_empty_res(res: Result<(), krillserver::Error>) -> HttpResponse {
/// A clean 404 result for the API (no content, not for humans)
fn api_not_found() -> HttpResponse {
HttpResponse::build(StatusCode::NOT_FOUND).finish()
let code = ErrorCode::UnknownResource;
let res: ErrorResponse = code.into();
let msg = serde_json::to_string(&res).unwrap();
let status = StatusCode::NOT_FOUND;
HttpResponse::build(status).body(msg)
}
pub fn api_bad_request() -> HttpResponse {
let code = ErrorCode::UnknownMethod;
let res: ErrorResponse = code.into();
let msg = serde_json::to_string(&res).unwrap();
let status = StatusCode::BAD_REQUEST;
HttpResponse::build(status).body(msg)
}
pub fn not_found() -> HttpResponse {
+3 -1
View File
@@ -102,7 +102,9 @@ pub fn start(config: &Config) -> Result<(), Error> {
// Force resyncing of all CAs at repo servers
.route("/cas/resync_all", post().to(resync_all))
// Force refresh of ALL CA certificates
.route("/cas/refresh_all", post().to(refresh_all)),
.route("/cas/refresh_all", post().to(refresh_all))
// Methods that are not found should return a bad request and some explanation
.default_service(web::route().to(api_bad_request)),
)
// Logged in users for the API
.route("/ui/is_logged_in", get().to(is_logged_in))