Skip to content

Commit

Permalink
Merge pull request #40 from gooiman/refactor/error-status-code
Browse files Browse the repository at this point in the history
refactor: HttpStatus가 실제 오류 코드를 반영
  • Loading branch information
EATSTEAK authored Sep 28, 2024
2 parents bf58f51 + 2a7eecd commit e420b53
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.gooiman.server.common.exception;

import dev.gooiman.server.common.dto.ResponseDto;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
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.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
Expand Down Expand Up @@ -64,18 +67,21 @@ public ResponseDto<?> handleArgumentNotValidException(

// 개발자가 직접 정의한 예외
@ExceptionHandler(value = {CommonException.class})
public ResponseDto<?> handleApiException(CommonException e) {
@ResponseBody
public ResponseDto<?> handleApiException(CommonException e, HttpServletResponse response) {
log.error("handleApiException() in GlobalExceptionHandler throw CommonException : {}",
e.getMessage());
response.setStatus(e.getErrorCode().getHttpStatus().value());
return ResponseDto.fail(e);
}

// 서버, DB 예외
@ExceptionHandler(value = {Exception.class})
public ResponseDto<?> handleException(Exception e) {
public ResponseDto<?> handleException(Exception e, HttpServletResponse response) {
log.error("handleException() in GlobalExceptionHandler throw Exception : {}",
e.getMessage());
e.printStackTrace();
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
return ResponseDto.fail(new CommonException(ErrorCode.INTERNAL_SERVER_ERROR));
}
}

0 comments on commit e420b53

Please sign in to comment.