diff --git a/server/src/main/java/server/haengdong/ServerApplication.java b/server/src/main/java/server/haengdong/HaengdongApplication.java similarity index 69% rename from server/src/main/java/server/haengdong/ServerApplication.java rename to server/src/main/java/server/haengdong/HaengdongApplication.java index 784978356..31b6e46e7 100644 --- a/server/src/main/java/server/haengdong/ServerApplication.java +++ b/server/src/main/java/server/haengdong/HaengdongApplication.java @@ -4,10 +4,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class ServerApplication { +public class HaengdongApplication { public static void main(String[] args) { - SpringApplication.run(ServerApplication.class, args); + SpringApplication.run(HaengdongApplication.class, args); } } diff --git a/server/src/main/java/server/haengdong/exception/ErrorResponse.java b/server/src/main/java/server/haengdong/exception/ErrorResponse.java new file mode 100644 index 000000000..5d1e33c9b --- /dev/null +++ b/server/src/main/java/server/haengdong/exception/ErrorResponse.java @@ -0,0 +1,10 @@ +package server.haengdong.exception; + +public record ErrorResponse( + String message +) { + + public static ErrorResponse of(HaengdongErrorCode errorCode) { + return new ErrorResponse(errorCode.getMessage()); + } +} diff --git a/server/src/main/java/server/haengdong/exception/GlobalExceptionHandler.java b/server/src/main/java/server/haengdong/exception/GlobalExceptionHandler.java new file mode 100644 index 000000000..dc5aaf57e --- /dev/null +++ b/server/src/main/java/server/haengdong/exception/GlobalExceptionHandler.java @@ -0,0 +1,31 @@ +package server.haengdong.exception; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public ResponseEntity haengdongException() { + return ResponseEntity.badRequest() + .body(ErrorResponse.of(HaengdongErrorCode.BAD_REQUEST)); + } + + @ExceptionHandler(HaengdongException.class) + public ResponseEntity haengdongException(HaengdongException e) { + return ResponseEntity.status(e.getStatusCode()) + .body(ErrorResponse.of(e.getErrorCode())); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleException(Exception e) { + log.error(e.getMessage(), e); + return ResponseEntity.internalServerError() + .body(ErrorResponse.of(HaengdongErrorCode.INTERNAL_SERVER_ERROR)); + } +} diff --git a/server/src/main/java/server/haengdong/exception/HaengdongErrorCode.java b/server/src/main/java/server/haengdong/exception/HaengdongErrorCode.java new file mode 100644 index 000000000..1b8f632f1 --- /dev/null +++ b/server/src/main/java/server/haengdong/exception/HaengdongErrorCode.java @@ -0,0 +1,20 @@ +package server.haengdong.exception; + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public enum HaengdongErrorCode { + BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), + NOT_FOUND_EVENT(HttpStatus.NOT_FOUND, "존재하지 않는 행사입니다."), + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부에서 에러가 발생했습니다."), + ; + + private final HttpStatus httpStatus; + private final String message; + + HaengdongErrorCode(HttpStatus httpStatus, String message) { + this.httpStatus = httpStatus; + this.message = message; + } +} diff --git a/server/src/main/java/server/haengdong/exception/HaengdongException.java b/server/src/main/java/server/haengdong/exception/HaengdongException.java new file mode 100644 index 000000000..812df50f8 --- /dev/null +++ b/server/src/main/java/server/haengdong/exception/HaengdongException.java @@ -0,0 +1,32 @@ +package server.haengdong.exception; + +import lombok.Getter; +import org.springframework.http.HttpStatusCode; + +@Getter +public class HaengdongException extends RuntimeException { + + private final HaengdongErrorCode errorCode; + private final String message; + + public HaengdongException(HaengdongErrorCode errorCode) { + this(errorCode, null); + } + + public HaengdongException(HaengdongErrorCode errorCode, String message) { + this.errorCode = errorCode; + this.message = message; + } + + public HttpStatusCode getStatusCode() { + return errorCode.getHttpStatus(); + } + + @Override + public String getMessage() { + if (message == null) { + return errorCode.getMessage(); + } + return message; + } +}