Skip to content

Commit

Permalink
[fix] 401, 403 에러 핸들러 추가 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
soeunkk committed Aug 29, 2022
1 parent 2369346 commit 98a06f9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.format.DateTimeParseException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.nadoyagsa.pillaroid.common.exception.ForbiddenException;
import com.nadoyagsa.pillaroid.common.exception.InternalServerException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand All @@ -17,6 +18,7 @@
import com.nadoyagsa.pillaroid.common.exception.BadRequestException;
import com.nadoyagsa.pillaroid.common.exception.ErrorCode;
import com.nadoyagsa.pillaroid.common.exception.NotFoundException;
import com.nadoyagsa.pillaroid.common.exception.UnauthorizedException;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -30,6 +32,43 @@ protected ApiResponse badRequestError(BadRequestException ex) {
return ApiResponse.error(ex.getErrorCode());
}

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {MethodArgumentTypeMismatchException.class})
protected ApiResponse methodArgumentTypeMismatchError(MethodArgumentTypeMismatchException ex) {
log.info(ex.getMessage());
return ApiResponse.error(ErrorCode.BAD_PARAMETER_TYPE);
}

// 유효성 검사 실패 시
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {MethodArgumentNotValidException.class})
protected ApiResponse methodArgumetNotValidError(MethodArgumentNotValidException ex) {
log.info(ex.getMessage());
return ApiResponse.error(ErrorCode.BAD_PARAMETER, ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
}

// JSON 직렬화, 역직렬화 실패 (ex. 타입 변환 실패)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {JsonProcessingException.class})
protected ApiResponse jsonProcessingError(JsonProcessingException ex) {
log.info(ex.getMessage());
return ApiResponse.error(ErrorCode.BAD_PARAMETER);
}

@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
@ExceptionHandler(value = {UnauthorizedException.class})
protected ApiResponse unauthorizedError(UnauthorizedException ex) {
log.info(ex.getErrorCode().getDetail());
return ApiResponse.error(ex.getErrorCode());
}

@ResponseStatus(value = HttpStatus.FORBIDDEN)
@ExceptionHandler(value = {ForbiddenException.class})
protected ApiResponse forbiddenError(ForbiddenException ex) {
log.info(ex.getErrorCode().getDetail());
return ApiResponse.error(ex.getErrorCode());
}

@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(value = {NotFoundException.class})
protected ApiResponse notFoundError(NotFoundException ex) {
Expand All @@ -50,27 +89,4 @@ protected ApiResponse IOError(IOException ex) {
log.error(ex.getMessage());
return ApiResponse.error(ErrorCode.INTERNAL_ERROR);
}

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {MethodArgumentTypeMismatchException.class})
protected ApiResponse methodArgumentTypeMismatchError(MethodArgumentTypeMismatchException ex) {
log.error(ex.getMessage());
return ApiResponse.error(ErrorCode.BAD_PARAMETER_TYPE);
}

// 유효성 검사 실패 시
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {MethodArgumentNotValidException.class})
protected ApiResponse methodArgumetNotValidException(MethodArgumentNotValidException ex) {
log.error(ex.getMessage());
return ApiResponse.error(ErrorCode.BAD_PARAMETER, ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
}

// JSON 직렬화, 역직렬화 실패 (ex. 타입 변환 실패)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {JsonProcessingException.class})
protected ApiResponse jsonProcessingException(JsonProcessingException ex) {
log.error(ex.getMessage());
return ApiResponse.error(ErrorCode.BAD_PARAMETER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public enum ErrorCode {
/* 401 UNAUTHORIZED: 인증 자격 없음 */
UNAUTHORIZED_USER(401, UNAUTHORIZED, "인증된 사용자가 아닙니다."),

/* 403 FORBIDDEN: 권한 없음 */
DELETE_FORBIDDEN(40301, FORBIDDEN, "삭제할 권한이 없습니다."),

/* 404 NOT_FOUND : Resource 를 찾을 수 없음 */
DATA_NOT_FOUND(40403, NOT_FOUND, "해당 정보가 없습니다."),
BARCODE_NOT_FOUND(40401, NOT_FOUND, "해당 바코드에 대한 정보가 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.nadoyagsa.pillaroid.common.exception;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ForbiddenException extends Exception {
public static final ForbiddenException deleteForbidden = new ForbiddenException(ErrorCode.DELETE_FORBIDDEN);

private final ErrorCode errorCode;

public ErrorCode getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nadoyagsa.pillaroid.common.exception.UnauthorizedException;

@Component
public class AuthInterceptor implements HandlerInterceptor {
private final AuthTokenProvider authTokenProvider;
Expand All @@ -18,13 +20,12 @@ public AuthInterceptor(AuthTokenProvider authTokenProvider) {
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String token = request.getHeader("authorization");
if (token != null && authTokenProvider.validateToken(token)) {
return true;
} else {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
return false;
throw UnauthorizedException.UNAUTHORIZED_USER;
}
}
}

0 comments on commit 98a06f9

Please sign in to comment.