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

🔀 :: GlobalExceptionHandler 구현 #4

Merged
merged 5 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/kotlin/andreas311/miso/global/error/ErrorCode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package andreas311.miso.global.error

enum class ErrorCode(
val status: Int,
val message: String
) {

// SERVER ERROR
UNKNOWN_ERROR(500, "알 수 없는 에러입니다."),

// MAIL
EMAIL_SEND_FAIL(500, "사용자를 찾을 수 없습니다."),

// FILE
INVALID_FORMAT_FILE(400, "잘못된 형식의 파일입니다."),
FILE_UPLOAD_FAIL(500, "파일 업로드에 실패했습니다."),

// TOKEN
TOKEN_IS_EXPIRED(401, "토큰이 만료 되었습니다."),
TOKEN_NOT_VALID(401, "토큰이 유효 하지 않습니다."),

// USER
EMAIL_KEY_IS_INVALID(401, "이메일 인증번호가 일치하지 않습니다."),
EMAIL_IS_NOT_VALID(403, "인증되지 않은 이메일입니다."),
USER_NOT_FOUND(404, "사용자를 찾을 수 없습니다."),
ROLE_NOT_EXIST(404, "역할이 존재하지 않습니다"),
USER_ALREADY_EXIST(409, "이미 사용자가 존재합니다."),
MISMATCH_PASSWORD(400, "비밀번호가 일치하지 않습니다."),

// ITEM
ITEM_NOT_FOUND(404, "상품을 찾을 수 없습니다."),
ITEM_IS_SOLD_OUT(410, "상품의 재고가 남아있지 않습니다."),

// PURCHASE
POINT_IS_NOT_ENOUGH(403, "포인트가 부족합니다."),
PURCHASE_LOG_NOT_FOUND(404, "구매 기록을 찾을 수 없습니다."),

// INQUIRY
INQUIRY_LOG_NOT_FOUND(404, "문의 내역을 찾을 수 없습니다."),

// RECYCLABLES
RECYCLABLES_NOT_FOUND(404, "분리수거 페이지를 찾을 수 없습니다."),

// NOTIFICATION
NOTIFICATION_NOT_FOUND(404, "문의 사항 답변을 찾을 수 없습니다.")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package andreas311.miso.global.error.exception

import andreas311.miso.global.error.ErrorCode

open class MisoException(val errorCode: ErrorCode) : RuntimeException(errorCode.message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package andreas311.miso.global.error.exception.handler

import andreas311.miso.global.error.exception.MisoException
import andreas311.miso.global.error.response.ErrorResponse
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import javax.servlet.http.HttpServletRequest

@RestControllerAdvice
class GlobalExceptionHandler {

private val log = LoggerFactory.getLogger(this.javaClass.simpleName)

@ExceptionHandler(MisoException::class)
fun globalExceptionHandler(request: HttpServletRequest, ex: MisoException): ResponseEntity<ErrorResponse> {

log.error(ex.errorCode.message)

log.error(request.requestURI)

return ResponseEntity(
ErrorResponse(ex.errorCode.status, ex.errorCode.message),
HttpStatus.valueOf(ex.errorCode.status)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package andreas311.miso.global.error.response

data class ErrorResponse(
val status: Int,
val message: String
)
Loading