-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
325 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...lauwe/errorhandlingspringbootstarter/handler/HandlerMethodValidationExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.github.wimdeblauwe.errorhandlingspringbootstarter.handler; | ||
|
||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.ApiErrorResponse; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.ApiFieldError; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.ApiGlobalError; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.ErrorCodeMapper; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.ErrorMessageMapper; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.HttpStatusMapper; | ||
import org.springframework.context.MessageSourceResolvable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.method.annotation.HandlerMethodValidationException; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class HandlerMethodValidationExceptionHandler extends AbstractApiExceptionHandler { | ||
|
||
public HandlerMethodValidationExceptionHandler(HttpStatusMapper httpStatusMapper, | ||
ErrorCodeMapper errorCodeMapper, | ||
ErrorMessageMapper errorMessageMapper) { | ||
|
||
super(httpStatusMapper, errorCodeMapper, errorMessageMapper); | ||
} | ||
|
||
@Override | ||
public boolean canHandle(Throwable exception) { | ||
return exception instanceof HandlerMethodValidationException; | ||
} | ||
|
||
@Override | ||
public ApiErrorResponse handle(Throwable ex) { | ||
var response = new ApiErrorResponse(HttpStatus.BAD_REQUEST, getErrorCode(ex), getErrorMessage(ex)); | ||
var validationException = (HandlerMethodValidationException) ex; | ||
List<? extends MessageSourceResolvable> errors = validationException.getAllErrors(); | ||
|
||
errors.forEach(error -> { | ||
if (error instanceof FieldError fieldError) { | ||
var apiFieldError = new ApiFieldError( | ||
errorCodeMapper.getErrorCode(fieldError.getCode()), | ||
fieldError.getField(), | ||
errorMessageMapper.getErrorMessage(fieldError.getCode(), fieldError.getDefaultMessage()), | ||
fieldError.getRejectedValue(), | ||
null); | ||
response.addFieldError(apiFieldError); | ||
} else { | ||
var lastCode = Optional.ofNullable(error.getCodes()) | ||
.filter(codes -> codes.length > 0) | ||
.map(codes -> codes[codes.length - 1]) | ||
.orElse(null); | ||
var apiGlobalErrorMessage = new ApiGlobalError( | ||
errorCodeMapper.getErrorCode(lastCode), | ||
errorMessageMapper.getErrorMessage(lastCode, error.getDefaultMessage())); | ||
response.addGlobalError(apiGlobalErrorMessage); | ||
} | ||
}); | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.