Newer
Older
bremer / src / main / kotlin / ExceptionControllerAdvice.kt
yhornisse on 2 Jul 2023 1 KB add project
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer

import net.piedpiper.bremer.exception.BadRequestException
import net.piedpiper.bremer.exception.NotFoundException
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler

@ControllerAdvice
class ExceptionControllerAdvice {
    class ErrorMessageModel(
        val status: Int,
        val message: String?
    )

    @ExceptionHandler
    fun handleNotFoundException(e: BadRequestException): ResponseEntity<ErrorMessageModel> {
        return ResponseEntity(
            ErrorMessageModel(
                HttpStatus.BAD_REQUEST.value(),
                e.message
            ), HttpStatus.BAD_REQUEST
        )
    }

    @ExceptionHandler
    fun handleBadRequestException(e: NotFoundException): ResponseEntity<ErrorMessageModel> {
        return ResponseEntity(
            ErrorMessageModel(
                HttpStatus.NOT_FOUND.value(),
                e.message
            ), HttpStatus.NOT_FOUND
        )
    }
}