🎨 [demo-app, common-util] Global Exception Handler 설정 추가
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package me.jiniworld.demohx.config.exception
|
||||
|
||||
import me.jiniworld.demohx.model.BaseResponse
|
||||
import me.jiniworld.demohx.model.ServerException
|
||||
import mu.KotlinLogging
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
|
||||
|
||||
@RestControllerAdvice
|
||||
internal class GlobalExceptionHandler: ResponseEntityExceptionHandler() {
|
||||
private val logging = KotlinLogging.logger {}
|
||||
|
||||
@ExceptionHandler(ServerException::class)
|
||||
fun handleServerException(ex: ServerException): ResponseEntity<BaseResponse> {
|
||||
logging.error { ex.message }
|
||||
return ResponseEntity.status(ex.code)
|
||||
.body(BaseResponse(result = "fail", reason = ex.message))
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception::class)
|
||||
fun handleException(ex: Exception): ResponseEntity<BaseResponse> {
|
||||
logging.error { ex.message }
|
||||
return ResponseEntity.internalServerError()
|
||||
.body(BaseResponse(result = "fail", reason = "Internal Server Error"))
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,11 @@ import io.swagger.v3.oas.annotations.tags.Tag
|
||||
import me.jiniworld.demohx.annotation.WebAdapter
|
||||
import me.jiniworld.demohx.application.notice.port.input.GetNoticeQuery
|
||||
import me.jiniworld.demohx.application.notice.port.input.GetNoticesCommand
|
||||
import me.jiniworld.demohx.model.BaseResponse
|
||||
import me.jiniworld.demohx.model.DataResponse
|
||||
import me.jiniworld.demohx.model.NotFoundException
|
||||
import me.jiniworld.demohx.model.ServerException
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.*
|
||||
|
||||
@WebAdapter
|
||||
@@ -21,10 +26,14 @@ internal class GetNoticeController(
|
||||
@RequestParam(value = "page", required = false, defaultValue = "0") page: Int,
|
||||
@RequestParam(value = "size", required = false, defaultValue = "10") size: Int,
|
||||
) = getNoticeQuery.getNoticeSimple(GetNoticesCommand(page = page, size = size))
|
||||
?.let { DataResponse(data = it) }
|
||||
?: throw NotFoundException("공지사항이 없습니다.")
|
||||
|
||||
@Operation(summary = "공지사항 상세조회")
|
||||
@GetMapping("/{notice_id}")
|
||||
fun notice(@PathVariable("notice_id") noticeId: Long) =
|
||||
getNoticeQuery.getNoticeDetail(noticeId)
|
||||
fun notice(@PathVariable("notice_id") noticeId: Long,
|
||||
) = getNoticeQuery.getNoticeDetail(noticeId)
|
||||
?.let { DataResponse(data = it) }
|
||||
?: throw NotFoundException("조회되는 공지사항이 없습니다.")
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package me.jiniworld.demohx.model
|
||||
|
||||
sealed class ServerException(
|
||||
val code: Int,
|
||||
override val message: String
|
||||
): RuntimeException(message)
|
||||
|
||||
data class NotFoundException(
|
||||
override val message: String,
|
||||
): ServerException(404, message)
|
||||
|
||||
data class UnauthorizedException(
|
||||
override val message: String = "access_token 이 유효하지 않습니다.",
|
||||
): ServerException(401, message)
|
||||
Reference in New Issue
Block a user