-
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.
* feat: 에러를 표준화할 enum 정의 * feat: 에러 응답을 표준화하기위한 예외 클래스 구현 * feat: 표준 응답객체 구현 및 GlobalExceptionHandler구현 * feat: 표준 응답 객체에 created 편의 메서드 추가 * chore: spring starter validation 의존성 추가
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.leets.team2.xclone.common; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.leets.team2.xclone.exception.ErrorInfo; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.FieldError; | ||
|
||
@JsonSerialize | ||
@Builder | ||
@Getter | ||
public class ApiData<T> { | ||
|
||
private Boolean success; | ||
private T data; | ||
private Integer errorCode; | ||
private Object errorMessage; | ||
|
||
public static <T> ResponseEntity<ApiData<T>> successFrom(HttpStatus httpStatus, T data) { | ||
ApiData<T> apiData = ApiData.<T>builder() | ||
.success(true) | ||
.data(data) | ||
.build(); | ||
return ResponseEntity.status(httpStatus).body(apiData); | ||
} | ||
|
||
public static <T> ResponseEntity<ApiData<T>> ok(T data) { | ||
ApiData<T> apiData = ApiData.<T>builder() | ||
.success(true) | ||
.data(data) | ||
.build(); | ||
return ResponseEntity.ok(apiData); | ||
} | ||
|
||
public static <T> ResponseEntity<ApiData<T>> created(T data) { | ||
ApiData<T> apiData = ApiData.<T>builder() | ||
.success(true) | ||
.data(data) | ||
.build(); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(apiData); | ||
} | ||
|
||
public static <T> ResponseEntity<ApiData<T>> validationFailure(List<FieldError> fieldErrors) { | ||
Map<String, String> errors = new HashMap<>(); | ||
fieldErrors.forEach( | ||
fieldError -> errors.put(fieldError.getField(), fieldError.getDefaultMessage())); | ||
|
||
ApiData<T> apiData = ApiData.<T>builder() | ||
.success(false) | ||
.errorCode(ErrorInfo.NOT_VALID_REQUEST.getCode()) | ||
.errorMessage(errors) | ||
.build(); | ||
return ResponseEntity.ok(apiData); | ||
} | ||
|
||
public static ResponseEntity<ApiData<String>> errorFrom(ErrorInfo error) { | ||
ApiData<String> apiData = ApiData.<String>builder() | ||
.success(false) | ||
.errorCode(error.getCode()) | ||
.errorMessage(error.getMessage()) | ||
.build(); | ||
return ResponseEntity.status(error.getStatusCode()).body(apiData); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/leets/team2/xclone/exception/ApplicationException.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.leets.team2.xclone.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ApplicationException extends RuntimeException { | ||
|
||
protected ErrorInfo errorInfo; | ||
|
||
protected ApplicationException(ErrorInfo errorInfo) { | ||
super(errorInfo.getMessage()); | ||
this.errorInfo = errorInfo; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/leets/team2/xclone/exception/ErrorInfo.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,20 @@ | ||
package com.leets.team2.xclone.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorInfo { | ||
|
||
// validation 실패 | ||
NOT_VALID_REQUEST(HttpStatus.BAD_REQUEST, "요청을 검증하는데 실패했습니다.", 9999), | ||
|
||
// Member 영역 | ||
NO_SUCH_MEMBER(HttpStatus.NOT_FOUND, "해당 멤버를 찾을 수 없습니다.", 10001); | ||
|
||
private final HttpStatus statusCode; | ||
private final String message; | ||
private final int code; | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/leets/team2/xclone/exception/handler/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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.leets.team2.xclone.exception.handler; | ||
|
||
import com.leets.team2.xclone.common.ApiData; | ||
import com.leets.team2.xclone.exception.ApplicationException; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
public class GlobalExceptionHandler { | ||
|
||
private static final String INTERNAL_SERVER_ERROR = "서버에 오류가 발생했습니다.\n잠시후 다시 시도해주세요."; | ||
|
||
@ExceptionHandler(ApplicationException.class) | ||
public ResponseEntity<ApiData<String>> handleApplicationException(ApplicationException e) { | ||
return ApiData.errorFrom(e.getErrorInfo()); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(RuntimeException.class) | ||
public String handleAnyRunTimeException(RuntimeException e) { | ||
log.warn("Unexpected Error Occurred", e); | ||
return INTERNAL_SERVER_ERROR; | ||
} | ||
|
||
|
||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(Exception.class) | ||
public String handleAnyCheckedException(Exception e) { | ||
log.error("Unexpected Error Occurred", e); | ||
return INTERNAL_SERVER_ERROR; | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ApiData<String>> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
return ApiData.validationFailure(e.getFieldErrors()); | ||
} | ||
} |