Skip to content

Commit

Permalink
Update: 에러로깅 수정 및 swagger uri 씨큐리티 예외 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
BYEONGRYEOL committed Jun 5, 2024
1 parent 32561b2 commit 2640a3a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/gt/genti/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum ErrorCode {
REFRESH_TOKEN_INVALID(HttpStatus.UNAUTHORIZED, ErrorUtils.REFRESH_TOKEN_INVALID, "유효하지 않은 리프레시 토큰입니다."),
TOKEN_CREATION_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, ErrorUtils.TOKEN_CREATION_FAILED, "토큰 생성에 실패했습니다."),
TOKEN_REFRESH_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, ErrorUtils.TOKEN_REFRESH_FAILED, "토큰 갱신에 실패했습니다."),
UnHandledException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorUtils.UnHandledException, "예기치 못한 문제가 발생했습니다."),
UnHandledException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorUtils.UnHandledException,
"예기치 못한 문제가 발생했습니다.\n오류내용 : \n%s"),
ActivePictureGenerateRequestNotExists(HttpStatus.NOT_FOUND, ErrorUtils.ActivePictureGenerateRequestNotExists,
"현재 진행중인 요청이 없습니다."),
FileTypeNotProvided(HttpStatus.BAD_REQUEST, ErrorUtils.FileTypeNotProvided, "파일 형식이 주어지지 않았습니다."),
Expand Down Expand Up @@ -62,9 +63,10 @@ public enum ErrorCode {
"DB -> ENUM 값 불러오기 실패 enum : %s value : %s detail : %s"),
WithDrawnUser(HttpStatus.BAD_REQUEST, ErrorUtils.WithDrawnUser, "탈퇴한 사용자입니다."),
Undefined(HttpStatus.INTERNAL_SERVER_ERROR, ErrorUtils.Undefined, "FOR FE 원래 비즈니스 로직 상 발생하면 안되는 오류입니다. 문의 부탁드립니다."),
ValidationError(HttpStatus.BAD_REQUEST, ErrorUtils.ControllerValidationError,"%s"),
OnlyRequesterCanViewRequest(HttpStatus.FORBIDDEN, ErrorUtils.OnlyRequesterCanViewRequest,"사진생성요청을 요청한 유저만 볼 수 있습니다."),
NotAllowedOauthProvider(HttpStatus.NOT_ACCEPTABLE,ErrorUtils.NotAllowedOauthProvider , "허가되지 않은 oauth type %s");
ValidationError(HttpStatus.BAD_REQUEST, ErrorUtils.ControllerValidationError, "%s"),
OnlyRequesterCanViewRequest(HttpStatus.FORBIDDEN, ErrorUtils.OnlyRequesterCanViewRequest,
"사진생성요청을 요청한 유저만 볼 수 있습니다."),
NotAllowedOauthProvider(HttpStatus.NOT_ACCEPTABLE, ErrorUtils.NotAllowedOauthProvider, "허가되지 않은 oauth type %s");

private final HttpStatusCode httpStatusCode;
private final String code;
Expand Down
32 changes: 15 additions & 17 deletions src/main/java/com/gt/genti/error/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,29 @@ public class GlobalExceptionHandler {
protected ResponseEntity<ApiResult<ExpectedException>> handleExpectedException(final HttpServletRequest request,
final ExpectedException exception) {
log.error("""
[Error] uri : %s \n%s""".formatted(request.getRequestURI(), exception.toString()));
\n[Error] uri : %s \n%s""".formatted(request.getRequestURI(), exception.toString()));
return error(exception);
}

// @ExceptionHandler(RuntimeException.class)
// protected ResponseEntity<ApiResult<?>> handleUnExpectedException(final RuntimeException exception) {
// String error = """
// Class : %s
// Cause : %s
// Message : %s
// StackTrace : %s
// """.formatted(exception.getClass(), exception.getCause(), exception.getMessage(),
// exception.getStackTrace());
// log.error(error);
// return error(ErrorCode.UnHandledException);
// }
@ExceptionHandler(RuntimeException.class)
protected ResponseEntity<ApiResult<ExpectedException>> handleUnExpectedException(final RuntimeException exception) {
String error = """
Class : %s
Cause : %s
Message : %s
StackTrace : %s
""".formatted(exception.getClass(), exception.getCause(), exception.getMessage(),
exception.getStackTrace());
log.error(error);
return error(new ExpectedException(ErrorCode.UnHandledException, error));
}

@ExceptionHandler(WebExchangeBindException.class)
protected ResponseEntity<ApiResult<?>> processValidationError(WebExchangeBindException exception) {
String errorMessage = exception.getBindingResult().getFieldErrors().stream().map(
GlobalExceptionHandler::makeFieldErrorMessage).collect(Collectors.joining());
log.info(exception.getMessage());

// return error(new DynamicException("VALIDATION", errorMessage, HttpStatus.BAD_REQUEST));
return null;
log.error(errorMessage);
return error(exception);
}

// Controller에서 @Min @NotNull 등의 어노테이션 유효성 검사 오류시
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class SecurityConfig {
"/test/**",
"/oauth2",
// swagger
"/swagger-ui.html",
"/swagger-ui/**",
"/swagger-resources/**",
"/v3/api-docs/**",
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/gt/genti/other/util/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.support.WebExchangeBindException;

import com.gt.genti.error.ErrorCode;
import com.gt.genti.error.ExpectedException;
Expand All @@ -19,6 +20,11 @@ public static ResponseEntity<ApiResult<ExpectedException>> error(ExpectedExcepti
exception.getErrorCode().getHttpStatusCode());
}

public static ResponseEntity<ApiResult<?>> error(WebExchangeBindException exception) {
return new ResponseEntity<>(new ApiResult<>(false, null, ErrorCode.ValidationError),
exception.getStatusCode());
}

// public static ResponseEntity<ApiResult<?>> error(DynamicException exception) {
// return new ResponseEntity<>(new ApiResult<>(false, null, exception), exception.getHttpStatusCode());
// }
Expand Down

0 comments on commit 2640a3a

Please sign in to comment.