Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

[WEAV-000] 예외 타입 리팩터링 #253

Merged
merged 1 commit into from
Apr 21, 2024
Merged

[WEAV-000] 예외 타입 리팩터링 #253

merged 1 commit into from
Apr 21, 2024

Conversation

waterfogSW
Copy link
Member

@waterfogSW waterfogSW commented Apr 21, 2024

배경

  • 기존 예외처리 방식의 CustomException으로 타입이 통일되어있어 ExceptionHandler나, shouldThrow<타입> 과 같은 곳에서 클래스 타입을 기반으로 구현하기 어려움
  • 내부 속성을 기반으로 예외 타입을 판단 -> 내부 속성을 꺼내는 코드들이 계속 중복됨

리펙터링 방향

  • CustomException을 abstract class로, abstract class를 상속한 각 도메인별, 레이어별 Exception을 구현
  • 도메인, 레이어별 Exception은 sealed class로 세부 예외들을 내부 클래스로 선언

기존방식

class CustomException(
    val type: CustomExceptionType,
    override val message: String
) : RuntimeException(message)

enum class JwtExceptionType(override val code: String) : CustomExceptionType {
    JWT_DECODE_EXCEPTION("JWT-001"),
    JWT_EXPIRED_EXCEPTION("JWT-002"),
    JWT_VERIFICATION_EXCEPTION("JWT-003"),
}

사용 예시

        if (meetingDomainService.existsMeetingRequest(myTeam.id, receivingTeamId)) {
            throw CustomException(
                MeetingExceptionType.ALREADY_REQUEST_MEETING,
                "이미 상대팀에게 미팅을 신청했어요!",
            )
        }

변경된 방식

abstract class CustomException(
    codePrefix: String = DEFAULT_CODE_PREFIX,
    codeNumber: Int,
    override val message: String = DEFAULT_MESSAGE,
) : RuntimeException(message) {

    val code: String = "$codePrefix-${
        codeNumber.toString().padStart(DEFAULT_CODE_NUMBER_LENGTH, DEFAULT_CODE_NUMBER_PAD_CHAR)
    }"

    companion object {

        const val DEFAULT_CODE_NUMBER_LENGTH = 3
        const val DEFAULT_CODE_NUMBER_PAD_CHAR = '0'
        const val DEFAULT_CODE_PREFIX = "UNKNOWN"
        const val DEFAULT_MESSAGE = "알 수 없는 오류가 발생했습니다."

    }

}


sealed class JwtException(
    codeNumber: Int,
    message: String,
) : CustomException(CODE_PREFIX, codeNumber, message) {

    class DecodeException(message: String = "유효하지 않은 토큰입니다.") :
        JwtException(codeNumber = 1, message = message)

    class Expired(message: String = "토큰이 만료되었습니다.") :
        JwtException(codeNumber = 2, message = message)

    class VerificationException(message: String = "유효하지 않은 토큰입니다.") :
        JwtException(codeNumber = 3, message = message)

    companion object {
        const val CODE_PREFIX = "JWT"
    }

}

사용 예시

        if (meetingDomainService.existsMeetingRequest(myTeam.id, receivingTeamId)) {
            throw MeetingException.AlreadyRequestMeeting()
        }

@waterfogSW waterfogSW self-assigned this Apr 21, 2024
@waterfogSW waterfogSW added the refactor Refactoring code label Apr 21, 2024
@waterfogSW waterfogSW changed the title [WEAV-000] 예외 처리 리팩터링 [WEAV-000] 예외 타입 리팩터링 Apr 21, 2024
Copy link

sonarcloud bot commented Apr 21, 2024

@waterfogSW waterfogSW merged commit 40be553 into main Apr 21, 2024
3 checks passed
@waterfogSW waterfogSW deleted the feat/WEAV-000 branch April 21, 2024 13:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
refactor Refactoring code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants