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

#763 ApiExceptionHandler - Missing handling generic, 500 error #808

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ResponseEntity<ErrorVm> handleBadRequestException(BadRequestException ex,
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers,
protected ResponseEntity<ErrorVm> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {
List<String> errors = ex.getBindingResult()
.getFieldErrors()
Expand All @@ -47,6 +47,16 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
return ResponseEntity.badRequest().body(errorVm);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorVm> handleOtherException(Exception ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 500, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.INTERNAL_SERVER_ERROR);
}

private String getServletPath(WebRequest webRequest) {
ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
return servletRequest.getRequest().getServletPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,24 @@ public ResponseEntity<ErrorVm> handleWrongEmailFormatException(WrongEmailFormatE
}

@ExceptionHandler(CreateGuestUserException.class)
public ResponseEntity<ErrorVm> CreateGuestUserException(CreateGuestUserException ex, WebRequest request) {
public ResponseEntity<ErrorVm> handleCreateGuestUserException(CreateGuestUserException ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.BAD_REQUEST.toString(), "Bad request", message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 500, message);
log.debug(ex.toString());
return ResponseEntity.badRequest().body(errorVm);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorVm> handleOtherException(Exception ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 500, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.INTERNAL_SERVER_ERROR);
}

private String getServletPath(WebRequest webRequest) {
ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
return servletRequest.getRequest().getServletPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ public ResponseEntity<ErrorVm> handleNotFoundException(NotFoundException ex, Web
}

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorVm> handleBadRequestException(BadRequestException ex,
WebRequest request) {
public ResponseEntity<ErrorVm> handleBadRequestException(BadRequestException ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.BAD_REQUEST.toString(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), message);
return ResponseEntity.badRequest().body(errorVm);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex) {
protected ResponseEntity<ErrorVm> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
Expand All @@ -56,7 +54,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(
}

@ExceptionHandler({ConstraintViolationException.class})
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex) {
public ResponseEntity<ErrorVm> handleConstraintViolation(ConstraintViolationException ex) {
List<String> errors = new ArrayList<>();
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
errors.add(violation.getRootBeanClass().getName() + " " +
Expand All @@ -69,26 +67,20 @@ public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationExcep
}

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<?> handleDataIntegrityViolationException(
DataIntegrityViolationException e) {
public ResponseEntity<ErrorVm> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
String message = NestedExceptionUtils.getMostSpecificCause(e).getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.BAD_REQUEST.toString(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), message);
return ResponseEntity.badRequest().body(errorVm);
}

@ExceptionHandler(DuplicatedException.class)
protected ResponseEntity<Object> handleDuplicated(DuplicatedException e) {
protected ResponseEntity<ErrorVm> handleDuplicated(DuplicatedException e) {
ErrorVm errorVm = new ErrorVm(HttpStatus.BAD_REQUEST.toString(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), e.getMessage());
return ResponseEntity.badRequest().body(errorVm);
}

private String getServletPath(WebRequest webRequest) {
ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
return servletRequest.getRequest().getServletPath();
}

@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ErrorVm> handleAccessDeniedException(AccessDeniedException ex, WebRequest request) {
String message = ex.getMessage();
Expand All @@ -97,4 +89,20 @@ public ResponseEntity<ErrorVm> handleAccessDeniedException(AccessDeniedException
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.FORBIDDEN);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorVm> handleOtherException(Exception ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 500, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.INTERNAL_SERVER_ERROR);
}

private String getServletPath(WebRequest webRequest) {
ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
return servletRequest.getRequest().getServletPath();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public ResponseEntity<ErrorVm> handleBadRequestException(BadRequestException ex,
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex) {
protected ResponseEntity<ErrorVm> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
Expand All @@ -56,7 +55,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(
}

@ExceptionHandler({ConstraintViolationException.class})
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex) {
public ResponseEntity<ErrorVm> handleConstraintViolation(ConstraintViolationException ex) {
List<String> errors = new ArrayList<>();
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
errors.add(violation.getRootBeanClass().getName() + " " +
Expand All @@ -69,21 +68,30 @@ public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationExcep
}

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<?> handleDataIntegrityViolationException(
DataIntegrityViolationException e) {
public ResponseEntity<ErrorVm> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
String message = NestedExceptionUtils.getMostSpecificCause(e).getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.BAD_REQUEST.toString(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), message);
return ResponseEntity.badRequest().body(errorVm);
}

@ExceptionHandler(DuplicatedException.class)
protected ResponseEntity<Object> handleDuplicated(DuplicatedException e) {
protected ResponseEntity<ErrorVm> handleDuplicated(DuplicatedException e) {
ErrorVm errorVm = new ErrorVm(HttpStatus.BAD_REQUEST.toString(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), e.getMessage());
return ResponseEntity.badRequest().body(errorVm);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorVm> handleOtherException(Exception ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 500, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.INTERNAL_SERVER_ERROR);
}

private String getServletPath(WebRequest webRequest) {
ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
return servletRequest.getRequest().getServletPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ public ResponseEntity<ErrorVm> handleConstraintViolation(ConstraintViolationExce
ErrorVm errorVm = new ErrorVm("400", "Bad Request", "Request information is not valid", errors);
return ResponseEntity.badRequest().body(errorVm);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorVm> handleOtherException(Exception ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), message);
return new ResponseEntity<>(errorVm, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ResponseEntity<ErrorVm> handleBadRequestException(BadRequestException ex,
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
protected ResponseEntity<ErrorVm> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
Expand All @@ -49,7 +49,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
}

@ExceptionHandler({ ConstraintViolationException.class })
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex) {
public ResponseEntity<ErrorVm> handleConstraintViolation(ConstraintViolationException ex) {
List<String> errors = new ArrayList<>();
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
errors.add(violation.getRootBeanClass().getName() + " " +
Expand All @@ -61,21 +61,31 @@ public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationExcep
}

@ExceptionHandler({ SignInRequiredException.class })
public ResponseEntity<Object> handleSignInRequired(SignInRequiredException ex) {
public ResponseEntity<ErrorVm> handleSignInRequired(SignInRequiredException ex) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm("403", "Authentication required", message);
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorVm);
}

@ExceptionHandler({ Forbidden.class })
public ResponseEntity<Object> handleForbidden(NotFoundException ex, WebRequest request) {
public ResponseEntity<ErrorVm> handleForbidden(NotFoundException ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.FORBIDDEN.toString(), "Forbidden", message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 403, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.FORBIDDEN);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorVm> handleOtherException(Exception ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 500, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.INTERNAL_SERVER_ERROR);
}

private String getServletPath(WebRequest webRequest) {
ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
return servletRequest.getRequest().getServletPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ResponseEntity<ErrorVm> handleBadRequestException(BadRequestException ex,
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
protected ResponseEntity<ErrorVm> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
Expand All @@ -49,7 +49,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
}

@ExceptionHandler({ ConstraintViolationException.class })
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex) {
public ResponseEntity<ErrorVm> handleConstraintViolation(ConstraintViolationException ex) {
List<String> errors = new ArrayList<>();
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
errors.add(violation.getRootBeanClass().getName() + " " +
Expand All @@ -61,21 +61,31 @@ public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationExcep
}

@ExceptionHandler({ SignInRequiredException.class })
public ResponseEntity<Object> handleSignInRequired(SignInRequiredException ex) {
public ResponseEntity<ErrorVm> handleSignInRequired(SignInRequiredException ex) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm("403", "Authentication required", message);
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorVm);
}

@ExceptionHandler({ ForbiddenException.class })
public ResponseEntity<Object> handleForbidden(NotFoundException ex, WebRequest request) {
public ResponseEntity<ErrorVm> handleForbidden(NotFoundException ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.FORBIDDEN.toString(), "Forbidden", message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 403, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.FORBIDDEN);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorVm> handleOtherException(Exception ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 500, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.INTERNAL_SERVER_ERROR);
}

private String getServletPath(WebRequest webRequest) {
ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
return servletRequest.getRequest().getServletPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ResponseEntity<ErrorVm> handleBadRequestException(BadRequestException ex,
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
protected ResponseEntity<ErrorVm> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
Expand All @@ -49,7 +49,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
}

@ExceptionHandler({ ConstraintViolationException.class })
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex) {
public ResponseEntity<ErrorVm> handleConstraintViolation(ConstraintViolationException ex) {
List<String> errors = new ArrayList<>();
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
errors.add(violation.getRootBeanClass().getName() + " " +
Expand All @@ -60,6 +60,15 @@ public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationExcep
return ResponseEntity.badRequest().body(errorVm);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorVm> handleOtherException(Exception ex, WebRequest request) {
String message = ex.getMessage();
ErrorVm errorVm = new ErrorVm(HttpStatus.INTERNAL_SERVER_ERROR.toString(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), message);
log.warn(ERROR_LOG_FORMAT, this.getServletPath(request), 500, message);
log.debug(ex.toString());
return new ResponseEntity<>(errorVm, HttpStatus.INTERNAL_SERVER_ERROR);
}

private String getServletPath(WebRequest webRequest) {
ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
Expand Down
Loading