-
Notifications
You must be signed in to change notification settings - Fork 1
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
10 changed files
with
101 additions
and
62 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
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
52 changes: 43 additions & 9 deletions
52
contents/joonseo/todo/src/main/java/org/todo/todo/global/error/GlobalExceptionHandler.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,38 +1,72 @@ | ||
package org.todo.todo.global.error; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpStatusCode; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.todo.todo.global.error.code.CustomErrorCode; | ||
import org.todo.todo.global.error.code.ErrorCode; | ||
import org.todo.todo.global.error.exception.RestApiException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler{ | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(RestApiException.class) | ||
public ResponseEntity<Object> handleRestApiException(RestApiException e){ | ||
public ResponseEntity<ErrorResponse<String>> handleRestApiException(RestApiException e){ | ||
ErrorCode errorCode = e.getErrorCode(); | ||
return handleExceptionInternal(errorCode); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ErrorResponse<List<String>>> handleValidException(MethodArgumentNotValidException e) { | ||
BindingResult result = e.getBindingResult(); | ||
List<String> errorMessages = new ArrayList<>(); | ||
|
||
log.error("@Valid Exception occur with below parameter"); | ||
for (FieldError error : result.getFieldErrors()){ | ||
String errorMessage = "[ " + error.getField() + " ]" + | ||
"[ " + error.getDefaultMessage() + " ]" + | ||
"[ " + error.getRejectedValue() + " ]"; | ||
errorMessages.add(errorMessage); | ||
} | ||
|
||
return handleExceptionInternal(CustomErrorCode.INVALID_PARAMS, errorMessages); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<Object> handleException(Exception e){ | ||
public ResponseEntity<ErrorResponse<String>> handleException(Exception e){ | ||
ErrorCode errorCode = CustomErrorCode.INTERNAL_SERVER_ERROR; | ||
return handleExceptionInternal(errorCode); | ||
} | ||
|
||
private ResponseEntity<Object> handleExceptionInternal(ErrorCode errorCode) { | ||
private ResponseEntity<ErrorResponse<String>> handleExceptionInternal(ErrorCode errorCode) { | ||
return ResponseEntity.status(errorCode.getHttpStatus()) | ||
.body(makeErrorResponse(errorCode)); | ||
} | ||
|
||
public ErrorResponse makeErrorResponse(ErrorCode errorCode){ | ||
return ErrorResponse.builder() | ||
private ResponseEntity<ErrorResponse<List<String>>> handleExceptionInternal(ErrorCode errorCode, List<String> message) { | ||
return ResponseEntity.status(errorCode.getHttpStatus()) | ||
.body(makeErrorResponse(errorCode, message)); | ||
} | ||
|
||
private ErrorResponse<String> makeErrorResponse(ErrorCode errorCode){ | ||
return ErrorResponse.<String>builder() | ||
.error(errorCode.getCode()) | ||
.message(errorCode.getMessage()) | ||
.build(); | ||
} | ||
} | ||
|
||
private ErrorResponse<List<String>> makeErrorResponse(ErrorCode errorCode, List<String> message){ | ||
return ErrorResponse.<List<String>>builder() | ||
.error(errorCode.getCode()) | ||
.message(message) | ||
.build(); | ||
} | ||
} |
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