From cadcfa442c687dcc49b4e3dddb0030fdfde1cfef Mon Sep 17 00:00:00 2001 From: Jan Cizmar Date: Mon, 23 Sep 2024 14:12:03 +0200 Subject: [PATCH] fix: No static resource found error handling (#2479) It was throwing 500 when static resource was not found. (Usually favicon.ico) --- .../main/kotlin/io/tolgee/ExceptionHandlers.kt | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/backend/app/src/main/kotlin/io/tolgee/ExceptionHandlers.kt b/backend/app/src/main/kotlin/io/tolgee/ExceptionHandlers.kt index 4f21919c7a..3f16f427a2 100644 --- a/backend/app/src/main/kotlin/io/tolgee/ExceptionHandlers.kt +++ b/backend/app/src/main/kotlin/io/tolgee/ExceptionHandlers.kt @@ -7,7 +7,11 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse import io.tolgee.constants.Message import io.tolgee.dtos.request.validators.ValidationErrorType import io.tolgee.dtos.request.validators.exceptions.ValidationException -import io.tolgee.exceptions.* +import io.tolgee.exceptions.BadRequestException +import io.tolgee.exceptions.ErrorException +import io.tolgee.exceptions.ErrorResponseBody +import io.tolgee.exceptions.ErrorResponseTyped +import io.tolgee.exceptions.NotFoundException import io.tolgee.security.ratelimit.RateLimitResponseBody import io.tolgee.security.ratelimit.RateLimitedException import jakarta.persistence.EntityNotFoundException @@ -32,6 +36,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException import org.springframework.web.multipart.MaxUploadSizeExceededException import org.springframework.web.multipart.support.MissingServletRequestPartException +import org.springframework.web.servlet.resource.NoResourceFoundException import java.io.Serializable import java.util.* import java.util.function.Consumer @@ -238,6 +243,15 @@ class ExceptionHandlers { ) } + @ExceptionHandler(NoResourceFoundException::class) + fun handleNoResourceFound(ex: NoResourceFoundException): ResponseEntity { + logger.debug("No resource found", ex) + return ResponseEntity( + ErrorResponseBody(Message.RESOURCE_NOT_FOUND.code, listOf(ex.resourcePath)), + HttpStatus.NOT_FOUND, + ) + } + @ExceptionHandler(Throwable::class) fun handleOtherExceptions(ex: Throwable): ResponseEntity { Sentry.captureException(ex)