Skip to content

Commit

Permalink
refactor : HttpStatus 코드 ExceptionHandler에서 반환하도록 수정(#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
isprogrammingfun committed May 21, 2024
1 parent 5fecd36 commit bde04b0
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package com.bamyanggang.apimodule.domain.user.application.exception

import com.bamyanggang.commonmodule.exception.CustomException
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

sealed class AuthException (
errorCode: Int,
httpStatusCode: HttpStatusCode,
message: String,
) : CustomException(CODE_PREFIX, errorCode, httpStatusCode , message) {
) : CustomException(CODE_PREFIX, errorCode, message) {

class OAuthFailed(message: String = "OAuth 인증에 실패하였습니다.") :
AuthException(errorCode = 1, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
AuthException(errorCode = 1, message = message)

class KakaoUserInfoRetrievalException(message: String = "카카오 사용자 정보를 가져오는데 실패했습니다.") :
AuthException(errorCode = 2, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
AuthException(errorCode = 2, message = message)

class GoogleUserInfoRetrievalException(message: String = "구글 사용자 정보를 가져오는데 실패했습니다.") :
AuthException(errorCode = 3, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
AuthException(errorCode = 3, message = message)

companion object {
const val CODE_PREFIX = "AUTH"
}

}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package com.bamyanggang.commonmodule.exception

import org.springframework.http.HttpStatusCode

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

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

val httpStatusCode: HttpStatusCode = httpStatusCode

companion object {
const val DEFAULT_CODE_PREFIX = "UNKNOWN"
const val DEFAULT_MESSAGE = "예상하지 못한 오류가 발생했습니다."
const val DEFAULT_CODE_NUMBER_LENGTH = 3
const val DEFAULT_CODE_NUMBER_PAD_CHAR = '0'
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ExceptionHandler {
private val logger = LoggerFactory.getLogger(ExceptionHandler::class.java)
@ExceptionHandler(CustomException::class)
fun handleJwtException(e: CustomException): ResponseEntity<ErrorResponse> {
return ResponseEntity(ErrorResponse(e.code, e.message), e.httpStatusCode)
return ResponseEntity(ErrorResponse(e.code, e.message), HttpStatus.BAD_REQUEST)
}

@ExceptionHandler(Exception::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.bamyanggang.domainmodule.domain.jobDescription.exception

import com.bamyanggang.commonmodule.exception.CustomException
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

sealed class JobDescriptionException (
errorCode: Int,
httpStatusCode: HttpStatusCode,
message: String,
) : CustomException(CODE_PREFIX, errorCode, httpStatusCode , message) {
) : CustomException(CODE_PREFIX, errorCode, message) {

class ModifyWriteStatusFailed(message: String = "쓰기 상태 변경에 실패하였습니다.") :
JobDescriptionException(errorCode = 1, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
JobDescriptionException(errorCode = 1, message = message)

companion object {
const val CODE_PREFIX = "AUTH"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package com.bamyanggang.domainmodule.domain.strongpoint.exception

import com.bamyanggang.commonmodule.exception.CustomException
import com.bamyanggang.domainmodule.domain.strongpoint.aggregate.StrongPoint
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

sealed class StrongPointException(
errorCode: Int,
httpStatusCode: HttpStatusCode,
message: String,
) : CustomException(CODE_PREFIX, errorCode, httpStatusCode , message) {
) : CustomException(CODE_PREFIX, errorCode, message) {

class OverCountLimit (message: String = "역량 키워드는 최대 ${StrongPoint.LIMIT}개까지 등록 가능합니다.") :
StrongPointException(errorCode = 3, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
StrongPointException(errorCode = 3, message = message)

companion object {
const val CODE_PREFIX = "STRONG_POINT"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package com.bamyanggang.domainmodule.domain.tag.exception

import com.bamyanggang.commonmodule.exception.CustomException
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

sealed class TagException(
errorCode: Int,
httpStatusCode: HttpStatusCode,
message: String,
) : CustomException(CODE_PREFIX, errorCode, httpStatusCode , message) {
) : CustomException(CODE_PREFIX, errorCode, message) {

class DuplicatedTagName(message: String = "태그 이름이 중복됩니다.") :
TagException(errorCode = 1, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
TagException(errorCode = 1, message = message)

class OverTagCountLimit(message: String = "태그 개수가 제한 개수보다 많습니다.") :
TagException(errorCode = 2, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
TagException(errorCode = 2, message = message)

class NotFoundTag(message: String = "존재하지 않는 태그 입니다.") :
TagException(errorCode = 3, httpStatusCode = HttpStatus.NOT_FOUND, message = message)
TagException(errorCode = 3, message = message)

companion object {
const val CODE_PREFIX = "TAG"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.bamyanggang.persistence.common.exception;

import com.bamyanggang.commonmodule.exception.CustomException;
import org.springframework.http.HttpStatus;

public class PersistenceException extends CustomException {
public static final String CODE_PREFIX = "PERSISTENCE";

public PersistenceException(int errorCode, HttpStatus httpStatusCode, String message) {
super(CODE_PREFIX, errorCode, httpStatusCode, message);
public PersistenceException(int errorCode, String message) {
super(CODE_PREFIX, errorCode, message);
}

public static class NotFound extends PersistenceException {
public NotFound() {
super(1, HttpStatus.NOT_FOUND, "해당 데이터를 찾을 수 없습니다.");
super(1, "해당 데이터를 찾을 수 없습니다.");
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package com.bamyanggang.jwt.exception

import com.bamyanggang.commonmodule.exception.CustomException
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

sealed class JwtException(
errorCode: Int,
httpStatusCode: HttpStatusCode,
message: String,
) : CustomException(CODE_PREFIX, errorCode, httpStatusCode , message) {
) : CustomException(CODE_PREFIX, errorCode, message) {

class InvalidTokenException(message: String = "유효하지 않은 토큰입니다.") :
JwtException(errorCode = 1, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
JwtException(errorCode = 1, message = message)

class ExpiredTokenException(message: String = "만료된 토큰입니다.") :
JwtException(errorCode = 2, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
JwtException(errorCode = 2, message = message)

class TokenNotFoundException(message: String = "토큰이 존재하지 않습니다.") :
JwtException(errorCode = 3, httpStatusCode = HttpStatus.BAD_REQUEST, message = message)
JwtException(errorCode = 3, message = message)

companion object {
const val CODE_PREFIX = "JWT"
}

}
}

0 comments on commit bde04b0

Please sign in to comment.