-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refac] BaseResponse, ExceptionHandler 수정 (#32)
* [refac] create interface for code * [refac] refactor exception and common api response * [feat] create response advice * [refac] fix typo in UserErrorCode.java * [refac] change Hankki response creation logic * [refac] delete duplicated BaseTimeEntity.java * [refac] fix HankkiResponse.java as api spec * [refac] fix unappropriate error message while validating refreshtoken * [refac] change typo in ResponseAdvice.java
- Loading branch information
Showing
33 changed files
with
299 additions
and
240 deletions.
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
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.