Skip to content

Commit

Permalink
Add route not found exception
Browse files Browse the repository at this point in the history
  • Loading branch information
krabiworld committed Dec 31, 2024
1 parent 9734432 commit c4be0ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import su.foxogram.configs.APIConfig;
import su.foxogram.constants.ExceptionsConstants;
import su.foxogram.dtos.response.ExceptionDTO;
Expand Down Expand Up @@ -36,7 +37,7 @@ public ResponseEntity<ExceptionDTO> handleBaseException(BaseException exception)
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ExceptionDTO> handleHttpMessageNotReadable(HttpMessageNotReadableException exception) {
public ResponseEntity<ExceptionDTO> handleHttpMessageNotReadable() {
return buildErrorResponse(ExceptionsConstants.API.EMPTY_BODY.getValue(), ExceptionsConstants.Messages.REQUEST_BODY_EMPTY.getValue(), HttpStatus.BAD_REQUEST);
}

Expand All @@ -49,6 +50,11 @@ public ResponseEntity<ExceptionDTO> handleValidationException(MethodArgumentNotV
return buildErrorResponse(ExceptionsConstants.API.VALIDATION_ERROR.getValue(), message, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<ExceptionDTO> handleNoHandlerFoundException() {
return buildErrorResponse(ExceptionsConstants.API.ROUTE_NOT_FOUND.getValue(), ExceptionsConstants.Messages.ROUTE_NOT_FOUND.getValue(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ExceptionDTO> handleException(Exception exception) {
String message = exception.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public enum Messages {
USER_CREDENTIALS_IS_INVALID("Invalid password or email"),
USER_EMAIL_VERIFIED("You need to verify your email first"),
USER_NOT_FOUND("Unknown user"),
USER_UNAUTHORIZED("You need to authorize first");
USER_UNAUTHORIZED("You need to authorize first"),
ROUTE_NOT_FOUND("Route not found"),
;

private final String message;

Expand Down Expand Up @@ -112,7 +114,8 @@ public int getValue() {
public enum API {
RATE_LIMIT_EXCEEDED,
EMPTY_BODY,
VALIDATION_ERROR;
VALIDATION_ERROR,
ROUTE_NOT_FOUND;

public int getValue() {
return API_ERROR + this.ordinal();
Expand Down

0 comments on commit c4be0ad

Please sign in to comment.