Skip to content

Commit a1ea6e8

Browse files
committed
feat: error 핸들링 엔드포인트 추가
- 404, 500 등 에러 상황에 따라 ErrorResponse 타입으로 응답하도록 구현 - 에러 코드 추가
1 parent 291a97d commit a1ea6e8

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dmu.dasom.api.domain.common.exception;
2+
3+
import jakarta.servlet.RequestDispatcher;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import org.springframework.boot.web.servlet.error.ErrorController;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
public class CustomErrorController implements ErrorController {
13+
14+
@RequestMapping("/error")
15+
public ResponseEntity<ErrorResponse> handleError(final HttpServletRequest request) {
16+
// 에러 코드 추출
17+
final Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
18+
final HttpStatus status = statusCode != null ? HttpStatus.valueOf(statusCode) : HttpStatus.INTERNAL_SERVER_ERROR;
19+
20+
// 404 에러
21+
if (status == HttpStatus.NOT_FOUND)
22+
return ResponseEntity.status(status).body(new ErrorResponse(ErrorCode.NOT_FOUND));
23+
24+
// 기타 에러
25+
return ResponseEntity.status(status).body(new ErrorResponse(ErrorCode.INTERNAL_SERVER_ERROR));
26+
}
27+
28+
}

src/main/java/dmu/dasom/api/domain/common/exception/ErrorCode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public enum ErrorCode {
1515
SIGNUP_FAILED(400, "C006", "회원가입에 실패하였습니다."),
1616
ARGUMENT_NOT_VALID(400, "C007", "요청한 값이 올바르지 않습니다."),
1717
TOKEN_NOT_VALID(400, "C008", "토큰이 올바르지 않습니다."),
18+
INTERNAL_SERVER_ERROR(500, "C009", "서버에 문제가 발생하였습니다."),
19+
NOT_FOUND(404, "C010", "해당 리소스를 찾을 수 없습니다.")
1820
;
1921

2022
private final int status;

0 commit comments

Comments
 (0)