mirror of https://github.com/longtai-cn/hippo4j
parent
de18fd509e
commit
fd4cb46158
@ -0,0 +1,48 @@
|
||||
package cn.hippo4j.console.config;
|
||||
|
||||
import cn.hippo4j.common.web.base.Result;
|
||||
import cn.hippo4j.common.web.base.Results;
|
||||
import cn.hippo4j.common.web.exception.ServiceException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 全局异常捕获器.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/11/18 22:18
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = {ServiceException.class})
|
||||
public Result serviceException(HttpServletRequest request, ServiceException ex) {
|
||||
if (ex.getCause() != null) {
|
||||
log.error("[{}] {} [ex] {}", request.getMethod(), request.getRequestURL().toString(), ex.toString(), ex.getCause());
|
||||
return Results.failure(ex);
|
||||
}
|
||||
|
||||
log.info("[{}] {} [ex] {}", request.getMethod(), request.getRequestURL().toString(), ex.toString());
|
||||
return Results.failure(ex);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = Throwable.class)
|
||||
public Result defaultErrorHandler(HttpServletRequest request, Throwable throwable) {
|
||||
log.error("[{}] {} ", request.getMethod(), getUrl(request), throwable);
|
||||
return Results.failure(throwable);
|
||||
}
|
||||
|
||||
private String getUrl(HttpServletRequest request) {
|
||||
if (StringUtils.isEmpty(request.getQueryString())) {
|
||||
return request.getRequestURL().toString();
|
||||
}
|
||||
|
||||
return request.getRequestURL().toString() + "?" + request.getQueryString();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue