0%
June 18, 2024

Global Exception Handler

kotlin

springboot

Folder Structure:

Implementation:

package com.kotlinspring.exceptionHandler

import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody

@ControllerAdvice
class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception::class)
    fun handleException(e: Exception): Map<String, Any?> {
        return mapOf(
            "success" to false,
            "errorMessage" to e.message
        )
    }
}