refactor: CustomException 계층화를 통한 예외 통일 #140
Merged
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.
🔥이슈
#139
🔎 작업 내용
문제 상황
현재 global/exception 내에서 여러 도메인의 예외가 만들어져있다. 이 예외들은 단순히 RuntimeException을 상속받아 사용한다. 이 경우 각 예외를 ControllerAdvice에서 세부적으로 handling할 수 없다.
예를 들어, DuplicateUserException과 CompanyNotFoundException은 모두 RuntimeException을 상속한다고 하자. 이 경우, 각 예외들을 핸들링하는 방법은 RuntimeException으로 처리하는 방법과 각 예외를 처리하는 방법, 두 가지가 전부이다.
구현한 내용
따라서 각 예외를 계층화하여 세부적으로 예외를 핸들링하고, 핸들링하는 코드를 줄일 수 있도록 하였다.

위 그림처럼 최상위 BusinessException에서 공통적으로 나타나는 Duplicate와 NotFound를 자식으로 생성하도록 했다.
이렇게 작성할 경우, BusinessException, DuplicateException, NotFoundException만 ControllerAdvice에서 처리하면 되기 때문에 코드가 매우 줄어든다.
또한, 통일된 응답을 위해 ErrorCode와 ErrorResponse을 만들었다.

ErrorCodes에선 자체적인 에러코드와 에러 메시지를 Enum으로 만들어 사용하기 편하도록 두었다. 또한 ErrorResposne을 통해 정해진 틀의 정보를 제공하도록 하였다.