From beeb2f92d1e88b01fc86b657ffc39d4d1bb11705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=9D=80=EC=B0=AC?= Date: Thu, 9 Jan 2025 17:51:31 +0900 Subject: [PATCH] feat : (#19) apply custom exception --- .../domain/report/query/service/ReportQueryService.kt | 1 - .../gitauth/equusgithubauth/global/exception/ErrorCode.kt | 4 ++++ .../global/security/auth/AuthDetailsService.kt | 3 ++- .../global/security/auth/exception/UserNotFoundException.kt | 6 ++++++ .../equusgithubauth/global/security/jwt/JwtTokenProvider.kt | 6 ++++-- .../security/jwt/exception/JwtTokenExpiredException.kt | 6 ++++++ .../security/jwt/exception/JwtTokenInvalidException.kt | 6 ++++++ 7 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/auth/exception/UserNotFoundException.kt create mode 100644 src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/exception/JwtTokenExpiredException.kt create mode 100644 src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/exception/JwtTokenInvalidException.kt diff --git a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/domain/report/query/service/ReportQueryService.kt b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/domain/report/query/service/ReportQueryService.kt index 120a8aa..1bbdfbe 100644 --- a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/domain/report/query/service/ReportQueryService.kt +++ b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/domain/report/query/service/ReportQueryService.kt @@ -1,6 +1,5 @@ package entry.dsm.gitauth.equusgithubauth.domain.report.query.service -import entry.dsm.gitauth.equusgithubauth.domain.report.entity.Report import entry.dsm.gitauth.equusgithubauth.domain.report.exception.ReportNotFoundException import entry.dsm.gitauth.equusgithubauth.domain.report.query.dto.response.ReportQueryResponse import entry.dsm.gitauth.equusgithubauth.domain.report.query.repository.ReportQueryRepository diff --git a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/exception/ErrorCode.kt b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/exception/ErrorCode.kt index 95411ef..4266dd7 100644 --- a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/exception/ErrorCode.kt +++ b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/exception/ErrorCode.kt @@ -4,6 +4,7 @@ enum class ErrorCode( val status: Int, val message: String, ) { + USER_NOT_FOUND(404, "사용자를 찾을 수 없습니다."), USER_INFO_FETCH_ERROR(500, "GitHub 사용자 정보를 가져올 수 없습니다."), AUTHORIZED_CLIENT_NOT_FOUND(400, "사용자에 대한 인증된 클라이언트를 찾을 수 없습니다."), ORGANIZATION_MEMBERSHIP_ERROR(400, "사용자가 조직의 멤버가 아닙니다."), @@ -15,4 +16,7 @@ enum class ErrorCode( NOTICE_NOT_FOUND(404, "공지사항을 찾을 수 없습니다."), REPORT_NOT_FOUND(404, "보고서를 찾을 수 없습니다."), + + JWT_TOKEN_EXPIRED(401, "JWT 토큰이 만료되었습니다."), + JWT_TOKEN_INVALID(401, "JWT 토큰이 유효하지 않습니다."), } diff --git a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/auth/AuthDetailsService.kt b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/auth/AuthDetailsService.kt index 9389572..d27cf59 100644 --- a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/auth/AuthDetailsService.kt +++ b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/auth/AuthDetailsService.kt @@ -2,6 +2,7 @@ package entry.dsm.gitauth.equusgithubauth.global.security.auth import entry.dsm.gitauth.equusgithubauth.domain.user.entity.User import entry.dsm.gitauth.equusgithubauth.domain.user.entity.repository.UserRepository +import entry.dsm.gitauth.equusgithubauth.global.security.auth.exception.UserNotFoundException import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.UserDetailsService import org.springframework.stereotype.Service @@ -13,7 +14,7 @@ class AuthDetailsService( override fun loadUserByUsername(githubId: String): UserDetails { val user: User = userRepository.findByGithubId(githubId) - ?: throw IllegalArgumentException("User not found with githubId: $githubId") + ?: throw UserNotFoundException return AuthDetails(user) } diff --git a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/auth/exception/UserNotFoundException.kt b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/auth/exception/UserNotFoundException.kt new file mode 100644 index 0000000..c1a9202 --- /dev/null +++ b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/auth/exception/UserNotFoundException.kt @@ -0,0 +1,6 @@ +package entry.dsm.gitauth.equusgithubauth.global.security.auth.exception + +import entry.dsm.gitauth.equusgithubauth.global.exception.CustomException +import entry.dsm.gitauth.equusgithubauth.global.exception.ErrorCode + +object UserNotFoundException : CustomException(ErrorCode.USER_NOT_FOUND) diff --git a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/JwtTokenProvider.kt b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/JwtTokenProvider.kt index 1db8892..710451e 100644 --- a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/JwtTokenProvider.kt +++ b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/JwtTokenProvider.kt @@ -4,6 +4,8 @@ import entry.dsm.gitauth.equusgithubauth.domain.user.entity.RefreshToken import entry.dsm.gitauth.equusgithubauth.domain.user.entity.repository.RefreshTokenRepository import entry.dsm.gitauth.equusgithubauth.domain.user.presentation.dto.response.TokenResponse import entry.dsm.gitauth.equusgithubauth.global.security.auth.AuthDetailsService +import entry.dsm.gitauth.equusgithubauth.global.security.jwt.exception.JwtTokenExpiredException +import entry.dsm.gitauth.equusgithubauth.global.security.jwt.exception.JwtTokenInvalidException import io.jsonwebtoken.Claims import io.jsonwebtoken.ExpiredJwtException import io.jsonwebtoken.Jwts @@ -86,8 +88,8 @@ class JwtTokenProvider( .body } catch (e: Exception) { when (e) { - is ExpiredJwtException -> throw IllegalArgumentException("토큰이 만료되었습니다.") - else -> throw IllegalArgumentException("유효하지 않은 토큰입니다.") + is ExpiredJwtException -> throw JwtTokenExpiredException + else -> throw JwtTokenInvalidException } } } diff --git a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/exception/JwtTokenExpiredException.kt b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/exception/JwtTokenExpiredException.kt new file mode 100644 index 0000000..bf3b1a6 --- /dev/null +++ b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/exception/JwtTokenExpiredException.kt @@ -0,0 +1,6 @@ +package entry.dsm.gitauth.equusgithubauth.global.security.jwt.exception + +import entry.dsm.gitauth.equusgithubauth.global.exception.CustomException +import entry.dsm.gitauth.equusgithubauth.global.exception.ErrorCode + +object JwtTokenExpiredException : CustomException(ErrorCode.JWT_TOKEN_EXPIRED) diff --git a/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/exception/JwtTokenInvalidException.kt b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/exception/JwtTokenInvalidException.kt new file mode 100644 index 0000000..30eba85 --- /dev/null +++ b/src/main/kotlin/entry/dsm/gitauth/equusgithubauth/global/security/jwt/exception/JwtTokenInvalidException.kt @@ -0,0 +1,6 @@ +package entry.dsm.gitauth.equusgithubauth.global.security.jwt.exception + +import entry.dsm.gitauth.equusgithubauth.global.exception.CustomException +import entry.dsm.gitauth.equusgithubauth.global.exception.ErrorCode + +object JwtTokenInvalidException : CustomException(ErrorCode.JWT_TOKEN_INVALID)