ReddyTintaya
11/03/2020, 2:48 PMconst val reason = "store not found"
@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = reason)
class StoreNotFoundException : RuntimeException()
but i want to log the error whenever its been throw
const val reason = "store not found"
@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = reason)
class StoreNotFoundException : RuntimeException(){
init {
warningLog(reason)
}
companion object {
private val logger = LoggerFactory.getLogger(CustomException::class.java)
fun warningLog(message: String) {
logger.warn(message)
}
}
}
i've implemented like this
Since i have a lot of exceptions defined like this and i want to have the log whenever one is threw, how can i generalize this behavior?
how can i write a CustomException?nicholasnet
11/03/2020, 3:42 PMReddyTintaya
11/03/2020, 3:44 PM@PutMapping("/{id}")
fun updateStore(...): Store {
... ?: throw StoreNotFoundException()
}
nicholasnet
11/03/2020, 3:46 PMReddyTintaya
11/03/2020, 3:47 PMtry{
...
throw StoreNotFoundException()
}catch(e: StoreNotFoundException){
log("store not found throwed")
}
ReddyTintaya
11/03/2020, 3:47 PMnicholasnet
11/03/2020, 3:56 PMkqr
11/03/2020, 4:07 PMkqr
11/03/2020, 4:07 PMReddyTintaya
11/03/2020, 4:16 PMReddyTintaya
11/03/2020, 4:17 PMReddyTintaya
11/03/2020, 8:49 PM@ControllerAdvice
class GlobalResponseException {
@ExceptionHandler(RuntimeException::class)
private fun handleRuntimeException(e: Exception) {
ExceptionLogger.log("$e at ${e.stackTrace?.first()?.methodName} inside ${e.stackTrace?.first()?.fileName}")
}
}
class ExceptionLogger {
companion object {
private val logger = LoggerFactory.getLogger(ExceptionLogger::class.java)
fun log(message: String) = logger.warn(message)
}
}
(if in the future someone has the same problem :3 )
like my Exceptions inherit from RuntimeException() it does everything i need