Skip to content

Commit

Permalink
refactor: Spring MVC 기본 에러처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dani820 committed Sep 30, 2024
1 parent 4985f83 commit a76074d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/com/inmybook/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
public enum ErrorCode {
INVALID_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),
INVALID_REQUIRED_PARAMETER(HttpStatus.BAD_REQUEST, "필수 파라미터가 존재하지 않습니다."),
UNAUTHORIZED_ACCESS(HttpStatus.UNAUTHORIZED, "리소스에 접근하려면 인증이 필요합니다."),
FORBIDDEN_ACCESS(HttpStatus.FORBIDDEN, "접근 권한이 없습니다."),
NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 정보입니다."),
NOT_FOUND_HTTP_METHOD(HttpStatus.NOT_FOUND, "존재하지 않는 HTTP 메소드입니다."),
NOT_ALLOWED_HTTP_METHOD(HttpStatus.METHOD_NOT_ALLOWED, "허용되지 않는 HTTP 메소드입니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러가 발생하였습니다. 다시 시도해주세요."),

POST_NOT_FOUND(HttpStatus.NOT_FOUND, "독서록 정보가 존재하지 않습니다."),
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/inmybook/error/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,55 @@
package com.inmybook.error;

import java.nio.file.AccessDeniedException;

import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.inmybook.application.service.exception.PostDeletionFailedException;
import com.inmybook.application.service.exception.PostModificationFailedException;
import com.inmybook.application.service.exception.PostRegistrationFailedException;
import com.inmybook.error.exception.PostNotFoundException;
import com.inmybook.error.exception.UnauthorizedException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(UnauthorizedException.class)
protected ResponseEntity<ErrorResponseDto> handleUnauthorizedException(
UnauthorizedException ex) {
log.error("handleUnauthorizedException", ex);
ErrorCode errorCode = ErrorCode.UNAUTHORIZED_ACCESS;
return ErrorResponseDto.error(errorCode);
}

@ExceptionHandler(AccessDeniedException.class)
protected ResponseEntity<ErrorResponseDto> handleAccessDeniedException(
AccessDeniedException ex) {
log.error("handleAccessDeniedException", ex);
ErrorCode errorCode = ErrorCode.FORBIDDEN_ACCESS;
return ErrorResponseDto.error(errorCode);
}

@ExceptionHandler(NoHandlerFoundException.class)
protected ResponseEntity<ErrorResponseDto> handleNoHandlerFoundException(
NoHandlerFoundException ex) {
log.error("handleNoHandlerFoundException", ex);
ErrorCode errorCode = ErrorCode.NOT_FOUND_HTTP_METHOD;
return ErrorResponseDto.error(errorCode);
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<ErrorResponseDto> handleHttpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException ex) {
log.error("handleHttpRequestMethodNotSupportedException", ex);
ErrorCode errorCode = ErrorCode.NOT_ALLOWED_HTTP_METHOD;
return ErrorResponseDto.error(errorCode);
}

@ExceptionHandler(PostModificationFailedException.class)
protected ResponseEntity<ErrorResponseDto> handlePostModificationFailedException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.inmybook.error.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.UNAUTHORIZED)
public class UnauthorizedException extends RuntimeException {
public UnauthorizedException(String message) {
super(message);
}
}
5 changes: 4 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
spring:
application:
name: inmybook
web:
resources:
add-mappings: false
springdoc:
api-docs:
path: /api-docs
Expand All @@ -12,4 +15,4 @@ springdoc:
path: /swagger-ui.html
disable-swagger-default-url: true
display-query-params-without-oauth2: true
doc-expansion: none
doc-expansion: none

0 comments on commit a76074d

Please sign in to comment.