From 3394b01962bb51b5f968181685f6af89c1013214 Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Wed, 29 Nov 2023 22:14:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=9B=E5=87=BA=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/BusinessException.kt | 32 +++++++++++++++++++ ...sHttpRequestMethodNotSupportedException.kt | 18 +++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/main/kotlin/com/xlf/dromstarkotlin/exception/BusinessException.kt create mode 100644 src/main/kotlin/com/xlf/dromstarkotlin/exception/BusinessHttpRequestMethodNotSupportedException.kt diff --git a/src/main/kotlin/com/xlf/dromstarkotlin/exception/BusinessException.kt b/src/main/kotlin/com/xlf/dromstarkotlin/exception/BusinessException.kt new file mode 100644 index 0000000..f544c07 --- /dev/null +++ b/src/main/kotlin/com/xlf/dromstarkotlin/exception/BusinessException.kt @@ -0,0 +1,32 @@ +package com.xlf.dromstarkotlin.exception + +import com.frontleaves.general.utils.BaseResponse +import com.frontleaves.general.utils.ErrorCode +import com.frontleaves.general.utils.ResultUtil +import jakarta.servlet.http.HttpServletRequest +import org.springframework.http.ResponseEntity +import java.text.SimpleDateFormat +import java.util.* + +/** + * 业务自定义抛出 + * + * @since v1.0.0 + * @author 筱锋xiao_lfeng + */ +class BusinessException(): RuntimeException() { + constructor(errorCode: ErrorCode) : this() { + throw RuntimeException(errorCode.output) + } + + fun globalReturn(errorMessage: String?, errorCode: ErrorCode, httpServletRequest: HttpServletRequest): ResponseEntity { + return ResultUtil.error(errorCode, errorMessage, httpServletRequest) + } + + /** + * 返回前端信息 + */ + fun backInfo(errorCode: ErrorCode, httpServletRequest: HttpServletRequest): ResponseEntity { + return ResultUtil.error(errorCode, httpServletRequest) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/xlf/dromstarkotlin/exception/BusinessHttpRequestMethodNotSupportedException.kt b/src/main/kotlin/com/xlf/dromstarkotlin/exception/BusinessHttpRequestMethodNotSupportedException.kt new file mode 100644 index 0000000..2fa0f28 --- /dev/null +++ b/src/main/kotlin/com/xlf/dromstarkotlin/exception/BusinessHttpRequestMethodNotSupportedException.kt @@ -0,0 +1,18 @@ +package com.xlf.dromstarkotlin.exception + +import com.frontleaves.general.utils.BaseResponse +import com.frontleaves.general.utils.ErrorCode +import jakarta.servlet.http.HttpServletRequest +import org.springframework.http.ResponseEntity +import org.springframework.web.HttpRequestMethodNotSupportedException +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.RestControllerAdvice + +@RestControllerAdvice +class BusinessHttpRequestMethodNotSupportedException { + + @ExceptionHandler(HttpRequestMethodNotSupportedException::class) + fun handleCustomException(ignore: HttpRequestMethodNotSupportedException, httpServletRequest: HttpServletRequest): ResponseEntity { + return BusinessException().globalReturn(ignore.message, ErrorCode.METHOD_NOT_ALLOWED, httpServletRequest) + } +} \ No newline at end of file