package com.qianwen.core.log.error;
|
|
import javax.servlet.Servlet;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import com.qianwen.core.log.exception.BizServiceException;
|
import com.qianwen.core.log.exception.ServiceException;
|
import com.qianwen.core.log.props.BladeRequestLogProperties;
|
import com.qianwen.core.log.publisher.ErrorLogPublisher;
|
import com.qianwen.core.secure.exception.SecureException;
|
import com.qianwen.core.tool.api.R;
|
import com.qianwen.core.tool.api.ResultCode;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.core.tool.utils.UrlUtil;
|
import com.qianwen.core.tool.utils.WebUtil;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.core.annotation.Order;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.servlet.DispatcherServlet;
|
|
@RestControllerAdvice
|
@Configuration(proxyBeanMethods = false)
|
@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
|
@Order
|
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
/* loaded from: blade-starter-log-9.3.0.0-SNAPSHOT.jar:org/springblade/core/log/error/BladeRestExceptionTranslator.class */
|
public class BladeRestExceptionTranslator {
|
private static final Logger log = LoggerFactory.getLogger(BladeRestExceptionTranslator.class);
|
private final BladeRequestLogProperties properties;
|
|
public BladeRestExceptionTranslator(final BladeRequestLogProperties properties) {
|
this.properties = properties;
|
}
|
|
@ExceptionHandler({BizServiceException.class})
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
public R handleError(BizServiceException e) {
|
log.error("自定义业务异常", e);
|
return R.fail(e.getResultCode(), e.getMessage());
|
}
|
|
@ExceptionHandler({ServiceException.class})
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
public R handleError(ServiceException e) {
|
log.error("业务异常", e);
|
return R.fail(e.getResultCode(), e.getMessage());
|
}
|
|
@ExceptionHandler({SecureException.class})
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
public R handleError(SecureException e) {
|
log.error("认证异常", e);
|
return R.fail(e.getResultCode(), e.getMessage());
|
}
|
|
@ExceptionHandler({Throwable.class})
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
public R handleError(Throwable e) {
|
log.error("服务器异常", e);
|
if (this.properties.getErrorLog().booleanValue()) {
|
ErrorLogPublisher.publishEvent(e, UrlUtil.getPath(WebUtil.getRequest().getRequestURI()));
|
}
|
Boolean isDisplayInternalServerError = false;
|
if (System.getProperties().getProperty("spring.profiles.active", "dev").equals("prod") || Func.isEmpty(e.getMessage())) {
|
isDisplayInternalServerError = Boolean.TRUE;
|
}
|
return R.fail(ResultCode.INTERNAL_SERVER_ERROR, isDisplayInternalServerError.booleanValue() ? ResultCode.INTERNAL_SERVER_ERROR.getMessage() : e.getMessage());
|
}
|
}
|