Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apiserver: include error messages in error responses #897

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions sources/api/apiserver/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ mod error;
pub use error::Error;

use crate::datastore::{Committed, FilesystemDataStore, Key, Value};
use actix_web::{error::ResponseError, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web::{
error::ResponseError, web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder,
};
use bottlerocket_release::BottlerocketRelease;
use error::Result;
use futures::future;
Expand Down Expand Up @@ -64,6 +66,17 @@ where

let http_server = HttpServer::new(move || {
App::new()
// In our implementation of ResponseError on our own error type below, we include the
// error message in the response for debugging purposes. If actix rejects a request
// early because it doesn't fit our model, though, it doesn't even get to the
// ResponseError implementation. This configuration of the Json extractor allows us to
// add the error message into the response.
.app_data(web::Json::<Settings>::configure(|cfg| {
cfg.error_handler(|err, _req| HttpResponse::BadRequest().body(err.to_string()).into())
}))

// This makes the data store available to API methods merely by having a Data
// parameter.
.app_data(shared_datastore.clone())

// Retrieve the full API model; not all data is writable, so we only support GET.
Expand Down Expand Up @@ -396,7 +409,10 @@ impl ResponseError for error::Error {
SetGroup { .. } => HttpResponse::InternalServerError(),
ReleaseData { .. } => HttpResponse::InternalServerError(),
}
.finish()
// Include the error message in the response, and for all error types. The Bottlerocket
// API is only exposed locally, and only on the host filesystem and to authorized
// containers, so we're not worried about exposing error details.
.body(self.to_string())
}
}

Expand Down