-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from STUDIO-EYE/feat/EPIC-136]-user
[Feat] GlobalExceptionHandler 구현하여 이메일검증
- Loading branch information
Showing
6 changed files
with
99 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/com/example/UserService/dto/ApiResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.example.UserService.dto; | ||
|
||
import com.example.UserService.exception.ExceptionCode; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ApiResponse<T> { | ||
|
||
// API 상태 코드 | ||
private int code; | ||
|
||
// API 상태 | ||
private HttpStatus status; | ||
|
||
// API 응답 메시지 | ||
private String message; | ||
|
||
// API 응답 데이터 | ||
private T data; | ||
|
||
private ApiResponse(HttpStatus status, String message, T data) { | ||
this.code = status.value(); | ||
this.status = status; | ||
this.message = message; | ||
this.data = data; | ||
} | ||
|
||
public static <T> ApiResponse<T> of(HttpStatus status, String message, T data) { | ||
return new ApiResponse<>(status, message, data); | ||
} | ||
|
||
public static <T> ApiResponse<T> of(HttpStatus status, T data) { | ||
return of(status, status.name(), data); | ||
} | ||
|
||
public static <T> ApiResponse<T> ok(T data) { | ||
return of(HttpStatus.OK, data); | ||
} | ||
|
||
public static <T> ApiResponse<T> ok(String message) { | ||
return of(HttpStatus.OK, message, null); | ||
} | ||
|
||
public static <T> ApiResponse<T> ok(String message, T data) { | ||
return of(HttpStatus.OK, message, data); | ||
} | ||
|
||
public static <T> ApiResponse<T> withError(ExceptionCode exceptionCode, T data) { | ||
return new ApiResponse<>(exceptionCode.getStatus(), exceptionCode.getMessage(), data); | ||
} | ||
|
||
public static <T> ApiResponse<T> withError(ExceptionCode exceptionCode) { | ||
return withError(exceptionCode, null); | ||
} | ||
} |
39 changes: 18 additions & 21 deletions
39
src/main/java/com/example/UserService/exception/ExceptionCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,26 @@ | ||
package com.example.UserService.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ExceptionCode { | ||
MEMBER_NOT_FOUND(404, "회원을 찾을 수 없습니다"), | ||
RESOURCE_NOT_FOUND(404, "리소스를 찾을 수 없습니다"), | ||
BAD_REQUEST(400, "잘못된 요청입니다"), | ||
UNAUTHORIZED(401, "인증되지 않았습니다"), | ||
FORBIDDEN(403, "접근이 금지되었습니다"), | ||
INVALID_INPUT(422, "유효하지 않은 입력입니다"), | ||
INTERNAL_SERVER_ERROR(500, "내부 서버 오류입니다"), | ||
MEMBER_EXISTS(409, "이미 존재하는 회원입니다"), | ||
NO_SUCH_ALGORITHM(500, "존재하지 않는 알고리즘입니다"), | ||
UNABLE_TO_SEND_EMAIL(500, "이메일을 전송할 수 없습니다"), | ||
TOKEN_IS_NOT_SAME(401, "액세스 토큰과 리프레시 토큰이 일치하지 않습니다"), | ||
HEADER_REFRESH_TOKEN_NOT_EXISTS(400, "요청 헤더에 리프레시 토큰이 누락되었습니다"); | ||
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "회원을 찾을 수 없습니다"), | ||
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "리소스를 찾을 수 없습니다"), | ||
BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다"), | ||
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "인증되지 않았습니다"), | ||
FORBIDDEN(HttpStatus.FORBIDDEN, "접근이 금지되었습니다"), | ||
INVALID_INPUT(HttpStatus.BAD_REQUEST, "유효하지 않은 입력입니다"), | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "내부 서버 오류입니다"), | ||
MEMBER_EXISTS(HttpStatus.CONFLICT, "이미 존재하는 회원입니다"), | ||
NO_SUCH_ALGORITHM(HttpStatus.INTERNAL_SERVER_ERROR, "존재하지 않는 알고리즘입니다"), | ||
UNABLE_TO_SEND_EMAIL(HttpStatus.INTERNAL_SERVER_ERROR, "이메일을 전송할 수 없습니다"), | ||
TOKEN_IS_NOT_SAME(HttpStatus.UNAUTHORIZED, "액세스 토큰과 리프레시 토큰이 일치하지 않습니다"), | ||
HEADER_REFRESH_TOKEN_NOT_EXISTS(HttpStatus.BAD_REQUEST, "요청 헤더에 리프레시 토큰이 누락되었습니다"); | ||
|
||
@Getter | ||
private int status; | ||
private final HttpStatus status; | ||
private final String message; | ||
|
||
@Getter | ||
private String message; | ||
|
||
ExceptionCode(int status, String message) { | ||
this.status = status; | ||
this.message = message; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/UserService/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.UserService.exception; | ||
|
||
import com.example.UserService.dto.ApiResponse; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(BlogAPIException.class) | ||
public ApiResponse<?> handleBlogAPIException(BlogAPIException e) { | ||
return ApiResponse.withError(ExceptionCode.MEMBER_EXISTS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters