Skip to content

Commit

Permalink
chore: Exception handler에 logging을 추가한다. (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarltj authored Feb 3, 2024
1 parent 2615ed7 commit a254137
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.moneymong.global.exception;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.moneymong.global.constant.MoneymongConstant;
import com.moneymong.global.exception.custom.BusinessException;
import com.moneymong.global.exception.dto.ErrorResponse;
import com.moneymong.global.exception.dto.ErrorResponses;
import com.moneymong.global.exception.enums.ErrorCode;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
Expand All @@ -22,6 +20,10 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

private static final String EXCEPTION_FORMAT = "[EXCEPTION] -----> ";
private static final String EXCEPTION_MESSAGE_FORMAT = "[EXCEPTION] EXCEPTION_MESSAGE -----> [{}]";
private static final String EXCEPTION_TYPE_FORMAT = "[EXCEPTION] EXCEPTION_TYPE -----> [{}]";

@ExceptionHandler(BusinessException.class)
public ResponseEntity<ErrorResponse> handleBusinessException(
final BusinessException exception
Expand All @@ -41,6 +43,8 @@ public ResponseEntity<ErrorResponses> handleMethodArgumentNotValidException(
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.toList();

logWarn(exception);

return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ErrorResponses.of(
Expand All @@ -54,9 +58,21 @@ public ResponseEntity<ErrorResponses> handleMethodArgumentNotValidException(
public ResponseEntity<ErrorResponse> handleInternalException(
final Exception exception
) {
log.info(exception.getMessage());
logError(exception);

return ResponseEntity
.status(MoneymongConstant.INTERNAL_SERVER_ERROR)
.body(ErrorResponse.from(ErrorCode.INTERNAL_SERVER));
}

private void logError(Exception e) {
log.error(EXCEPTION_TYPE_FORMAT, e.getClass().getSimpleName());
log.error(EXCEPTION_MESSAGE_FORMAT, e.getMessage());
log.error(EXCEPTION_FORMAT, e);
}

private void logWarn(Exception e) {
log.warn(EXCEPTION_TYPE_FORMAT, e.getClass().getSimpleName());
log.warn(EXCEPTION_MESSAGE_FORMAT, e.getMessage());
}
}

0 comments on commit a254137

Please sign in to comment.