From 2b1f7a47394363e95deb4dfa0f1c67d41e747f7f Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期三, 01 二月 2023 10:56:40 +0800 Subject: [PATCH] Merge branch 'wvp-28181-2.0' into fix-269 --- src/main/java/com/genersoft/iot/vmp/conf/GlobalExceptionHandler.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 57 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/conf/GlobalExceptionHandler.java b/src/main/java/com/genersoft/iot/vmp/conf/GlobalExceptionHandler.java new file mode 100644 index 0000000..728afb9 --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/conf/GlobalExceptionHandler.java @@ -0,0 +1,57 @@ +package com.genersoft.iot.vmp.conf; + +import com.genersoft.iot.vmp.conf.exception.ControllerException; +import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; +import com.genersoft.iot.vmp.vmanager.bean.WVPResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * 鍏ㄥ眬寮傚父澶勭悊 + */ +@RestControllerAdvice +public class GlobalExceptionHandler { + + private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + /** + * 榛樿寮傚父澶勭悊 + * @param e 寮傚父 + * @return 缁熶竴杩斿洖缁撴灉 + */ + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public WVPResult<String> exceptionHandler(Exception e) { + logger.error("[鍏ㄥ眬寮傚父]锛� ", e); + return WVPResult.fail(ErrorCode.ERROR500.getCode(), e.getMessage()); + } + + + /** + * 鑷畾涔夊紓甯稿鐞嗭紝 澶勭悊controller涓繑鍥炵殑閿欒 + * @param e 寮傚父 + * @return 缁熶竴杩斿洖缁撴灉 + */ + @ExceptionHandler(ControllerException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public ResponseEntity<WVPResult<String>> exceptionHandler(ControllerException e) { + return new ResponseEntity<>(WVPResult.fail(e.getCode(), e.getMsg()), HttpStatus.OK); + } + + /** + * 鐧婚檰澶辫触 + * @param e 寮傚父 + * @return 缁熶竴杩斿洖缁撴灉 + */ + @ExceptionHandler(BadCredentialsException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public ResponseEntity<WVPResult<String>> exceptionHandler(BadCredentialsException e) { + return new ResponseEntity<>(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMessage()), HttpStatus.OK); + } +} -- Gitblit v1.8.0