抛出处理

This commit is contained in:
筱锋xiao_lfeng 2023-11-29 22:14:24 +08:00
parent 0ece4ca76d
commit 3394b01962
2 changed files with 50 additions and 0 deletions

View File

@ -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<BaseResponse> {
return ResultUtil.error(errorCode, errorMessage, httpServletRequest)
}
/**
* 返回前端信息
*/
fun backInfo(errorCode: ErrorCode, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
return ResultUtil.error(errorCode, httpServletRequest)
}
}

View File

@ -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<BaseResponse> {
return BusinessException().globalReturn(ignore.message, ErrorCode.METHOD_NOT_ALLOWED, httpServletRequest)
}
}