Skip to content
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

에러코드 관리 및 유연한 에러 메세지 추가 #84

Closed
ghkdqhrbals opened this issue Apr 10, 2024 · 8 comments
Closed

에러코드 관리 및 유연한 에러 메세지 추가 #84

ghkdqhrbals opened this issue Apr 10, 2024 · 8 comments
Assignees

Comments

@ghkdqhrbals
Copy link
Member

현재 에러코드가 enum 으로 사전정의되어있습니다. 그리고 내부 필드변수로 메세지또한 사전정의되어있습니다. 그래서 외부에서는 메세지를 추가할 수 없죠.

유연하게 메세지를 추가하고 싶었습니다. 예로 ErrorCode.NO_USER 이라면 어떤 유저가 없는지 메세지를 넣어주고 싶었어요.

그래서 GlobalException.class 에 내부 메세지 필드 변수 추가하고 이를 RestControllerAdvice 에서 ResponseEntity 변환 후 클라이언트에게 전송할 때 메세지 변수를 읽는 로직을 추가하였습니다.

@ghkdqhrbals ghkdqhrbals self-assigned this Apr 10, 2024
@LeeJeongGi
Copy link
Member

테스크 #84 연결 하신거 같은데,, 이거 pr 올리고 적용시킬수 있는걸까요?

@ghkdqhrbals
Copy link
Member Author

네 PR 지금 올리겠습니다!

@LeeJeongGi
Copy link
Member

연결 자체를 어떻게 한건지 물어본거였어요 ㅋㅋㅋ 저도 그 메일인증 테스크에 연결을 해보고싶어서요,,ㅎㅎ

@ghkdqhrbals
Copy link
Member Author

ghkdqhrbals commented Apr 11, 2024

아하! 원하는 에러 메세지를 클라이언트에게 전달할 수 있는 있는 방법 을 말씀하신 거라면 관련 코드를 설명드리겠습니다.

관련코드는 위의 커밋 클릭하시면 확인하실 수 있어요

@Getter
public class GlobalException extends RuntimeException {

    private final ErrorCode errorCode;
    private String message = null;
...
}

먼저 위와 같이 GlobalException 에 메세지 필드를 추가합니다. 그리고 message 파라미터를 받는 생성자 하나 더 만들어 줍니다.

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class GlobalErrorResponse implements Serializable {
...
    public static ResponseEntity<GlobalErrorResponse> toResponseEntity(GlobalException e) {
        ErrorCode errorCode = e.getErrorCode();
        if (e.getMessage() != null){
            return ResponseEntity
                .status(errorCode.getHttpStatus())
                .body(GlobalErrorResponse.builder()
                    .status(errorCode.getHttpStatus())
                    .code(errorCode.name())
                    .message(e.getMessage())
                    .build()
                );
        }
        return ResponseEntity
            .status(errorCode.getHttpStatus())
            .body(GlobalErrorResponse.builder()
                .status(errorCode.getHttpStatus())
                .code(errorCode.name())
                .message(errorCode.getMessage())
                .build()
            );

    }
...

그리고 toResponseEntity 메서드에 GlobalException 에 메세지가 들어잇다면 ResponseEntity 변환 시 메세지를 사용하도록 설정하시면 됩니다. 이후 실제 사용할 땐 아래와 같이 사용되겠죠?

if (runningTestId != null) {
    throw new GlobalException(ErrorCode.ALREADY_RUNNING, "template is already running in testId " + runningTestId);
}

@ghkdqhrbals
Copy link
Member Author

연결 자체를 어떻게 한건지 물어본거였어요 ㅋㅋㅋ 저도 그 메일인증 테스크에 연결을 해보고싶어서요,,ㅎㅎ

혹시 redis 연결에 대해서 말씀하신거라면 docker compose 를 통해 띄우실 수 있습니다! 개별 컨테이너로 띄우는게 작업하시기 편하실거에요

@LeeJeongGi
Copy link
Member

image

이슈랑 pr 이랑 연결해둔걸까요?

@ghkdqhrbals
Copy link
Member Author

ghkdqhrbals commented Apr 11, 2024

이슈로 올린 피쳐가 구현된 코드를 커밋할 때, 커밋 메세지에 이슈번호를 기술해둔것입니다 :)

이슈와 커밋 이 연결됬다고 볼 수 있습니다.

@LeeJeongGi
Copy link
Member

아하 커밋 메시지에 이슈번호를 기술하면 되는거였군요
감사합니다!

ghkdqhrbals added a commit that referenced this issue Apr 12, 2024
* fix: #82 javaCompile -parameter

* dev: removing unused logging, class

* dev: compiled param with naming

* dev: configuring redis env. #81

* dev: add redis container

* dev: enable caching

* dev: local redis connection setup

* dev: ingress path rewrite

* chore: undo unused import

* dev: de-active template connection of result

* dev: redis testContainer initializer

* dev: ExtendWith testContainer

* dev: insert intermediate table #78

* dev: add additional errorCode

* dev: pageable test results list

* dev: front data loader

* dev: ErrorCode dynamic message input #84

* dev: randomized interface and generics

* dev: refactoring & testing

* test: discovery service & scheduler test

* test: undo unnecessary mocking test

* chore: undo log() for performance

* chore: update mttfbAvg

* dev: dto for MTTFB, TPS

* dev: chartjs setup

* dev: main controller of visualization MTTFB, TPS time series data

* dev: link for redirect graph page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants