-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTOR(error) :: GlobalException Log 추가 및 리팩토링
- Loading branch information
1 parent
2eeebb4
commit ddf7e7e
Showing
4 changed files
with
41 additions
and
50 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
64 changes: 27 additions & 37 deletions
64
src/main/java/com/woongeya/zoing/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,62 +1,52 @@ | ||
package com.woongeya.zoing.global.error; | ||
|
||
import com.woongeya.zoing.global.error.exception.ZoingException; | ||
import static org.springframework.http.HttpStatus.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindException; | ||
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 jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.ConstraintViolationException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.woongeya.zoing.global.error.exception.ErrorCode.INTERNAL_SERVER_ERROR; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(ZoingException.class) | ||
public ResponseEntity<?> handleCustomException(ZoingException e) { | ||
return new ResponseEntity<>(new ErrorResponse(e.getErrorCode().getStatus(), e.getErrorCode().getCode(), e.getErrorCode().getMessage()), HttpStatus.valueOf(e.getErrorCode().getStatus())); | ||
@ExceptionHandler(JJoingException.class) | ||
public ResponseEntity<ErrorResponse> handleDefineException(JJoingException e) { | ||
log.warn(e.getMessage() + "\n \t {}", e); | ||
return ResponseEntity.status(e.getStatus()) | ||
.body(new ErrorResponse(e.getStatus().value(), e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(BindException.class) | ||
@ResponseStatus(value = HttpStatus.BAD_REQUEST) | ||
public ResponseEntity<?> bindException(BindException e) { | ||
Map<String, String> errorMap = new HashMap<>(); | ||
|
||
for (FieldError error : e.getFieldErrors()) { | ||
errorMap.put(error.getField(), error.getDefaultMessage()); | ||
} | ||
|
||
return new ResponseEntity<>(errorMap, HttpStatus.BAD_REQUEST); | ||
} | ||
@ExceptionHandler({MethodArgumentNotValidException.class}) | ||
@ResponseStatus(BAD_REQUEST) | ||
public ErrorResponse handleDefineException(MethodArgumentNotValidException e) { | ||
log.warn(e.getMessage() + "\n \t {}", e); | ||
|
||
@ExceptionHandler({ConstraintViolationException.class}) | ||
@ResponseStatus(value = HttpStatus.BAD_REQUEST) | ||
public ResponseEntity<?> handleConstraintViolation(ConstraintViolationException e) { | ||
Map<String, String> errorMap = new HashMap<>(); | ||
String message; | ||
|
||
for (ConstraintViolation<?> violation : e.getConstraintViolations()) { | ||
errorMap.put(violation.getPropertyPath().toString(), violation.getMessage()); | ||
if (e.getFieldError() == null) { | ||
message = ""; | ||
} else { | ||
message = e.getFieldError().getDefaultMessage(); | ||
} | ||
|
||
return new ResponseEntity<>(errorMap, HttpStatus.BAD_REQUEST); | ||
return new ErrorResponse(400, message); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleServerException(Exception ex) { | ||
return new ResponseEntity<>( | ||
new ErrorResponse( | ||
INTERNAL_SERVER_ERROR.getStatus(), | ||
INTERNAL_SERVER_ERROR.getCode(), | ||
INTERNAL_SERVER_ERROR.getMessage() | ||
), | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
); | ||
@ResponseStatus(INTERNAL_SERVER_ERROR) | ||
public ErrorResponse handleDefineException(Exception e) { | ||
log.error(e.getMessage() + "\n \t {}", e); | ||
return new ErrorResponse(500, "서버에서 알 수 없는 에러가 발생했습니다."); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/woongeya/zoing/global/error/JJoingException.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,14 @@ | ||
package com.woongeya.zoing.global.error; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public class JJoingException extends RuntimeException { | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
} |
11 changes: 0 additions & 11 deletions
11
src/main/java/com/woongeya/zoing/global/error/exception/ZoingException.java
This file was deleted.
Oops, something went wrong.