Skip to content

Commit

Permalink
Merge pull request #40 from Anifriends/feat/#39
Browse files Browse the repository at this point in the history
feat: Exception에 예외코드를 추가한다.
  • Loading branch information
funnysunny08 authored Oct 31, 2023
2 parents 1786f59 + b985fe0 commit e935a65
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.clova.anifriends.global.exception;

public abstract class AniFriendsException extends RuntimeException {

private final ErrorCode errorCode;

protected AniFriendsException(ErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}

public String getErrorCode() {
return errorCode.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.clova.anifriends.global.exception;

public abstract class AuthenticationException extends RuntimeException {
public abstract class AuthenticationException extends AniFriendsException {

protected AuthenticationException(String message) {
super(message);
protected AuthenticationException(ErrorCode errorCode, String message) {
super(errorCode, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.clova.anifriends.global.exception;

public abstract class AuthorizationException extends RuntimeException {
public abstract class AuthorizationException extends AniFriendsException {

protected AuthorizationException(String message) {
super(message);
protected AuthorizationException(ErrorCode errorCode, String message) {
super(errorCode, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.clova.anifriends.global.exception;

public abstract class BadRequestException extends RuntimeException{
public abstract class BadRequestException extends AniFriendsException {

protected BadRequestException(String message) {
super(message);
protected BadRequestException(ErrorCode errorCode, String message) {
super(errorCode, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.clova.anifriends.global.exception;

public abstract class ConflictException extends RuntimeException {
public abstract class ConflictException extends AniFriendsException {

protected ConflictException(String message) {
super(message);
protected ConflictException(ErrorCode errorCode, String message) {
super(errorCode, message);
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/clova/anifriends/global/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.clova.anifriends.global.exception;

import com.clova.anifriends.EnumType;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum ErrorCode implements EnumType {

// 400
BAD_REQUEST("AF001"), // 잘못된 입력값
INVALID_AUTH_INFO("AF002"), // 아이디 비밀번호 매치 안됨
// 401
TOKEN_EXPIRED("AF101"), // 토큰 만료
UN_AUTHENTICATION("AF102"), // 인증 안됨
// 403
UN_AUTHORIZATION("AF301"), // 권한 없음
// 404
NOT_FOUND("AF401"), // 존재하지 않는 리소스
// 409
ALREADY_EXISTS("AF901"), // 이미 존재하는 리소스
CONCURRENCY("AF902"), // 선착순 마감
// 500
INTERNAL_SERVER_ERROR("AF999"); // 서버 내부 에러

private final String value;

@Override
public String getName() {
return name();
}

@Override
public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.clova.anifriends.global.exception;

public record ErrorResponse(String message) {
public record ErrorResponse(String errorCode, String message) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

import java.util.Objects;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

Expand All @@ -15,26 +20,56 @@ public class GlobalExceptionHandler {

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponse> badRequestEx(BadRequestException e) {
return ResponseEntity.status(BAD_REQUEST).body(new ErrorResponse(e.getMessage()));
return ResponseEntity.status(BAD_REQUEST)
.body(new ErrorResponse(e.getErrorCode(), e.getMessage()));
}

@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<ErrorResponse> authenticationEx(AuthenticationException e) {
return ResponseEntity.status(UNAUTHORIZED).body(new ErrorResponse(e.getMessage()));
return ResponseEntity.status(UNAUTHORIZED)
.body(new ErrorResponse(e.getErrorCode(), e.getMessage()));
}

@ExceptionHandler(AuthorizationException.class)
public ResponseEntity<ErrorResponse> authorizationEx(AuthorizationException e) {
return ResponseEntity.status(FORBIDDEN).body(new ErrorResponse(e.getMessage()));
return ResponseEntity.status(FORBIDDEN)
.body(new ErrorResponse(e.getErrorCode(), e.getMessage()));
}

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ErrorResponse> notFoundEx(NotFoundException e) {
return ResponseEntity.status(NOT_FOUND).body(new ErrorResponse(e.getMessage()));
return ResponseEntity.status(NOT_FOUND)
.body(new ErrorResponse(e.getErrorCode(), e.getMessage()));
}

@ExceptionHandler(ConflictException.class)
public ResponseEntity<ErrorResponse> conflictEx(ConflictException e) {
return ResponseEntity.status(CONFLICT).body(new ErrorResponse(e.getMessage()));
return ResponseEntity.status(CONFLICT)
.body(new ErrorResponse(e.getErrorCode(), e.getMessage()));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(
MethodArgumentNotValidException e) {
FieldError fieldError = Objects.requireNonNull(e.getFieldError());
return ResponseEntity.status(BAD_REQUEST)
.body(new ErrorResponse(ErrorCode.BAD_REQUEST.getValue(),
String.format("%s. (%s)", fieldError.getDefaultMessage(), fieldError.getField())));
}

@ExceptionHandler(MissingRequestHeaderException.class)
protected ResponseEntity<ErrorResponse> handleMissingRequestHeaderException(
MissingRequestHeaderException e) {
return ResponseEntity.status(BAD_REQUEST)
.body(new ErrorResponse(ErrorCode.BAD_REQUEST.getValue(),
String.format("%s. (%s)", e.getMessage(), e.getHeaderName())));
}

@ExceptionHandler(MissingServletRequestParameterException.class)
protected ResponseEntity<ErrorResponse> handleMissingRequestParameterException(
MissingServletRequestParameterException e) {
return ResponseEntity.status(BAD_REQUEST)
.body(new ErrorResponse(ErrorCode.BAD_REQUEST.getValue(),
String.format("%s. (%s)", e.getMessage(), e.getParameterName())));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.clova.anifriends.global.exception;

public abstract class NotFoundException extends RuntimeException {
public abstract class NotFoundException extends AniFriendsException {

protected NotFoundException(String message) {
super(message);
protected NotFoundException(ErrorCode errorCode, String message) {
super(errorCode, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ExceptionController {

@GetMapping("/bad-request")
public void badRequest() {
throw new BadRequestException("bad-request") {
throw new BadRequestException(ErrorCode.BAD_REQUEST, "bad-request") {
@Override
public String getMessage() {
return super.getMessage();
Expand All @@ -20,7 +20,7 @@ public String getMessage() {

@GetMapping("/authentication")
public void authentication() {
throw new AuthenticationException("authentication") {
throw new AuthenticationException(ErrorCode.TOKEN_EXPIRED, "authentication") {
@Override
public String getMessage() {
return super.getMessage();
Expand All @@ -30,7 +30,7 @@ public String getMessage() {

@GetMapping("/authorization")
public void authorization() {
throw new AuthorizationException("authorization") {
throw new AuthorizationException(ErrorCode.UN_AUTHORIZATION, "authorization") {
@Override
public String getMessage() {
return super.getMessage();
Expand All @@ -40,7 +40,7 @@ public String getMessage() {

@GetMapping("/not-found")
public void notFound() {
throw new NotFoundException("notFound") {
throw new NotFoundException(ErrorCode.NOT_FOUND, "notFound") {
@Override
public String getMessage() {
return super.getMessage();
Expand All @@ -50,7 +50,7 @@ public String getMessage() {

@GetMapping("/conflict")
public void conflict() {
throw new ConflictException("conflict") {
throw new ConflictException(ErrorCode.ALREADY_EXISTS, "conflict") {
@Override
public String getMessage() {
return super.getMessage();
Expand Down

0 comments on commit e935a65

Please sign in to comment.