From c730f230e0f2d4584551464b21b6c4edebce8b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A5=E1=86=BC=E1=84=8B=E1=85=B5=E1=84=89?= =?UTF-8?q?=E1=85=A1=E1=86=A8?= Date: Fri, 7 May 2021 10:24:31 +0900 Subject: [PATCH] [#6] feat: Create GlobalExceptionHandler --- .../GlobalExceptionHandler.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 BE/baseball/src/main/java/team9/baseball/exceptionHandler/GlobalExceptionHandler.java diff --git a/BE/baseball/src/main/java/team9/baseball/exceptionHandler/GlobalExceptionHandler.java b/BE/baseball/src/main/java/team9/baseball/exceptionHandler/GlobalExceptionHandler.java new file mode 100644 index 000000000..28a23aad0 --- /dev/null +++ b/BE/baseball/src/main/java/team9/baseball/exceptionHandler/GlobalExceptionHandler.java @@ -0,0 +1,23 @@ +package team9.baseball.exceptionHandler; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import team9.baseball.DTO.response.ApiResult; +import team9.baseball.exception.NotFoundException; + +@RestControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(RuntimeException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public ApiResult runtimeExcpetion(Exception ex) { + return ApiResult.failed(ex); + } + + @ExceptionHandler(NotFoundException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public ApiResult notFoundException(Exception ex) { + return ApiResult.failed(ex); + } +}