Skip to content

Commit

Permalink
fix: reflect on 지윤's code review (#9, #10)
Browse files Browse the repository at this point in the history
1. ErrorDTO에 detail 부분 삭제
2. 에러 전, log.warn 추가
3. DTO 타입 Record 타입으로 통일
  • Loading branch information
chea-young committed Dec 13, 2023
1 parent 48fc981 commit 0dfc96f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@
import com.compono.ibackend.common.enumType.ErrorCode;
import com.compono.ibackend.common.exception.CustomException;
import lombok.Builder;
import lombok.Data;
import org.springframework.http.ResponseEntity;

@Data
@Builder
public class ErrorDTO {

private int code;
private String msg;
private String detail;
public record ErrorDTO(int code, String msg) {

public static ResponseEntity<ErrorDTO> toResponseEntity(CustomException ex) {
ErrorCode errorType = ex.getErrorCode();
String detail = ex.getDetail();

return ResponseEntity
.status(ex.getStatus())
.body(ErrorDTO.builder()
.code(errorType.getCode())
.msg(errorType.getMsg())
.detail(detail)
.build());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,20 @@
public class CustomException extends RuntimeException {
private final HttpStatus status;
private final ErrorCode errorCode;
private final String detail;

public CustomException(HttpStatus status, ErrorCode errorCode) {
this.status = status;
this.errorCode = errorCode;
this.detail = "";
}

public CustomException(HttpStatus status, ErrorCode errorCode, String detail) {
this.status = status;
this.errorCode = errorCode;
this.detail = detail;
}

public CustomException(HttpStatus status, ErrorCode errorCode, Throwable cause) {
this.status = status;
this.errorCode = errorCode;
this.detail = cause.getMessage();
}

public CustomException(HttpStatus status, CustomException customException) {
this.status = status;
this.errorCode = customException.getErrorCode();
this.detail = customException.getDetail();
}

public CustomException(HttpStatus status, Throwable cause) {
this.status = status;
this.errorCode = ErrorCode.UNKNOWN;
this.detail = cause.getMessage();
}

public CustomException(Exception exception) {
if (exception.getClass() == CustomException.class) {
CustomException customException = (CustomException) exception;
this.status = customException.getStatus();
this.errorCode = customException.getErrorCode();
this.detail = customException.getMessage();
} else {
this.status = HttpStatus.BAD_REQUEST;
this.errorCode = ErrorCode.UNKNOWN;
this.detail = exception.getMessage();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.compono.ibackend.common.handler;

import com.compono.ibackend.common.dto.error.response.ErrorDTO;
import com.compono.ibackend.common.enumType.ErrorCode;
import com.compono.ibackend.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j
@ControllerAdvice
public class CustomExceptionHandler {

@ExceptionHandler(CustomException.class)
protected ResponseEntity<ErrorDTO> handleCustom400Exception(CustomException ex) {
ErrorCode errorCode = ex.getErrorCode();
log.warn(String.format("http-status={%d} code={%d} msg={%s}", ex.getStatus().value(), errorCode.getCode(),
errorCode.getMsg()));

return ErrorDTO.toResponseEntity(ex);
}
}

0 comments on commit 0dfc96f

Please sign in to comment.