Skip to content

Commit 8eeb05b

Browse files
committed
setting:error,global response
1 parent 6426b09 commit 8eeb05b

File tree

7 files changed

+242
-1
lines changed

7 files changed

+242
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cmf.commitField.global.error;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import org.springframework.http.HttpStatus;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public enum ErrorCode {
10+
11+
// Common
12+
INVALID_INPUT_VALUE(HttpStatus.BAD_REQUEST, "제곡된 μž…λ ₯ 값이 μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."),
13+
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "ν—ˆμš©λ˜μ§€ μ•Šμ€ μš”μ²­ λ°©μ‹μž…λ‹ˆλ‹€."),
14+
ENTITY_NOT_FOUND(HttpStatus.BAD_REQUEST, "μš”μ²­ν•œ μ—”ν‹°ν‹°λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
15+
INVALID_TYPE_VALUE(HttpStatus.BAD_REQUEST, "제곡된 κ°’μ˜ νƒ€μž…μ΄ μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."),
16+
ERROR_PARSING_JSON_RESPONSE(HttpStatus.BAD_REQUEST, "JSON 응닡을 νŒŒμ‹±ν•˜λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€."),
17+
MISSING_INPUT_VALUE(HttpStatus.BAD_REQUEST, "ν•„μˆ˜ μž…λ ₯ 값이 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€."),
18+
DATABASE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "λ°μ΄ν„°λ² μ΄μŠ€ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€."),
19+
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "μ„œλ²„ λ‚΄λΆ€ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€."),
20+
21+
// User
22+
NOT_FOUND_USER(HttpStatus.BAD_REQUEST, "ν•΄λ‹Ή μœ μ €κ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€"),
23+
PASSWORD_MISMATCH(HttpStatus.BAD_REQUEST, "λΉ„λ°€λ²ˆν˜Έκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."),
24+
25+
// Auth
26+
ACCESS_DENIED(HttpStatus.UNAUTHORIZED, "μΈμ¦λ˜μ§€ μ•Šμ€ μœ μ €μž…λ‹ˆλ‹€."),
27+
SC_FORBIDDEN(HttpStatus.UNAUTHORIZED, "κΆŒν•œμ΄ μ—†λŠ” μœ μ €μž…λ‹ˆλ‹€."),
28+
INVALID_JWT_SIGNATURE(HttpStatus.UNAUTHORIZED, "μ„œλͺ… 검증에 μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€." ),
29+
ILLEGAL_REGISTRATION_ID(HttpStatus.BAD_REQUEST,"ν•΄λ‹Ή 사항이 μ—†λŠ” 둜그인 κ²½λ‘œμž…λ‹ˆλ‹€."),
30+
31+
TOKEN_EXPIRED(HttpStatus.BAD_REQUEST, "토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€."),
32+
33+
// member
34+
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€");
35+
36+
37+
private final HttpStatus httpStatus;
38+
private final String message;
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmf.commitField.global.error;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import org.springframework.http.ResponseEntity;
6+
7+
import java.time.LocalDateTime;
8+
9+
@Getter
10+
public class ErrorResponse {
11+
private final String timestamp;
12+
private final String error;
13+
private final String message;
14+
15+
@Builder
16+
public ErrorResponse(String timestamp, String error, String message){
17+
this.timestamp = timestamp;
18+
this.error = error;
19+
this.message = message;
20+
}
21+
public static ResponseEntity<ErrorResponse> toResponseEntity(ErrorCode errorCode){
22+
return ResponseEntity
23+
.status(errorCode.getHttpStatus())
24+
.body(ErrorResponse.builder()
25+
.timestamp(LocalDateTime.now().toString())
26+
.error(errorCode.getHttpStatus().name())
27+
.message(errorCode.getMessage())
28+
.build()
29+
);
30+
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cmf.commitField.global.exception;
2+
3+
import cmf.commitField.global.error.ErrorCode;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public class CustomException extends RuntimeException {
10+
private final ErrorCode errorCode;
11+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cmf.commitField.global.exception;
2+
3+
import cmf.commitField.global.error.ErrorCode;
4+
import cmf.commitField.global.error.ErrorResponse;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.hibernate.exception.ConstraintViolationException;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.http.converter.HttpMessageNotReadableException;
9+
import org.springframework.web.HttpRequestMethodNotSupportedException;
10+
import org.springframework.web.bind.MethodArgumentNotValidException;
11+
import org.springframework.web.bind.MissingServletRequestParameterException;
12+
import org.springframework.web.bind.annotation.ExceptionHandler;
13+
import org.springframework.web.bind.annotation.RestControllerAdvice;
14+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
15+
16+
import java.sql.SQLIntegrityConstraintViolationException;
17+
18+
@Slf4j
19+
@RestControllerAdvice
20+
public class ExceptionControllerAdvice {
21+
22+
@ExceptionHandler(value = {ConstraintViolationException.class, MethodArgumentNotValidException.class, MethodArgumentTypeMismatchException.class})
23+
public ResponseEntity<ErrorResponse> handleValidationExceptions(Exception e) {
24+
log.error("Validation Exception: {}", e.getMessage(), e);
25+
return ErrorResponse.toResponseEntity(ErrorCode.INVALID_INPUT_VALUE);
26+
}
27+
28+
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
29+
public ResponseEntity<ErrorResponse> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
30+
log.error("HTTP Method Not Supported: {}", e.getMessage(), e);
31+
return ErrorResponse.toResponseEntity(ErrorCode.METHOD_NOT_ALLOWED);
32+
}
33+
34+
@ExceptionHandler(value = CustomException.class)
35+
protected ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
36+
log.error("Custom Exception: {}", e.getErrorCode(), e);
37+
return ErrorResponse.toResponseEntity(e.getErrorCode());
38+
}
39+
40+
@ExceptionHandler(value = NullPointerException.class)
41+
public ResponseEntity<ErrorResponse> handleNullPointerException(NullPointerException e) {
42+
log.error("Null Pointer Exception: {}", e.getMessage(), e);
43+
return ErrorResponse.toResponseEntity(ErrorCode.MISSING_INPUT_VALUE);
44+
}
45+
46+
@ExceptionHandler(value = MissingServletRequestParameterException.class)
47+
public ResponseEntity<ErrorResponse> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
48+
log.error("Missing Request Parameter: {}", e.getMessage(), e);
49+
return ErrorResponse.toResponseEntity(ErrorCode.MISSING_INPUT_VALUE);
50+
}
51+
52+
@ExceptionHandler(value = HttpMessageNotReadableException.class)
53+
public ResponseEntity<ErrorResponse> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
54+
log.error("Message Not Readable: {}", e.getMessage(), e);
55+
return ErrorResponse.toResponseEntity(ErrorCode.INVALID_INPUT_VALUE);
56+
}
57+
58+
@ExceptionHandler(value = SQLIntegrityConstraintViolationException.class)
59+
public ResponseEntity<ErrorResponse> handleSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
60+
log.error("SQL Integrity Constraint Violation: {}", e.getMessage(), e);
61+
return ErrorResponse.toResponseEntity(ErrorCode.DATABASE_ERROR);
62+
}
63+
64+
@ExceptionHandler(value = Exception.class)
65+
public ResponseEntity<ErrorResponse> handleGeneralException(Exception e) {
66+
log.error("Unhandled Exception: {}", e.getMessage(), e);
67+
return ErrorResponse.toResponseEntity(ErrorCode.INTERNAL_SERVER_ERROR);
68+
}
69+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cmf.commitField.global.globalDto;
2+
3+
import cmf.commitField.global.error.ErrorCode;
4+
import lombok.Getter;
5+
6+
import java.time.LocalDateTime;
7+
8+
@Getter
9+
public class GlobalResponse<T> {
10+
11+
private final LocalDateTime timestamp; // 응닡 생성 μ‹œκ°„
12+
private final int statusCode; // HTTP μƒνƒœ μ½”λ“œ
13+
private final String message; // 응닡 λ©”μ‹œμ§€
14+
private final T data; // 응닡 데이터 (성곡 μ‹œ 데이터, μ‹€νŒ¨ μ‹œ μΆ”κ°€ 정보)
15+
16+
// 성곡 응닡 μƒμ„±μž
17+
private GlobalResponse(GlobalResponseCode responseCode, T data) {
18+
this.timestamp = LocalDateTime.now();
19+
this.statusCode = responseCode.getCode();
20+
this.message = responseCode.getMessage();
21+
this.data = data;
22+
}
23+
24+
// μ—λŸ¬ 응닡 μƒμ„±μž
25+
private GlobalResponse(ErrorCode errorCode, T data) {
26+
this.timestamp = LocalDateTime.now();
27+
this.statusCode = errorCode.getHttpStatus().value();
28+
this.message = errorCode.getMessage();
29+
this.data = data;
30+
}
31+
32+
// 성곡 응닡 (데이터 포함)
33+
public static <T> GlobalResponse<T> success(T data) {
34+
return new GlobalResponse<>(GlobalResponseCode.OK, data);
35+
}
36+
37+
// 성곡 응닡 (데이터 μ—†μŒ)
38+
public static <T> GlobalResponse<T> success() {
39+
return success(null);
40+
}
41+
42+
// μ—λŸ¬ 응닡 (데이터 포함)
43+
public static <T> GlobalResponse<T> error(ErrorCode errorCode, T data) {
44+
return new GlobalResponse<>(errorCode, data);
45+
}
46+
47+
// μ—λŸ¬ 응닡 (데이터 μ—†μŒ)
48+
public static <T> GlobalResponse<T> error(ErrorCode errorCode) {
49+
return error(errorCode, null);
50+
}
51+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmf.commitField.global.globalDto;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@Getter
7+
@RequiredArgsConstructor
8+
public enum GlobalResponseCode {
9+
10+
OK(200, "μš”μ²­μ΄ μ„±κ³΅ν•˜μ˜€μŠ΅λ‹ˆλ‹€."),
11+
BAD_REQUEST(400, "잘λͺ»λœ μš”μ²­μž…λ‹ˆλ‹€."),
12+
NOT_FOUND(404, "찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
13+
INTERNAL_SERVER_ERROR(500, "μ„œλ²„ λ‚΄λΆ€ 였λ₯˜κ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€.");
14+
15+
private final int code; // HTTP μƒνƒœ μ½”λ“œ
16+
private final String message; // 응닡 λ©”μ‹œμ§€
17+
}
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
package cmf.commitField.global.jpa;
22

3+
import jakarta.persistence.GeneratedValue;
4+
import jakarta.persistence.Id;
5+
import lombok.EqualsAndHashCode;
6+
import lombok.Getter;
7+
import org.springframework.data.annotation.CreatedDate;
8+
9+
import java.time.LocalDateTime;
10+
11+
import static jakarta.persistence.GenerationType.IDENTITY;
12+
313
public class BaseEntity {
4-
}
14+
@Id
15+
@GeneratedValue(strategy = IDENTITY)
16+
@EqualsAndHashCode.Include
17+
private Long id;
18+
19+
@CreatedDate
20+
@Getter
21+
private LocalDateTime createdAt;
22+
23+
@CreatedDate
24+
@Getter
25+
private LocalDateTime modifiedAt;
26+
}

0 commit comments

Comments
Β (0)