Skip to content

Commit 1dde8c0

Browse files
authored
Merge pull request #9 from DevKor-github/feature/global-handler
Feature/ 예외 처리 클래스 구현
2 parents f4b2935 + c34b7ed commit 1dde8c0

19 files changed

Lines changed: 289 additions & 120 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ out/
3535

3636
### VS Code ###
3737
.vscode/
38+
39+
### 환경변수 ###
40+
.env

src/main/java/com/ODG/ODG_back/controller/VoteController.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,14 @@ public class VoteController {
2020
@PostMapping("/vote")
2121
public ResponseEntity<String> vote(@PathVariable String inviteCode,
2222
@RequestBody VoteRequestDto voteRequest) {
23-
try {
24-
voteService.vote(inviteCode, voteRequest);
25-
return ResponseEntity.ok().body("OK");
26-
} catch (RuntimeException e) {
27-
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
28-
}
29-
30-
23+
voteService.vote(inviteCode, voteRequest);
24+
return ResponseEntity.ok().body("OK");
3125
}
3226

3327
@GetMapping("/result")
3428
public ResponseEntity<?> getVoteResults(@PathVariable String inviteCode) {
35-
try {
36-
List<VoteResultDto> voteResults = voteService.getVoteResults(inviteCode);
37-
return ResponseEntity.ok(voteResults);
38-
} catch (RuntimeException e) {
39-
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
40-
}
29+
List<VoteResultDto> voteResults = voteService.getVoteResults(inviteCode);
30+
return ResponseEntity.ok(voteResults);
4131
}
4232

4333
}

src/main/java/com/ODG/ODG_back/domain/Vote.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.ODG.ODG_back.domain;
22

33
import jakarta.persistence.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
49
import java.time.LocalDateTime;
510

