Commit 48ee55a1 authored by jiaorz's avatar jiaorz

全局异常

parent e2374f7e
package com.github.wxiaoqi.security.common.config.global; package com.github.wxiaoqi.security.common.config.global;
import com.github.wxiaoqi.security.common.exception.GlobalExceptionHandler; import com.github.wxiaoqi.security.common.handler.GlobalExceptionHandler;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
......
package com.github.wxiaoqi.security.common.exception;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局异常
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
{
log.info("[初始化成功] --- GlobalExceptionHandler");
}
protected ObjectRestResponse<?> assembleResult(ObjectRestResponse<?> error, String msg) {
log.debug("Exception: " + msg);
return error;
}
protected ObjectRestResponse<?> assembleResult(ObjectRestResponse<?> error, Throwable e) {
return assembleResult(error, e.getClass().getSimpleName() + ": " + e.getMessage());
}
//服务器异常
@ExceptionHandler(Exception.class)
public ObjectRestResponse<?> exceptionHandler(Exception e){
Throwable cause = e.getCause();
if(cause != null && cause.toString().contains("com.netflix.client.ClientException")) {
log.error(cause.getMessage(), e);
return assembleResult(ObjectRestResponse.createFailedResult(5000, "服务器开小差了,请稍后重试!"), "Server exception: " + e.getMessage());
}
log.error("Server exception: ", e);
return assembleResult(ObjectRestResponse.createFailedResult(5000, "服务器开小差了,请稍后重试!"), "Server exception: " + e.getMessage());
}
}
\ No newline at end of file
...@@ -6,6 +6,7 @@ import com.github.wxiaoqi.security.common.exception.auth.ClientTokenException; ...@@ -6,6 +6,7 @@ import com.github.wxiaoqi.security.common.exception.auth.ClientTokenException;
import com.github.wxiaoqi.security.common.exception.auth.UserInvalidException; import com.github.wxiaoqi.security.common.exception.auth.UserInvalidException;
import com.github.wxiaoqi.security.common.exception.auth.UserTokenException; import com.github.wxiaoqi.security.common.exception.auth.UserTokenException;
import com.github.wxiaoqi.security.common.msg.BaseResponse; import com.github.wxiaoqi.security.common.msg.BaseResponse;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
...@@ -47,16 +48,29 @@ public class GlobalExceptionHandler { ...@@ -47,16 +48,29 @@ public class GlobalExceptionHandler {
public BaseResponse baseExceptionHandler(HttpServletResponse response, BaseException ex) { public BaseResponse baseExceptionHandler(HttpServletResponse response, BaseException ex) {
logger.error(ex.getMessage(),ex); logger.error(ex.getMessage(),ex);
if(0 == ex.getStatus()) { if(0 == ex.getStatus()) {
response.setStatus(500); response.setStatus(1001);
} }
return new BaseResponse(ex.getStatus(), ex.getMessage()); return new BaseResponse(ex.getStatus(), ex.getMessage());
} }
//服务器异常
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public BaseResponse otherExceptionHandler(HttpServletResponse response, Exception ex) { public ObjectRestResponse<?> exceptionHandler(Exception e){
response.setStatus(500); Throwable cause = e.getCause();
logger.error(ex.getMessage(),ex); if(cause != null && cause.toString().contains("com.netflix.client.ClientException")) {
return new BaseResponse(CommonConstants.EX_OTHER_CODE, ex.getMessage()); logger.error(cause.getMessage(), e);
return assembleResult(ObjectRestResponse.createFailedResult(5000, "服务器开小差了,请稍后重试!"), "Server exception: " + e.getMessage());
}
logger.error("Server exception: ", e);
return assembleResult(ObjectRestResponse.createFailedResult(5000, "服务器开小差了,请稍后重试!"), "Server exception: " + e.getMessage());
} }
protected ObjectRestResponse<?> assembleResult(ObjectRestResponse<?> error, String msg) {
logger.debug("Exception: " + msg);
return error;
}
protected ObjectRestResponse<?> assembleResult(ObjectRestResponse<?> error, Throwable e) {
return assembleResult(error, e.getClass().getSimpleName() + ": " + e.getMessage());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment