Skip to content

Commit

Permalink
Merge pull request #2 from Katchup-dev/task/#1
Browse files Browse the repository at this point in the history
[KS-3] Exception Handler 구현
  • Loading branch information
unanchoi authored Apr 15, 2024
2 parents 289fd45 + 270fb8c commit 0e41083
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Katchup PR build

on:
push:
pull_request:
branches: [ "main" ]

permissions:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Katchup Mobile API Server
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package site.katchup.springboot.exception

import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import site.katchup.springboot.config.getLogger
import site.katchup.springboot.exception.base.KatchupException
import site.katchup.springboot.global.response.BaseResponse

@RestControllerAdvice
class GlobalControllerAdvice {

private val log = getLogger()

@ExceptionHandler(KatchupException::class)
fun handleBadRequestException(exception: KatchupException): ResponseEntity<BaseResponse<Unit>> {
return BaseResponse.fail(exception.httpStatus, exception.failMessage)
}

@ExceptionHandler(Exception::class)
fun handleException(exception: Exception): ResponseEntity<BaseResponse<Unit>> {
log.error(exception.message)
return BaseResponse.fail(exception)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package site.katchup.springboot.exception.base

import org.springframework.http.HttpStatus
import site.katchup.springboot.global.message.FailMessage

open class BadRequestException(
message: String,
failMessage: FailMessage,
) : KatchupException(
HttpStatus.BAD_REQUEST,
message,
failMessage,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package site.katchup.springboot.exception.base

import org.springframework.http.HttpStatus
import site.katchup.springboot.global.message.FailMessage

open class ForbiddenException(
message: String,
failMessage: FailMessage,
) : KatchupException(
HttpStatus.FORBIDDEN,
message,
failMessage,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package site.katchup.springboot.exception.base

import org.springframework.http.HttpStatus
import site.katchup.springboot.global.message.FailMessage

open class InternalServerException(
message: String,
failMessage: FailMessage,
) : KatchupException(
HttpStatus.INTERNAL_SERVER_ERROR,
message,
failMessage,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package site.katchup.springboot.exception.base

import org.springframework.http.HttpStatus
import site.katchup.springboot.global.message.FailMessage

open class KatchupException(
httpStatus: HttpStatus,
message: String,
) : RuntimeException(message)
val httpStatus: HttpStatus,
val failMessage: FailMessage,
) : RuntimeException(failMessage.value)
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package site.katchup.springboot.exception.base

import org.springframework.http.HttpStatus
import site.katchup.springboot.global.message.FailMessage

open class NotFoundException(
message: String,
failMessage: FailMessage,
) : KatchupException(
HttpStatus.NOT_FOUND,
message,
failMessage,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package site.katchup.springboot.exception.base

import org.springframework.http.HttpStatus
import site.katchup.springboot.global.message.FailMessage

open class UnauthorizedException(
message: String,
failMessage: FailMessage,
) : KatchupException(
HttpStatus.UNAUTHORIZED,
message,
failMessage,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class BaseResponse<T>(
val data: T?,
) {
constructor(message: String) : this(message, null)

companion object {
fun <T> ok(message: SuccessMessage, data: T): ResponseEntity<BaseResponse<T>> {
return ResponseEntity.ok(BaseResponse(message.value, data))
Expand All @@ -21,8 +22,13 @@ class BaseResponse<T>(
return ResponseEntity.status(HttpStatus.CREATED).body(BaseResponse(message.value, data))
}

fun <T> fail(message: FailMessage): BaseResponse<T> {
return BaseResponse(message.value, null)
fun fail(status: HttpStatus, message: FailMessage): ResponseEntity<BaseResponse<Unit>> {
return ResponseEntity.status(status).body(BaseResponse(message.value))
}

fun fail(exception: Exception): ResponseEntity<BaseResponse<Unit>> {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(BaseResponse(exception.message ?: "Internal Server Error"))
}
}
}

0 comments on commit 0e41083

Please sign in to comment.