¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: Chill åºéª (smallchill@163.com) |
| | | */ |
| | | package org.springblade.core.log.error; |
| | | |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.log.props.BladeRequestLogProperties; |
| | | import org.springblade.core.log.publisher.ErrorLogPublisher; |
| | | import org.springblade.core.secure.exception.SecureException; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.api.ResultCode; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.core.tool.utils.UrlUtil; |
| | | import org.springblade.core.tool.utils.WebUtil; |
| | | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| | | 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; |
| | | |
| | | import javax.servlet.Servlet; |
| | | |
| | | /** |
| | | * æªç¥å¼å¸¸è½¬è¯ååéï¼æ¹ä¾¿çå¬ï¼å¯¹æªç¥å¼å¸¸ç»ä¸å¤çãOrder æåºä¼å
çº§ä½ |
| | | * |
| | | * @author Chill |
| | | */ |
| | | @Slf4j |
| | | @Order |
| | | @RequiredArgsConstructor |
| | | @AutoConfiguration |
| | | @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) |
| | | @ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) |
| | | @RestControllerAdvice |
| | | public class BladeRestExceptionTranslator { |
| | | |
| | | private final BladeRequestLogProperties properties; |
| | | |
| | | @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 (properties.getErrorLog()) { |
| | | //åéæå¡å¼å¸¸äºä»¶ |
| | | ErrorLogPublisher.publishEvent(e, UrlUtil.getPath(WebUtil.getRequest().getRequestURI())); |
| | | } |
| | | return R.fail(ResultCode.INTERNAL_SERVER_ERROR, (Func.isEmpty(e.getMessage()) ? ResultCode.INTERNAL_SERVER_ERROR.getMessage() : e.getMessage())); |
| | | } |
| | | |
| | | } |