From 4113c1a873816e36c2f7c18794c3c13295da35a5 Mon Sep 17 00:00:00 2001 From: ITQ Date: Sat, 18 Apr 2026 13:15:09 +0300 Subject: [PATCH] feat(web): added global exception handler --- .../adapters/web/ApiExceptionHandler.kt | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/kotlin/com/project/movienight/adapters/web/ApiExceptionHandler.kt diff --git a/src/main/kotlin/com/project/movienight/adapters/web/ApiExceptionHandler.kt b/src/main/kotlin/com/project/movienight/adapters/web/ApiExceptionHandler.kt new file mode 100644 index 0000000..27a236d --- /dev/null +++ b/src/main/kotlin/com/project/movienight/adapters/web/ApiExceptionHandler.kt @@ -0,0 +1,31 @@ +package com.project.movienight.adapters.web + +import com.project.movienight.domain.exception.BlockedValueException +import com.project.movienight.domain.exception.DomainException +import com.project.movienight.domain.exception.EntityNotFoundException +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.bind.annotation.RestControllerAdvice + +@RestControllerAdvice +class ApiExceptionHandler { + @ExceptionHandler(EntityNotFoundException::class) + @ResponseStatus(HttpStatus.NOT_FOUND) + fun handleNotFound(exception: EntityNotFoundException): ErrorResponse = + ErrorResponse(message = exception.message ?: "Entity not found") + + @ExceptionHandler(BlockedValueException::class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + fun handleBlockedValue(exception: BlockedValueException): ErrorResponse = + ErrorResponse(message = exception.message ?: "Blocked value") + + @ExceptionHandler(DomainException::class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + fun handleDomainException(exception: DomainException): ErrorResponse = + ErrorResponse(message = exception.message ?: "Domain error") +} + +data class ErrorResponse( + val message: String, +)