kotlin项目工程中统一异常处理类 末蓝、 2022-09-11 10:23 141阅读 0赞 程序开发中,将异常做统一的处理,将提示语区分中英文,放在国际化的资源类中。 ![在这里插入图片描述][60d3bf87bc6b48bdbc5977f67f881b7a.png]![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBATW9ua2V5S2luZ19zdW55dWh1YQ_size_7_color_FFFFFF_t_70_g_se_x_16] ![在这里插入图片描述][d46741d655b94e9d83adcdf5a1885edb.png] ## 1、异常的基础类,继承RuntimeException ## import mu.KotlinLogging import java.io.Serializable import java.lang.RuntimeException /** * 异常的基础类,继承RuntimeException * @program: EXceptionDemo * @description: BaseException * @author: sunyuhua * @create: 2021-09-13 17:19 */ open class BaseException : RuntimeException, Serializable { private val logger = KotlinLogging.logger{ } constructor() { } constructor(message: String?) : super(message) { logger.error(message) } constructor(cause: Throwable?) : super(cause) { } constructor(message: String?, cause: Throwable?) : super(message, cause) { logger.error("$message==$cause") } } ## 2、业务处理异常 ## import com.fastonetech.util.Result /** * 业务处理异常 * 指用户输入了非法数据等业务逻辑存在的异常 * @program: EXceptionDemo * @description: BusinessException * @author: sunyuhua * @create: 2021-09-14 10:04 */ class BusinessException : BaseException { var result = Result() constructor(result: Result) : super("${ result.code}:${ result.msg}") { this.result = result } constructor(code: Int, msg: String) : super("$code:$msg") { result.code = code result.msg = msg } constructor(result: Result, cause: Throwable?) : super("${ result.code}:${ result.msg}", cause) { this.result = result } constructor(code: Int, msg: String, cause: Throwable?) : super("$code:$msg", cause) { result.code = code result.msg = msg } } ## 3、系统级异常 ## import com.fastonetech.util.Result /** * 系统级异常。 * 指系统级别的,如:网络通信时连接中断、系统连接、超时等异常 * @program: EXceptionDemo * @description: SystemException * @author: sunyuhua * @create: 2021-09-14 10:00 */ class SystemException : BaseException { var result = Result() constructor(result: Result) : super("${ result.code}:${ result.msg}") { this.result = result } constructor(code: Int, msg: String) : super("$code:$msg") { result.code = code result.msg = msg } constructor(result: Result, cause: Throwable?) : super("${ result.code}:${ result.msg}", cause) { this.result = result } constructor(code: Int, msg: String, cause: Throwable?) : super("$code:$msg", cause) { result.code = code result.msg = msg } } ## 4、异常错误信息返回的实体类 ## import lombok.Data import lombok.ToString import java.io.Serializable /** * 异常错误信息返回的实体类 * @program: EXceptionDemo * @description: Result * @author: sunyuhua * @create: 2021-09-14 10:01 */ @Data @ToString class Result : Serializable { //状态码 var code: Int? = null //提示信息 var msg: String? = null //结果数据 var data: Any? = null constructor() { } constructor(code: Int?, msg: String?) { this.code = code this.msg = msg } } ## 5、全局异常捕获处理 ## import org.springframework.web.bind.annotation.ControllerAdvice import com.fastonetech.exception.BusinessException import org.springframework.http.ResponseEntity import com.fastonetech.exception.SystemException import mu.KotlinLogging import org.springframework.http.HttpStatus import org.springframework.web.bind.annotation.ExceptionHandler /** * 全局异常捕获处理 * @program: EXceptionDemo * @description: GlobalExceptionHandler * @author: sunyuhua * @create: 2021-09-14 10:11 */ @ControllerAdvice class GlobalExceptionHandler { private val logger = KotlinLogging.logger{ } /** * 业务逻辑异常。 * HTTP响应状态为200 * @param businessException * @return */ @ExceptionHandler(value = [BusinessException::class]) fun businessExceptionHandler(businessException: BusinessException): ResponseEntity<*> { val result = businessException.result logger.error(businessException.printStackTrace().toString()) return ResponseEntity<Any?>(result, HttpStatus.OK) } /** * 系统异常。 * HTTP响应状态为400 * @param systemException * @return */ @ExceptionHandler(value = [SystemException::class]) fun systemExceptionHandler(systemException: SystemException): ResponseEntity<*> { val result = systemException.result logger.error(systemException.printStackTrace().toString()) return ResponseEntity<Any?>(result, HttpStatus.BAD_REQUEST) } } ## 6、国际化能力 ## import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import java.util.Locale; /** * 国际化的配置 * @program: balance * @description: LocaleConfig * @author: sunyuhua * @create: 2021-09-14 14:54 **/ @Configuration public class LocaleConfig { /** * 默认解析器 其中locale表示默认语言 */ @Bean public LocaleResolver localeResolver() { SessionLocaleResolver localeResolver = new SessionLocaleResolver(); localeResolver.setDefaultLocale(Locale.CHINA); return localeResolver; } /** * 默认拦截器 其中lang表示切换语言的参数名 */ @Bean public WebMvcConfigurer localeInterceptor() { return new WebMvcConfigurer() { @Override public void addInterceptors(InterceptorRegistry registry) { LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); localeInterceptor.setParamName("lang"); registry.addInterceptor(localeInterceptor); } }; } } ## 7、国际化文件 ## 300301 = 线下转账出现异常,请检查账户信息 300302 = 充值的订单号不能为空,请检查 300303 = 租户Id不能为空,请检查 300304 = 消费的订单Id不能为空,请检查 300401 = 支付宝支付,支付校验,out_trade_no错误 300402 = 支付宝支付,支付校验,total_amount错误 ## 8、工程中引用的代码 ## ![在这里插入图片描述][4ff8a416f75b411b9d07ebc61357fec0.png] [60d3bf87bc6b48bdbc5977f67f881b7a.png]: https://img-blog.csdnimg.cn/60d3bf87bc6b48bdbc5977f67f881b7a.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBATW9ua2V5S2luZ19zdW55dWh1YQ_size_7_color_FFFFFF_t_70_g_se_x_16]: /images/20220828/f77704cc1e694b13a157f7099de3ccbc.png [d46741d655b94e9d83adcdf5a1885edb.png]: /images/20220828/861d17f5db664756bdbb933d7a872f05.png [4ff8a416f75b411b9d07ebc61357fec0.png]: /images/20220828/5a0006690e944b6ebf94f3777f81b18e.png
还没有评论,来说两句吧...