Skip to content

Commit 6df3554

Browse files
authored
Merge pull request #39 from YAPP-Github/feat/PRODUCT-111
[Feat] 400/500번대 에러 처리
2 parents a15f447 + eb00151 commit 6df3554

File tree

13 files changed

+179
-25
lines changed

13 files changed

+179
-25
lines changed

build.gradle

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
id("org.sonarqube") version "6.2.0.5505"
1313
}
1414

15-
group = 'time-eat'
15+
group = 'net.eatda'
1616
version = '0.0.1-SNAPSHOT'
1717

1818
java {
@@ -124,14 +124,22 @@ generateSwaggerUI {
124124
}
125125

126126
openapi3 {
127-
servers = [ // 서버 상황에 맞춰 추가 예정
127+
servers = [
128+
{
129+
url = "https://api-dev.eatda.net"
130+
description = "Develop Server"
131+
},
132+
{
133+
url = "https://api.eatda.net"
134+
description = "Production Server"
135+
},
128136
{
129137
url = "http://localhost:8080"
130138
description = "Local Server"
131139
}
132140
]
133-
title = "Time Eat API"
134-
description = "Time Eat API 명세서"
141+
title = "EatDa API"
142+
description = "EatDa API 명세서"
135143
version = "0.0.1"
136144
format = "yaml"
137145
outputDirectory = "build/resources/main/static/docs"

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'time-eat'
1+
rootProject.name = 'eatda'

src/main/java/timeeat/client/oauth/OauthProperties.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package timeeat.client.oauth;
22

3-
import org.springframework.boot.context.properties.ConfigurationProperties;
4-
import lombok.AllArgsConstructor;
53
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
66

77
@Getter
8-
@AllArgsConstructor
98
@ConfigurationProperties(prefix = "oauth")
9+
@RequiredArgsConstructor
1010
public class OauthProperties {
1111

1212
private final String clientId;

src/main/java/timeeat/client/oauth/OauthServerErrorHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import org.springframework.http.client.ClientHttpResponse;
66
import org.springframework.stereotype.Component;
77
import org.springframework.web.client.RestClient.ResponseSpec.ErrorHandler;
8+
import timeeat.exception.BusinessErrorCode;
9+
import timeeat.exception.BusinessException;
810

911
@Component
1012
public class OauthServerErrorHandler implements ErrorHandler {
1113

1214
@Override
1315
public void handle(HttpRequest request, ClientHttpResponse response) throws IOException {
14-
// TODO : 500 에러 처리
15-
throw new RuntimeException("Oauth server error occurred");
16+
throw new BusinessException(BusinessErrorCode.OAUTH_SERVER_ERROR);
1617
}
1718
}

src/main/java/timeeat/config/CorsConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.http.HttpMethod;
66
import org.springframework.web.servlet.config.annotation.CorsRegistry;
77
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8+
import timeeat.exception.InitializeException;
89

910
@Configuration
1011
public class CorsConfig implements WebMvcConfigurer {
@@ -18,11 +19,11 @@ public CorsConfig(CorsProperties corsProperties) {
1819

1920
private void validate(List<String> corsOriginList) {
2021
if (corsOriginList == null || corsOriginList.isEmpty()) {
21-
throw new RuntimeException("Initialization Error: CORS origin cannot be empty.");
22+
throw new InitializeException("CORS origin cannot be empty.");
2223
}
2324
for (String origin : corsOriginList) {
2425
if (origin == null || origin.isBlank()) {
25-
throw new RuntimeException("Initialization Error: CORS origin string cannot be blank.");
26+
throw new InitializeException("CORS origin string cannot be blank.");
2627
}
2728
}
2829
}

src/main/java/timeeat/controller/web/jwt/JwtProperties.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package timeeat.controller.web.jwt;
22

3+
import io.jsonwebtoken.security.Keys;
34
import java.time.Duration;
45
import java.util.Base64;
56
import javax.crypto.SecretKey;
6-
import org.springframework.boot.context.properties.ConfigurationProperties;
7-
import io.jsonwebtoken.security.Keys;
87
import lombok.Getter;
8+
import org.springframework.boot.context.properties.ConfigurationProperties;
9+
import timeeat.exception.InitializeException;
910

1011
@Getter
1112
@ConfigurationProperties(prefix = "jwt")
@@ -29,17 +30,16 @@ public JwtProperties(String secretKey, Duration accessTokenExpiration, Duration
2930

3031
private void validate(String secretKey) {
3132
if (secretKey == null || secretKey.getBytes().length < SECRET_KEY_MIN_BYTES) {
32-
// TODO Initialize error 논의
33-
throw new RuntimeException("JWT secret key must be at least 32 bytes");
33+
throw new InitializeException("JWT secret key must be at least 32 bytes");
3434
}
3535
}
3636

3737
private void validate(Duration expiration) {
3838
if (expiration == null) {
39-
throw new RuntimeException("JWT token duration cannot be null");
39+
throw new InitializeException("JWT token duration cannot be null");
4040
}
4141
if (expiration.isNegative()) {
42-
throw new RuntimeException("JWT token duration must be positive");
42+
throw new InitializeException("JWT token duration must be positive");
4343
}
4444
}
4545

src/main/java/timeeat/exception/BusinessErrorCode.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package timeeat.exception;
22

33
import lombok.Getter;
4+
import org.springframework.http.HttpStatus;
45

56
@Getter
67
public enum BusinessErrorCode {
@@ -41,15 +42,22 @@ public enum BusinessErrorCode {
4142
BOOKMARK_STORE_REQUIRED("BOK004", "북마크 생성 시 가게 정보는 필수입니다."),
4243

4344
// Auth
44-
UNAUTHORIZED_MEMBER("AUTH001", "인증되지 않은 회원입니다."),
45-
EXPIRED_TOKEN("AUTH002", "이미 만료된 토큰입니다."),
45+
UNAUTHORIZED_MEMBER("AUTH001", "인증되지 않은 회원입니다.", HttpStatus.UNAUTHORIZED),
46+
EXPIRED_TOKEN("AUTH002", "이미 만료된 토큰입니다.", HttpStatus.UNAUTHORIZED),
47+
OAUTH_SERVER_ERROR("AUTH003", "OAuth 서버와의 통신 오류입니다.", HttpStatus.INTERNAL_SERVER_ERROR),
4648
;
4749

4850
private final String code;
4951
private final String message;
52+
private final HttpStatus status;
5053

51-
BusinessErrorCode(String code, String message) {
54+
BusinessErrorCode(String code, String message, HttpStatus status) {
5255
this.code = code;
5356
this.message = message;
57+
this.status = status;
58+
}
59+
60+
BusinessErrorCode(String code, String message) {
61+
this(code, message, HttpStatus.BAD_REQUEST);
5462
}
5563
}

src/main/java/timeeat/exception/BusinessException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package timeeat.exception;
22

33
import lombok.Getter;
4+
import org.springframework.http.HttpStatus;
45

56
@Getter
67
public class BusinessException extends RuntimeException {
@@ -10,4 +11,8 @@ public BusinessException(BusinessErrorCode errorCode) {
1011
super(errorCode.getMessage());
1112
this.errorCode = errorCode;
1213
}
14+
15+
public HttpStatus getStatus() {
16+
return errorCode.getStatus();
17+
}
1318
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package timeeat.exception;
2+
3+
public record ErrorResponse(String errorCode, String message) {
4+
5+
public ErrorResponse(BusinessErrorCode errorCode) {
6+
this(errorCode.getCode(), errorCode.getMessage());
7+
}
8+
9+
public ErrorResponse(EtcErrorCode errorCode) {
10+
this(errorCode.getCode(), errorCode.getMessage());
11+
}
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package timeeat.exception;
2+
3+
import lombok.Getter;
4+
import org.springframework.http.HttpStatus;
5+
6+
@Getter
7+
public enum EtcErrorCode {
8+
9+
CLIENT_REQUEST_ERROR("CLIENT001", "클라이언트 요청이 잘못되었습니다.", HttpStatus.BAD_REQUEST),
10+
METHOD_ARGUMENT_TYPE_MISMATCH("CLIENT002", "요청 타입이 일치하지 않습니다.", HttpStatus.BAD_REQUEST),
11+
ALREADY_DISCONNECTED("CLIENT003", "이미 클라이언트에서 요청이 종료되었습니다.", HttpStatus.BAD_REQUEST),
12+
METHOD_NOT_SUPPORTED("CLIENT004", "허용되지 않은 메서드입니다.", HttpStatus.METHOD_NOT_ALLOWED),
13+
MEDIA_TYPE_NOT_SUPPORTED("CLIENT005", "허용되지 않은 미디어 타입입니다.", HttpStatus.UNSUPPORTED_MEDIA_TYPE),
14+
NO_RESOURCE_FOUND("CLIENT006", "요청한 리소스를 찾을 수 없습니다.", HttpStatus.NOT_FOUND),
15+
NO_COOKIE_FOUND("CLIENT007", "필수 쿠키 값이 존재하지 않습니다.", HttpStatus.BAD_REQUEST),
16+
17+
INTERNAL_SERVER_ERROR("SERVER001", "서버 내부 에러가 발생했습니다.", HttpStatus.INTERNAL_SERVER_ERROR),
18+
;
19+
20+
private final String code;
21+
private final String message;
22+
private final HttpStatus status;
23+
24+
EtcErrorCode(String code, String message, HttpStatus status) {
25+
this.code = code;
26+
this.message = message;
27+
this.status = status;
28+
}
29+
}

0 commit comments

Comments
 (0)