Skip to content

Commit

Permalink
feat: (#77) 공통 예외 처리 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
woo-chang committed Jul 19, 2023
1 parent 428293a commit 5eb92f2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.votogether.exception;

public class BadRequestException extends BaseException {

public BadRequestException(final ExceptionType exceptionType) {
super(exceptionType);
}

}
14 changes: 14 additions & 0 deletions backend/src/main/java/com/votogether/exception/BaseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.votogether.exception;

import lombok.Getter;

@Getter
public class BaseException extends RuntimeException {

private final ExceptionType exceptionType;

public BaseException(final ExceptionType exceptionType) {
this.exceptionType = exceptionType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.votogether.exception;

public record ExceptionResponse(int code, String message) {

public static ExceptionResponse from(final BaseException e) {
final ExceptionType exceptionType = e.getExceptionType();
return new ExceptionResponse(exceptionType.getCode(), exceptionType.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.votogether.exception;

public interface ExceptionType {

int getCode();

String getMessage();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.votogether.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public ResponseEntity<ExceptionResponse> handleException(final Exception e) {
return ResponseEntity.internalServerError()
.body(new ExceptionResponse(-9999, "알 수 없는 서버 에러가 발생했습니다."));
}

@ExceptionHandler
public ResponseEntity<ExceptionResponse> handleBadRequestException(final BadRequestException e) {
return ResponseEntity.badRequest()
.body(ExceptionResponse.from(e));
}

@ExceptionHandler
public ResponseEntity<ExceptionResponse> handleNotFoundException(final NotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ExceptionResponse.from(e));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.votogether.exception;

public class NotFoundException extends BaseException {

public NotFoundException(final ExceptionType exceptionType) {
super(exceptionType);
}

}

0 comments on commit 5eb92f2

Please sign in to comment.