<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>

1. Controller ์˜ˆ์™ธ์ฒ˜๋ฆฌ

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.

2. Global ์˜ˆ์™ธ์ฒ˜๋ฆฌ

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.