Commit 382fb61b authored by jiaorz's avatar jiaorz

全局异常

parent 604e0d90
package com.github.wxiaoqi.security.common.config.global; package com.github.wxiaoqi.security.common.config.global;
import com.github.wxiaoqi.security.common.handler.GlobalExceptionHandler; import com.github.wxiaoqi.security.common.handler.GlobalExceptionHandler;
import com.github.wxiaoqi.security.common.handler.PlatformExceptionHandler;
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;
...@@ -21,6 +22,11 @@ public class GlobalBasicConfiguration implements ApplicationRunner{ ...@@ -21,6 +22,11 @@ public class GlobalBasicConfiguration implements ApplicationRunner{
private GlobalExceptionHandler addGlobalExceptionHandler() { private GlobalExceptionHandler addGlobalExceptionHandler() {
return new GlobalExceptionHandler(); return new GlobalExceptionHandler();
} }
@Bean
@ConditionalOnMissingBean
private PlatformExceptionHandler addPlatformExceptionHandler() {
return new PlatformExceptionHandler();
}
@Autowired @Autowired
private void setEnv(Environment env) { private void setEnv(Environment env) {
......
...@@ -6,7 +6,6 @@ import com.github.wxiaoqi.security.common.exception.auth.ClientTokenException; ...@@ -6,7 +6,6 @@ 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;
...@@ -18,7 +17,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -18,7 +17,7 @@ import javax.servlet.http.HttpServletResponse;
/** /**
* Created by ace on 2017/9/8. * Created by ace on 2017/9/8.
*/ */
@ControllerAdvice @ControllerAdvice("com.github.wxiaoqi.security")
@ResponseBody @ResponseBody
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
...@@ -48,29 +47,16 @@ public class GlobalExceptionHandler { ...@@ -48,29 +47,16 @@ 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(1001); response.setStatus(500);
} }
return new BaseResponse(ex.getStatus(), ex.getMessage()); return new BaseResponse(ex.getStatus(), ex.getMessage());
} }
//服务器异常
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public ObjectRestResponse<?> exceptionHandler(Exception e){ public BaseResponse otherExceptionHandler(HttpServletResponse response, Exception ex) {
Throwable cause = e.getCause(); response.setStatus(500);
if(cause != null && cause.toString().contains("Exception")) { logger.error(ex.getMessage(),ex);
logger.error(cause.getMessage(), e); return new BaseResponse(CommonConstants.EX_OTHER_CODE, ex.getMessage());
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());
}
} }
...@@ -3,10 +3,13 @@ package com.github.wxiaoqi.security.common.handler; ...@@ -3,10 +3,13 @@ package com.github.wxiaoqi.security.common.handler;
import com.github.wxiaoqi.security.common.exception.BaseException; import com.github.wxiaoqi.security.common.exception.BaseException;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice("com.xxfc.platform") @RestControllerAdvice("com.xxfc.platform")
@Slf4j
public class PlatformExceptionHandler { public class PlatformExceptionHandler {
@ExceptionHandler(value = {BaseException.class}) @ExceptionHandler(value = {BaseException.class})
...@@ -18,4 +21,25 @@ public class PlatformExceptionHandler { ...@@ -18,4 +21,25 @@ public class PlatformExceptionHandler {
return new BaseResponse(400,e.getMessage()); return new BaseResponse(400,e.getMessage());
} }
//服务器异常
@ExceptionHandler(Exception.class)
public ObjectRestResponse<?> exceptionHandler(Exception e){
Throwable cause = e.getCause();
if(cause != null && cause.toString().contains("Exception")) {
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());
}
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());
}
} }
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