-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- handles IllegalStateException, JpaSystemException and Exceptions
- Loading branch information
1 parent
714618c
commit 7bb2f68
Showing
2 changed files
with
69 additions
and
6 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/com/f_lab/la_planete/controller/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.f_lab.la_planete.controller; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.swagger.v3.oas.annotations.Hidden; | ||
import jakarta.persistence.PessimisticLockException; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hibernate.exception.LockTimeoutException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.orm.jpa.JpaSystemException; | ||
import org.springframework.transaction.TransactionSystemException; | ||
import org.springframework.web.ErrorResponse; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@Hidden | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(IllegalStateException.class) | ||
public ResponseEntity<ErrorResponse> handleIllegalStateException(IllegalStateException e) { | ||
return ResponseEntity | ||
.status(HttpStatus.CONFLICT) | ||
.body(ErrorResponse.of(e.getMessage(), HttpStatus.CONFLICT.value())); | ||
} | ||
|
||
/** | ||
* 데이터베이스 관련 예외 (e.g. 주로 락을 얻는 대기시간이 오래되어서 요청을 반환할 때) | ||
*/ | ||
@ExceptionHandler(JpaSystemException.class) | ||
public ResponseEntity<ErrorResponse> handleJpaSystemException(JpaSystemException e) { | ||
log.error("JpaSystemException occurred", e); | ||
return ResponseEntity | ||
.status(HttpStatus.SERVICE_UNAVAILABLE) | ||
.body(ErrorResponse.of( | ||
"현재 너무 많은 요청을 처리하고 있습니다. 다시 시도해주세요", | ||
HttpStatus.SERVICE_UNAVAILABLE.value())); | ||
} | ||
|
||
/** | ||
* 어플리케이션에서 처리하지 못한 다른 모든 예외 | ||
*/ | ||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleGenericException(Exception e) { | ||
log.error("Unexpected Error occurred. Requires Potential Handling", e); | ||
return ResponseEntity | ||
.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body(ErrorResponse.of( | ||
"알 수 없는 오류가 발생했습니다. 다시 시도해주세요", | ||
HttpStatus.INTERNAL_SERVER_ERROR.value())); | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
static class ErrorResponse { | ||
private String message; | ||
|
||
@JsonProperty("status_code") | ||
private int statusCode; | ||
|
||
public static ErrorResponse of(String message, int statusCode) { | ||
return new ErrorResponse(message, statusCode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters