yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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());
    }
}