Skip to content

Commit

Permalink
Merge pull request #137 from europeana/EA-3781_add_logging_for_all_ex…
Browse files Browse the repository at this point in the history
…ceptions

add response error messages to the logs
  • Loading branch information
gsergiu authored Apr 23, 2024
2 parents d1fb2d3 + 607bfda commit f67e85d
Showing 1 changed file with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,19 @@ protected boolean stackTraceEnabled(){
@ExceptionHandler(HttpException.class)
public ResponseEntity<EuropeanaApiErrorResponse> handleCommonHttpException(
HttpException e, HttpServletRequest httpRequest) {
LOG.error("Error response: ", e);
final String errorMessage = buildResponseMessage(e, e.getI18nKey(), e.getI18nParams());
EuropeanaApiErrorResponse response =
new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled())
.setStatus(e.getStatus().value())
.setError(e.getStatus().getReasonPhrase())
.setMessage( buildResponseMessage(e, e.getI18nKey(), e.getI18nParams()))
.setMessage( errorMessage)
// code only included in JSON if a value is set in exception
.setCode(getNormalizedErrorCode(e.getI18nKey()))
.setSeeAlso(getSeeAlso())
.build();

LOG.error("Error response ({}): {}", e.getStatus().value(), errorMessage, e);

return ResponseEntity.status(e.getStatus()).headers(createHttpHeaders(httpRequest))
.body(response);
}
Expand Down Expand Up @@ -129,8 +132,9 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleEuropeanaBaseException(Eu
@ExceptionHandler(EuropeanaI18nApiException.class)
public ResponseEntity<EuropeanaApiErrorResponse> handleEuropeanaApiException(
EuropeanaI18nApiException e, HttpServletRequest httpRequest) {
logException(e);
return buildApiErrorResponse(e, httpRequest, e.getI18nKey(), e.getI18nParams());
ResponseEntity<EuropeanaApiErrorResponse> response = buildApiErrorResponse(e, httpRequest, e.getI18nKey(), e.getI18nParams());
LOG.error("Application Error ({}): {}", response.getStatusCode(), response.getBody().getMessage(), e);
return response;
}

protected ResponseEntity<EuropeanaApiErrorResponse> buildApiErrorResponse(EuropeanaApiException e,
Expand Down Expand Up @@ -164,14 +168,15 @@ protected String buildResponseMessage(Exception e, String i18nKey, String[] i18n
*/
@ExceptionHandler
public ResponseEntity<EuropeanaApiErrorResponse> handleOtherExceptionTypes(Exception e, HttpServletRequest httpRequest) {
LOG.error("Unexpected Internal Server Error:", e);
HttpStatus responseStatus = HttpStatus.INTERNAL_SERVER_ERROR;
EuropeanaApiErrorResponse response = new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled())
.setStatus(responseStatus.value())
.setError(responseStatus.getReasonPhrase())
.setSeeAlso(seeAlso)
.build();

LOG.error("Unexpected Internal Server Error (500): {}", response.getMessage(), e);

return ResponseEntity
.status(responseStatus)
.headers(createHttpHeaders(httpRequest))
Expand Down Expand Up @@ -212,14 +217,14 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleHttpMethodNotSupportedExc
*/
@ExceptionHandler
public ResponseEntity<EuropeanaApiErrorResponse> handleInputValidationError(ConstraintViolationException e, HttpServletRequest httpRequest) {
LOG.error("Bad Request Error (400):", e);
HttpStatus responseStatus = HttpStatus.BAD_REQUEST;
EuropeanaApiErrorResponse response = new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled())
.setStatus(responseStatus.value())
.setError(responseStatus.getReasonPhrase())
.setMessage(e.getMessage())
.build();


LOG.error("Bad Request error (400): {}", response.getMessage(), e);
return ResponseEntity
.status(responseStatus)
.headers(createHttpHeaders(httpRequest))
Expand All @@ -231,15 +236,15 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleInputValidationError(Cons
*/
@ExceptionHandler
public ResponseEntity<EuropeanaApiErrorResponse> handleInputValidationError(MissingServletRequestParameterException e, HttpServletRequest httpRequest) {
LOG.error("Bad Request Error (400):", e);
HttpStatus responseStatus = HttpStatus.BAD_REQUEST;
EuropeanaApiErrorResponse response = (new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled()))
.setStatus(responseStatus.value())
.setError(responseStatus.getReasonPhrase())
.setMessage(e.getMessage())
.setSeeAlso(seeAlso)
.build();


LOG.error("Bad Request error (400): {}", response.getMessage(), e);
return ResponseEntity
.status(responseStatus)
.headers(createHttpHeaders(httpRequest))
Expand All @@ -252,12 +257,13 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleInputValidationError(Miss
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public ResponseEntity<EuropeanaApiErrorResponse> handleMediaTypeNotAcceptableException(
HttpMediaTypeNotAcceptableException e, HttpServletRequest httpRequest) {
LOG.error("Media Format not Acceptable Error (406):", e);
final String message = "Server could not generate a response that is acceptable by the client";
LOG.error("Media Format not Acceptable Error (406): {}", message, e);
HttpStatus responseStatus = HttpStatus.NOT_ACCEPTABLE;
EuropeanaApiErrorResponse response = new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled())
.setStatus(responseStatus.value())
.setError(responseStatus.getReasonPhrase())
.setMessage("Server could not generate a response that is acceptable by the client")
.setMessage(message)
.setSeeAlso(seeAlso)
.build();

Expand Down Expand Up @@ -292,7 +298,6 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleNoHandlerFoundException(
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<EuropeanaApiErrorResponse> handleMethodArgNotValidException(MethodArgumentNotValidException e, HttpServletRequest httpRequest) {
LOG.error("Bad Request Error (400):", e);
BindingResult result = e.getBindingResult();
String error ="";
List<FieldError> fieldErrors = result.getFieldErrors();
Expand All @@ -301,13 +306,16 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleMethodArgNotValidExceptio
error = fieldErrors.get(0).getField() + " " + fieldErrors.get(0).getDefaultMessage();
}
HttpStatus responseStatus = HttpStatus.BAD_REQUEST;
final String message = "Invalid request body";
EuropeanaApiErrorResponse response = new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled())
.setStatus(responseStatus.value())
.setMessage("Invalid request body")
.setMessage(message)
.setError(error)
.setSeeAlso(seeAlso)
.build();


LOG.error("Bad Request Error (400): {}", message, e);

return ResponseEntity
.status(responseStatus)
.headers(createHttpHeaders(httpRequest))
Expand Down

0 comments on commit f67e85d

Please sign in to comment.