Skip to content

Commit

Permalink
refactor: exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanzhi33 committed Jul 20, 2024
1 parent 5af36c8 commit 19fd479
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,35 @@
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@RestControllerAdvice
public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ResponseEntity<Result<String>> handleException(Exception e) {
return new ResponseEntity<>(Result.serverError(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
public Result<String> handleException(Exception e) {
return Result.serverError(e.getMessage());
}
@ExceptionHandler(NoResourceFoundException.class)
public ResponseEntity<Result<String>> handleNoResourceFoundException(NoResourceFoundException e) {
return new ResponseEntity<>(Result.error(404, "Not Found"), HttpStatus.NOT_FOUND);
@ResponseStatus(HttpStatus.NOT_FOUND)
public Result<String> handleNoResourceFoundException(NoResourceFoundException e) {
return Result.error(404, "Not Found");
}
@ExceptionHandler({MethodArgumentNotValidException.class, IllegalArgumentException.class})
public ResponseEntity<Result<String>> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Result<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
Result<String> res = Result.clientError(e.getMessage());
FieldError fieldError = e.getFieldError();
if (fieldError != null) {
res = Result.clientError(fieldError.getDefaultMessage());
}
return new ResponseEntity<>(res, HttpStatus.BAD_REQUEST);
return res;
}
@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<Result<String>> handleUnauthorizedException(UnauthorizedException e) {
return new ResponseEntity<>(Result.error(401, e.getMessage()), HttpStatus.UNAUTHORIZED);
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public Result<String> handleUnauthorizedException(UnauthorizedException e) {
return Result.error(401, e.getMessage());
}
}

0 comments on commit 19fd479

Please sign in to comment.