Skip to content

Commit 8f084a7

Browse files
committed
feat : 예외 처리
1 parent 0812bd7 commit 8f084a7

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package roomescape.advice;
2+
3+
public record ErrorResponse(String message) {
4+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package roomescape.advice;
2+
3+
import org.springframework.data.crossstore.ChangeSetPersister;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.ControllerAdvice;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
8+
@ControllerAdvice
9+
public class GlobalExceptionHandler {
10+
11+
@ExceptionHandler(IllegalArgumentException.class)
12+
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException e) {
13+
return ResponseEntity.badRequest()
14+
.body(new ErrorResponse(e.getMessage()));
15+
}
16+
17+
@ExceptionHandler(ChangeSetPersister.NotFoundException.class)
18+
public ResponseEntity<ErrorResponse> handleNotFoundException(RuntimeException e) {
19+
return ResponseEntity.status(404)
20+
.body(new ErrorResponse(e.getMessage()));
21+
}
22+
23+
@ExceptionHandler(Exception.class)
24+
public ResponseEntity<ErrorResponse> handleException(Exception e) {
25+
return ResponseEntity.internalServerError()
26+
.body(new ErrorResponse("오류가 발생했습니다."));
27+
}
28+
}

0 commit comments

Comments
 (0)