Skip to content

Commit aebec0a

Browse files
Added RestExceptionHandler#buildErrorResponse
1 parent 3a9b1b2 commit aebec0a

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/main/java/net/javadiscord/javabot/api/exception/RestExceptionHandler.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler {
2222
*/
2323
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
2424
public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatch(@NotNull MethodArgumentTypeMismatchException e) {
25-
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(),
25+
return buildErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(),
2626
String.format("%s should be of type %s", e.getName(), e.getRequiredType() != null ? e.getRequiredType().getName() : ""));
27-
return new ResponseEntity<>(error, error.status());
2827
}
2928

3029
/**
@@ -35,8 +34,8 @@ public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatch(@NotNull M
3534
*/
3635
@ExceptionHandler(InvalidEntityIdException.class)
3736
public ResponseEntity<ErrorResponse> handleInvalidEntityIdException(@NotNull InvalidEntityIdException e) {
38-
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(), "Entity should be of type: " + e.getRequiredEntity().getName());
39-
return new ResponseEntity<>(error, error.status());
37+
return buildErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(),
38+
"Entity should be of type: " + e.getRequiredEntity().getName());
4039
}
4140

4241
/**
@@ -47,8 +46,7 @@ public ResponseEntity<ErrorResponse> handleInvalidEntityIdException(@NotNull Inv
4746
*/
4847
@ExceptionHandler(InternalServerException.class)
4948
public ResponseEntity<ErrorResponse> handleInternalServerException(@NotNull InternalServerException e) {
50-
ErrorResponse error = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getCause().getLocalizedMessage(), e.getLocalizedMessage());
51-
return new ResponseEntity<>(error, error.status());
49+
return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getCause().getLocalizedMessage(), e.getLocalizedMessage());
5250
}
5351

5452
/**
@@ -59,7 +57,11 @@ public ResponseEntity<ErrorResponse> handleInternalServerException(@NotNull Inte
5957
*/
6058
@ExceptionHandler(Exception.class)
6159
public ResponseEntity<ErrorResponse> handleGenericException(@NotNull Exception e) {
62-
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
63-
return new ResponseEntity<>(error, error.status());
60+
return buildErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
61+
}
62+
63+
private @NotNull ResponseEntity<ErrorResponse> buildErrorResponse(HttpStatus status, String message, String... errors) {
64+
ErrorResponse error = new ErrorResponse(status, message, errors);
65+
return new ResponseEntity<>(error, status);
6466
}
6567
}

0 commit comments

Comments
 (0)