-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[refac] BaseResponse, ExceptionHandler 수정 #32
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2428b1c
[refac] create interface for code
Parkjyun 3aba0e4
[refac] refactor exception and common api response
Parkjyun 6904850
[feat] create response advice
Parkjyun 0298f2d
[refac] fix typo in UserErrorCode.java
Parkjyun f6aa5d7
[refac] change Hankki response creation logic
Parkjyun fb965da
[refac] delete duplicated BaseTimeEntity.java
Parkjyun 9051f25
[refac] fix HankkiResponse.java as api spec
Parkjyun ae1759a
[refac] fix unappropriate error message while validating refreshtoken
Parkjyun effce80
[refac] change typo in ResponseAdvice.java
Parkjyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/org/hankki/hankkiserver/api/advice/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 |
---|---|---|
@@ -1,4 +1,41 @@ | ||
package org.hankki.hankkiserver.api.advice; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.hankki.hankkiserver.api.dto.HankkiResponse; | ||
import org.hankki.hankkiserver.common.code.BusinessErrorCode; | ||
import org.hankki.hankkiserver.common.exception.BadRequestException; | ||
import org.hankki.hankkiserver.common.exception.NotFoundException; | ||
import org.hankki.hankkiserver.common.exception.UnauthorizedException; | ||
|
||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(BadRequestException.class) | ||
public HankkiResponse<Void> handleBadRequestException(BadRequestException e) { | ||
log.error("handleBadRequestException() in GlobalExceptionHandler throw BadRequestException : {}", e.getMessage()); | ||
return HankkiResponse.fail(e.getErrorCode()); | ||
} | ||
|
||
@ExceptionHandler(UnauthorizedException.class) | ||
public HankkiResponse<Void> handleUnauthorizedException(UnauthorizedException e) { | ||
log.error("handleUnauthorizedException() in GlobalExceptionHandler throw UnauthorizedException : {}", e.getMessage()); | ||
return HankkiResponse.fail(e.getErrorCode()); | ||
} | ||
|
||
@ExceptionHandler(NotFoundException.class) | ||
public HankkiResponse<Void> handleEntityNotFoundException(NotFoundException e) { | ||
log.error("handleEntityNotFoundException() in GlobalExceptionHandler throw EntityNotFoundException : {}", e.getMessage()); | ||
return HankkiResponse.fail(e.getErrorCode()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public HankkiResponse<Void> handleException(Exception e) { | ||
log.error("handleException() in GlobalExceptionHandler throw Exception : {}", e.getMessage()); | ||
return HankkiResponse.fail(BusinessErrorCode.INTERNAL_SERVER_ERROR); | ||
|
||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.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,29 @@ | ||
package org.hankki.hankkiserver.api.advice; | ||
|
||
import org.hankki.hankkiserver.api.dto.HankkiResponse; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServerHttpResponse; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
||
@RestControllerAdvice(basePackages = "org.hankki.hankkiserver.api") | ||
public class ResponseAdvice implements ResponseBodyAdvice<Object> { | ||
|
||
@Override | ||
public boolean supports(MethodParameter returnType, Class converterType) { | ||
return returnType.getParameterType() == HankkiResponse.class; | ||
} | ||
|
||
@Override | ||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { | ||
if (returnType.getParameterType() == HankkiResponse.class) { | ||
HttpStatus status = HttpStatus.valueOf(((HankkiResponse<?>) body).getCode()); | ||
response.setStatusCode(status); | ||
} | ||
return body; | ||
} | ||
|
||
} |
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
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
23 changes: 0 additions & 23 deletions
23
src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.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,30 @@ | ||
package org.hankki.hankkiserver.api.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import lombok.*; | ||
import org.hankki.hankkiserver.common.code.ErrorCode; | ||
import org.hankki.hankkiserver.common.code.SuccessCode; | ||
|
||
@Getter | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class HankkiResponse<T> { | ||
|
||
private final int code; | ||
private final String message; | ||
@JsonInclude(value = JsonInclude.Include.NON_NULL) | ||
private T data; | ||
|
||
public static <T> HankkiResponse<T> success(SuccessCode success) { | ||
return new HankkiResponse<>(success.getHttpStatus().value(), success.getMessage()); | ||
} | ||
|
||
public static <T> HankkiResponse<T> success(SuccessCode success, T data) { | ||
return new HankkiResponse<>(success.getHttpStatus().value(), success.getMessage(), data); | ||
} | ||
|
||
public static <T> HankkiResponse<T> fail(ErrorCode error) { | ||
return new HankkiResponse<>(error.getHttpStatus().value(), error.getMessage()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.hankki.hankkiserver.api.dto.BaseResponse; | ||
import org.hankki.hankkiserver.api.dto.HankkiResponse; | ||
import org.hankki.hankkiserver.common.code.AuthErrorCode; | ||
import org.hankki.hankkiserver.common.code.ErrorCode; | ||
import org.hankki.hankkiserver.common.exception.UnauthorizedException; | ||
import org.springframework.http.HttpStatus; | ||
|
@@ -36,25 +37,25 @@ private void handleUnauthorizedException( | |
HttpServletResponse response, | ||
Exception e) throws IOException { | ||
UnauthorizedException ue = (UnauthorizedException) e; | ||
ErrorCode errorCode = ue.getErrorCode(); | ||
HttpStatus httpStatus = errorCode.getHttpStatus(); | ||
setResponse(response, httpStatus, errorCode); | ||
ErrorCode authErrorCode = ue.getErrorCode(); | ||
HttpStatus httpStatus = authErrorCode.getHttpStatus(); | ||
setResponse(response, httpStatus, authErrorCode); | ||
} | ||
|
||
private void handleException( | ||
HttpServletResponse response, | ||
Exception e) throws IOException { | ||
setResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.INTERNAL_SERVER_ERROR); | ||
setResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, AuthErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
private void setResponse( | ||
HttpServletResponse response, | ||
HttpStatus httpStatus, | ||
ErrorCode errorCode) throws IOException { | ||
ErrorCode authErrorCode) throws IOException { | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setCharacterEncoding("utf-8"); | ||
response.setStatus(httpStatus.value()); | ||
PrintWriter writer = response.getWriter(); | ||
writer.write(objectMapper.writeValueAsString(BaseResponse.of(errorCode))); | ||
writer.write(objectMapper.writeValueAsString(HankkiResponse.fail(authErrorCode))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굳굳 |
||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굳!