From 6f3ec5a45a4985ea879638db529f71eaed4b3ca2 Mon Sep 17 00:00:00 2001 From: 05412 <2738076308@qq.com> Date: Thu, 25 Jul 2024 18:15:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=95=8F=E6=84=9F=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/dev/surl/surl/handler/DefaultExceptionHandler.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/surl/surl/handler/DefaultExceptionHandler.kt b/src/main/java/dev/surl/surl/handler/DefaultExceptionHandler.kt index 7fbdcd8..c6e0e4b 100644 --- a/src/main/java/dev/surl/surl/handler/DefaultExceptionHandler.kt +++ b/src/main/java/dev/surl/surl/handler/DefaultExceptionHandler.kt @@ -1,6 +1,7 @@ package dev.surl.surl.handler import dev.surl.surl.common.Msg +import dev.surl.surl.common.exception.UnauthorizedExcecption import dev.surl.surl.common.exception.UserRegistException import jakarta.validation.ConstraintViolationException import org.springframework.http.HttpHeaders @@ -67,7 +68,7 @@ class DefaultExceptionHandler : ResponseEntityExceptionHandler() { override fun handleHttpMessageNotReadable( ex: HttpMessageNotReadableException, headers: HttpHeaders, status: HttpStatusCode, request: WebRequest ): ResponseEntity { - return ResponseEntity(Msg(code = -1, msg = ex.message ?: "unknown error"), status) + return ResponseEntity(Msg(code = -1, msg = "invalid request body"), status) } /** @@ -96,4 +97,9 @@ class DefaultExceptionHandler : ResponseEntityExceptionHandler() { fun handleConstraintViolationException(ex: Exception): ResponseEntity> { return ResponseEntity(Msg(code = -1, msg = ex.message ?: "unknown validation error"), HttpStatus.BAD_REQUEST) } + + @ExceptionHandler(value = [UnauthorizedExcecption::class]) + fun handleUnauthorizedException(ex: Exception): ResponseEntity> { + return ResponseEntity(Msg(code = -1, msg = ex.message ?: "unauthorized"), HttpStatus.UNAUTHORIZED) + } } \ No newline at end of file