-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
f0f17bc
commit 2e30237
Showing
1 changed file
with
48 additions
and
2 deletions.
There are no files selected for viewing
50 changes: 48 additions & 2 deletions
50
src/main/java/com/tukorea/planding/global/error/GlobalExceptionController.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 |
---|---|---|
@@ -1,17 +1,63 @@ | ||
package com.tukorea.planding.global.error; | ||
|
||
import com.fasterxml.jackson.databind.exc.InvalidFormatException; | ||
import com.tukorea.planding.common.CommonResponse; | ||
import com.tukorea.planding.common.CommonUtils; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
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 java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionController { | ||
@ExceptionHandler(value = { BusinessException.class }) | ||
|
||
/* | ||
잘못된 URL 예외 | ||
*/ | ||
@ExceptionHandler(NoHandlerFoundException.class) | ||
public CommonResponse<?> handleNoHandlerFoundException() { | ||
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_URL); | ||
return CommonUtils.fail(errorResponse); | ||
} | ||
|
||
/* | ||
NULL 값 | ||
*/ | ||
@ExceptionHandler(NullPointerException.class) | ||
public CommonResponse<?> handleNoHandlerNullPointException() { | ||
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_URL); | ||
return CommonUtils.fail(errorResponse); | ||
} | ||
|
||
/* | ||
@Valid 핸들러 | ||
*/ | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex) { | ||
Map<String, String> errors = new HashMap<>(); | ||
ex.getBindingResult().getFieldErrors().forEach(error -> | ||
errors.put(error.getField(), error.getDefaultMessage())); | ||
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
/* | ||
ErrorCode 핸들러 | ||
*/ | ||
@ExceptionHandler(value = {BusinessException.class}) | ||
protected CommonResponse<?> handleBusinessException(BusinessException ex) { | ||
final ErrorCode errorCode = ex.getErrorCode(); | ||
final ErrorResponse errorResponse = ErrorResponse.of(errorCode); | ||
return CommonUtils.fail(errorResponse); | ||
} | ||
|
||
@ExceptionHandler(InvalidFormatException.class) | ||
public ResponseEntity<ErrorResponse> handleInvalidFormatException(InvalidFormatException ex) { | ||
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_REQUEST); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); | ||
} | ||
} |