611
@AllArgsConstructor
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.ODG.ODG_back.dto.exception;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class ErrorResponse {
9+
10+
private String code;
11+
private String message;
12+
public ErrorResponse(String code, String message) {
13+
this.code = code;
14+
this.message = message;
15+
}
16+
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.ODG.ODG_back.exception;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public enum ErrorCode {
7+
8+
MEETING_NOT_FOUND("404", "해당 초대 코드의 모임이 존재하지 않습니다."),
9+
PARTICIPANT_NOT_FOUND("404", "해당 참가자가 존재하지 않습니다."),
10+
PLACE_NOT_FOUND("404", "해당 장소가 존재하지 않습니다."),
11+
RECOMMENDED_MIDPOINT_NOT_FOUND("404", "해당 모임에 대한 중간지점이 존재하지 않습니다."),
12+
13+
DATA_INTEGRITY_VIOLATION("400", "중복 혹은 유효하지 않은 데이터입니다."),
14+
BAD_REQUEST("400", "잘못된 요청입니다."),
15+
INVALID_ARGUMENTS("400", "잘못된 인자가 전달되었습니다."),
16+
17+
UNAUTHORIZED("401", "인증되지 않았습니다."),
18+
INTERNAL_SERVER_ERROR("500", "서버 내부 오류가 발생했습니다."),
19+
20+
EXTERNAL_API_CONNECTION_FAILED("503", "외부 API에 연결 실패했습니다."),
21+
EXTERNAL_API_RESPONSE_ERROR("502", "외부 API 응답 상태 오류");
22+
23+
private final String code;
24+
private final String message;
25+
26+
ErrorCode(String code, String message) {
27+
this.code = code;
28+
this.message = message;
29+
}
30+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.ODG.ODG_back.exception;
2+
3+
import com.ODG.ODG_back.dto.exception.ErrorResponse;
4+
import com.ODG.ODG_back.exception.custom.BadRequestException;
5+
import com.ODG.ODG_back.exception.custom.ExternalApiException;
6+
import com.ODG.ODG_back.exception.custom.NotFoundException;
7+
import com.ODG.ODG_back.exception.custom.UnauthorizedException;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.ExceptionHandler;
11+
import org.springframework.web.bind.annotation.RestControllerAdvice;
12+
13+
@RestControllerAdvice
14+
public class GlobalExceptionHandler {
15+
16+
17+
// 404 Not Found
18+
@ExceptionHandler(NotFoundException.class)
19+
public ResponseEntity<ErrorResponse> handlerNotFoundException(NotFoundException e) {
20+
return buildResponse(e.getErrorCode(), HttpStatus.NOT_FOUND);
21+
}
22+
23+
// 400 Bad Request
24+
@ExceptionHandler(BadRequestException.class)
25+
public ResponseEntity<ErrorResponse> handlerBadRequestException(BadRequestException e) {
26+
return buildResponse(e.getErrorCode(), HttpStatus.BAD_REQUEST);
27+
}
28+
29+
// 401 Unauthorized
30+
@ExceptionHandler(UnauthorizedException.class)
31+
public ResponseEntity<ErrorResponse> handlerUnauthorizedException(UnauthorizedException e) {
32+
return buildResponse(e.getErrorCode(), HttpStatus.UNAUTHORIZED);
33+
}
34+
35+
// 500 Internal Server Error
36+
@ExceptionHandler(RuntimeException.class)
37+
public ResponseEntity<ErrorResponse> handlerRuntimeException(RuntimeException e) {
38+
ErrorCode errorCode = ErrorCode.INTERNAL_SERVER_ERROR;
39+
ErrorResponse response = new ErrorResponse(errorCode.getCode(), errorCode.getMessage());
40+
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
41+
}
42+
43+
// 502, 503 External Error
44+
@ExceptionHandler(ExternalApiException.class)
45+
public ResponseEntity<ErrorResponse> handlerExternalApiException(ExternalApiException e) {
46+
return buildResponse(e.getErrorCode(), HttpStatus.valueOf(Integer.parseInt(e.getErrorCode().getCode())));
47+
}
48+
49+
private ResponseEntity<ErrorResponse> buildResponse(ErrorCode errorCode, HttpStatus status) {
50+
return new ResponseEntity<>(new ErrorResponse(errorCode.getCode(), errorCode.getMessage()), status);
51+
}
52+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ODG.ODG_back.exception.custom;
2+
3+
import com.ODG.ODG_back.exception.ErrorCode;
4+
5+
public class BadRequestException extends RuntimeException {
6+
private final ErrorCode errorCode;
7+
8+
public BadRequestException(ErrorCode errorCode) {
9+
super(errorCode.getMessage());
10+
this.errorCode = errorCode;
11+
}
12+
13+
public ErrorCode getErrorCode() {
14+
return errorCode;
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.ODG.ODG_back.exception.custom;
2+
3+
import com.ODG.ODG_back.exception.ErrorCode;
4+
5+
public class ExternalApiException extends RuntimeException {
6+
7+
private final ErrorCode errorCode;
8+
private final String detail;
9+
10+
11+
public ExternalApiException(ErrorCode errorCode, String detail) {
12+
super(errorCode.getMessage() + "[" + detail + "]");
13+
this.errorCode = errorCode;
14+
this.detail = detail;
15+
}
16+
17+
public ErrorCode getErrorCode() {
18+
return errorCode;
19+
}
20+
21+
public String getDetail() {
22+
return detail;
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.ODG.ODG_back.exception.custom;
2+
3+
import com.ODG.ODG_back.exception.ErrorCode;
4+
5+
public class NotFoundException extends RuntimeException {
6+
7+
private final ErrorCode errorCode;
8+
9+
public NotFoundException(ErrorCode errorCode) {
10+
super(errorCode.getMessage());
11+
this.errorCode = errorCode;
12+
}
13+
14+
public ErrorCode getErrorCode() {
15+
return errorCode;
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ODG.ODG_back.exception.custom;
2+
3+
import com.ODG.ODG_back.exception.ErrorCode;
4+
5+
public class UnauthorizedException extends RuntimeException {
6+
private final ErrorCode errorCode;
7+
8+
public UnauthorizedException(ErrorCode errorCode) {
9+
super(errorCode.getMessage());
10+
this.errorCode = errorCode;
11+
}
12+
13+
public ErrorCode getErrorCode() {
14+
return errorCode;
15+
}
16+
}

0 commit comments

Comments
 (0)