From 607bfdabaecfbf6898332ff86d03fb58cc0556b3 Mon Sep 17 00:00:00 2001 From: GordeaS Date: Tue, 23 Apr 2024 22:52:04 +0200 Subject: [PATCH] add response error messages to the logs --- .../EuropeanaGlobalExceptionHandler.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/commons-web/src/main/java/eu/europeana/api/commons/web/exception/EuropeanaGlobalExceptionHandler.java b/commons-web/src/main/java/eu/europeana/api/commons/web/exception/EuropeanaGlobalExceptionHandler.java index 91bbd711..8ab442e6 100644 --- a/commons-web/src/main/java/eu/europeana/api/commons/web/exception/EuropeanaGlobalExceptionHandler.java +++ b/commons-web/src/main/java/eu/europeana/api/commons/web/exception/EuropeanaGlobalExceptionHandler.java @@ -80,16 +80,19 @@ protected boolean stackTraceEnabled(){ @ExceptionHandler(HttpException.class) public ResponseEntity 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); } @@ -129,8 +132,9 @@ public ResponseEntity handleEuropeanaBaseException(Eu @ExceptionHandler(EuropeanaI18nApiException.class) public ResponseEntity handleEuropeanaApiException( EuropeanaI18nApiException e, HttpServletRequest httpRequest) { - logException(e); - return buildApiErrorResponse(e, httpRequest, e.getI18nKey(), e.getI18nParams()); + ResponseEntity response = buildApiErrorResponse(e, httpRequest, e.getI18nKey(), e.getI18nParams()); + LOG.error("Application Error ({}): {}", response.getStatusCode(), response.getBody().getMessage(), e); + return response; } protected ResponseEntity buildApiErrorResponse(EuropeanaApiException e, @@ -164,7 +168,6 @@ protected String buildResponseMessage(Exception e, String i18nKey, String[] i18n */ @ExceptionHandler public ResponseEntity 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()) @@ -172,6 +175,8 @@ public ResponseEntity handleOtherExceptionTypes(Excep .setSeeAlso(seeAlso) .build(); + LOG.error("Unexpected Internal Server Error (500): {}", response.getMessage(), e); + return ResponseEntity .status(responseStatus) .headers(createHttpHeaders(httpRequest)) @@ -212,14 +217,14 @@ public ResponseEntity handleHttpMethodNotSupportedExc */ @ExceptionHandler public ResponseEntity 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)) @@ -231,7 +236,6 @@ public ResponseEntity handleInputValidationError(Cons */ @ExceptionHandler public ResponseEntity 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()) @@ -239,7 +243,8 @@ public ResponseEntity handleInputValidationError(Miss .setMessage(e.getMessage()) .setSeeAlso(seeAlso) .build(); - + + LOG.error("Bad Request error (400): {}", response.getMessage(), e); return ResponseEntity .status(responseStatus) .headers(createHttpHeaders(httpRequest)) @@ -252,12 +257,13 @@ public ResponseEntity handleInputValidationError(Miss @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) public ResponseEntity 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(); @@ -292,7 +298,6 @@ public ResponseEntity handleNoHandlerFoundException( */ @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity handleMethodArgNotValidException(MethodArgumentNotValidException e, HttpServletRequest httpRequest) { - LOG.error("Bad Request Error (400):", e); BindingResult result = e.getBindingResult(); String error =""; List fieldErrors = result.getFieldErrors(); @@ -301,13 +306,16 @@ public ResponseEntity 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))