Commit e2374f7e authored by jiaorz's avatar jiaorz

全局异常

parent 08135dcc
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 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;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
...@@ -13,6 +16,12 @@ public class GlobalBasicConfiguration implements ApplicationRunner{ ...@@ -13,6 +16,12 @@ public class GlobalBasicConfiguration implements ApplicationRunner{
private static Logger log = LoggerFactory.getLogger(GlobalBasicConfiguration.class); private static Logger log = LoggerFactory.getLogger(GlobalBasicConfiguration.class);
private static Environment env; private static Environment env;
@Bean
@ConditionalOnMissingBean
private GlobalExceptionHandler addGlobalExceptionHandler() {
return new GlobalExceptionHandler();
}
@Autowired @Autowired
private void setEnv(Environment env) { private void setEnv(Environment env) {
GlobalBasicConfiguration.env = env; GlobalBasicConfiguration.env = env;
......
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
...@@ -88,7 +88,9 @@ public class CommonLogServiceImpl implements CommonLogService { ...@@ -88,7 +88,9 @@ public class CommonLogServiceImpl implements CommonLogService {
} }
@Override @Override
public void run() { public void run() {
log.info("用户请求信息:xxLogEntity = {}", xxLogEntity.toString()); if(!xxLogEntity.getServletPath().contains("/vehicleInfo/app/unauth/getVehiclePlanList")) {
log.info("用户请求信息:xxLogEntity = {}", xxLogEntity.toString());
}
mongoTemplate.insert(xxLogEntity, xxLogEntity.getMongoKey()); mongoTemplate.insert(xxLogEntity, xxLogEntity.getMongoKey());
} }
} }
......
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