-
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
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...otstrap/src/main/java/com/flow/sa46lll/fileshield/adapter/in/web/ApiControllerAdvice.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,48 @@ | ||
package com.flow.sa46lll.fileshield.adapter.in.web; | ||
|
||
import com.flow.sa46lll.fileshield.dto.ApiResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
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 | ||
public class ApiControllerAdvice { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ApiControllerAdvice.class); | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
protected <T> ApiResponse<T> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
log.warn("handleMethodArgumentNotValidException: ", e); | ||
return ApiResponse.failure( | ||
e.getBindingResult().getAllErrors().get(0).getDefaultMessage() | ||
); | ||
} | ||
|
||
@ExceptionHandler(HttpMessageNotReadableException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public <T> ApiResponse<T> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { | ||
log.info("handleHttpMessageNotReadableException: ", e); | ||
return ApiResponse.failure(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) | ||
public <T> ApiResponse<T> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { | ||
log.info("handleHttpRequestMethodNotSupportedException: ", e); | ||
return ApiResponse.failure(e.getBody().getTitle()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public <T> ApiResponse<T> handleException(Exception e) { | ||
log.error("handleException: ", e); | ||
return ApiResponse.failure(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
fileshield-bootstrap/src/main/java/com/flow/sa46lll/fileshield/dto/ApiResponse.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,23 @@ | ||
package com.flow.sa46lll.fileshield.dto; | ||
|
||
public record ApiResponse<T>(String code, String message, T data) { | ||
|
||
private static final String SUCCESS = "요청에 성공했습니다."; | ||
private static final String FAIL = "요청에 실패했습니다."; | ||
|
||
public static <T> ApiResponse<T> success() { | ||
return new ApiResponse<>(SUCCESS, null, null); | ||
} | ||
|
||
public static <T> ApiResponse<T> success(T data) { | ||
return new ApiResponse<>(SUCCESS, null, data); | ||
} | ||
|
||
public static <T> ApiResponse<T> failure() { | ||
return new ApiResponse<>(FAIL, null, null); | ||
} | ||
|
||
public static <T> ApiResponse<T> failure(String message) { | ||
return new ApiResponse<>(FAIL, message, null); | ||
} | ||
} |