<aside> <img src="https://raw.githubusercontent.com/eirikmadland/notion-icons/master/v5/icon3/ul-github.svg" alt="https://raw.githubusercontent.com/eirikmadland/notion-icons/master/v5/icon3/ul-github.svg" width="40px" /> ์ฐธ์กฐ Github Code
</aside>
DMakerController
์ handleException
๊ตฌํ// DmakerController ์ ์ฉ Exception
@ResponseStatus(value = HttpStatus.CONFLICT)
@ExceptionHandler(DMakerException.class)
public DmakerErrorResponse handleException(DMakerException e, HttpServletRequest request) {
log.error("errorCode : {}, url : {}, message : {}",
e.getDMakerErrorCode(), request.getRequestURI(), e.getDetailMessage());
return DmakerErrorResponse.builder()
.errorCode(e.getDMakerErrorCode())
.errorMessage(e.getDetailMessage())
.build();
}
DmakerErrorResponse
DTO ๊ตฌํ@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DmakerErrorResponse {
private DMakerErrorCode errorCode;
private String errorMessage;
}
[nio-8080-exec-3] c.d.dMaker.controller.DMakerController : errorCode : DUPLICATED_MEMBER_ID, url : /create-developers, message : MemberId๊ฐ ์ค๋ณต๋๋ ๊ฐ๋ฐ์๊ฐ ์์ต๋๋ค.
์๋ฌ ๋ฐ์์ ๋ก๊ทธ ํ์ธ ๊ฐ๋ฅ
์ค๋ณต๋ ๋ฉค๋ฒ ๊ฐ์ ํ๋ ค๊ณ ํ๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ ํ์ธ ๊ฐ๋ฅ
HTTP/1.1 500
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 20 Mar 2022 09:40:27 GMT
Connection: close
{
"errorCode": "DUPLICATED_MEMBER_ID",
"errorMessage": "MemberId๊ฐ ์ค๋ณต๋๋ ๊ฐ๋ฐ์๊ฐ ์์ต๋๋ค."
}
Response file saved.
import javax.servlet.http.HttpServletRequest;
@Slf4j
@RestControllerAdvice
public class DMakerExceptionHandler {
@ResponseStatus(value = HttpStatus.CONFLICT)
@ExceptionHandler(DMakerException.class)
public DmakerErrorResponse handleException(DMakerException e, HttpServletRequest request) {
log.error("errorCode : {}, url : {}, message : {}",
e.getDMakerErrorCode(), request.getRequestURI(), e.getDetailMessage());
return DmakerErrorResponse.builder()
.errorCode(e.getDMakerErrorCode())
.errorMessage(e.getDetailMessage())
.build();
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = {
HttpRequestMethodNotSupportedException.class, // POST, GET ๋งคํ ์ค๋ฅ
MethodArgumentNotValidException.class // @Min(0) -- ์กฐ๊ฑด์ด ๋ง์ด ์์ ๊ฒฝ์ฐ
})
public DmakerErrorResponse handleBadRequest(Exception e, HttpServletRequest request) {
log.error("url : {}, message : {}",
request.getRequestURI(), e.getMessage());
return DmakerErrorResponse.builder()
.errorCode(DMakerErrorCode.INTERNAL_SERVER_ERROR)
.errorMessage(DMakerErrorCode.INTERNAL_SERVER_ERROR.getMessage())
.build();
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public DmakerErrorResponse handleException(Exception e, HttpServletRequest request) {
log.error("url : {}, message : {}",
request.getRequestURI(), e.getMessage());
return DmakerErrorResponse.builder()
.errorCode(DMakerErrorCode.INTERNAL_SERVER_ERROR)
.errorMessage(DMakerErrorCode.INTERNAL_SERVER_ERROR.getMessage())
.build();
}
}
2022-03-20 22:48:43.014 ERROR 19559 --- [nio-8080-exec-4] c.d.d.Exception.DMakerExceptionHandler : url : /create-developers, message : Request method 'PUT' not supported
Disconnected from the target VM, address: '127.0.0.1:51127', transport: 'socket'
HTTP/1.1 500
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 20 Mar 2022 13:48:43 GMT
Connection: close
{
"errorCode": "INTERNAL_SERVER_ERROR",
"errorMessage": "์๋ฒ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค."
}
Response file saved.