Skip to content

Commit 3a9b1b2

Browse files
Added generic ExceptionHandler
1 parent cd4a37c commit 3a9b1b2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
99
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
1010

11-
import java.sql.SQLException;
12-
1311
/**
1412
* Handles all Rest Exceptions.
1513
*/
@@ -42,14 +40,26 @@ public ResponseEntity<ErrorResponse> handleInvalidEntityIdException(@NotNull Inv
4240
}
4341

4442
/**
45-
* Handles all {@link SQLException}s.
43+
* Handles all {@link InternalServerException}s.
4644
*
47-
* @param e The {@link SQLException} which was thrown.
45+
* @param e The {@link InternalServerException} which was thrown.
4846
* @return The {@link ResponseEntity} containing the {@link ErrorResponse}.
4947
*/
50-
@ExceptionHandler({ InternalServerException.class })
48+
@ExceptionHandler(InternalServerException.class)
5149
public ResponseEntity<ErrorResponse> handleInternalServerException(@NotNull InternalServerException e) {
52-
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getCause().getLocalizedMessage(), e.getLocalizedMessage());
50+
ErrorResponse error = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getCause().getLocalizedMessage(), e.getLocalizedMessage());
51+
return new ResponseEntity<>(error, error.status());
52+
}
53+
54+
/**
55+
* Handles all generic {@link Exception}.
56+
*
57+
* @param e The {@link Exception} which was thrown.
58+
* @return The {@link ResponseEntity} containing the {@link ErrorResponse}.
59+
*/
60+
@ExceptionHandler(Exception.class)
61+
public ResponseEntity<ErrorResponse> handleGenericException(@NotNull Exception e) {
62+
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
5363
return new ResponseEntity<>(error, error.status());
5464
}
5565
}

0 commit comments

Comments
 (0)