mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-26 09:20:50 +00:00
fix: 通用异常处理
This commit is contained in:
parent
f5a9a7423f
commit
0afc7b1bbf
@ -3,11 +3,19 @@ package com.java2nb.novel.core.advice;
|
||||
import io.github.xxyopen.model.resp.RestResult;
|
||||
import io.github.xxyopen.model.resp.SysResultCode;
|
||||
import io.github.xxyopen.web.exception.BusinessException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* 统一异常处理器
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class CommonExceptionHandler {
|
||||
@ -27,9 +35,21 @@ public class CommonExceptionHandler {
|
||||
return RestResult.fail(e.getResultCode());
|
||||
}
|
||||
|
||||
@ExceptionHandler({Exception.class})
|
||||
public RestResult<Void> handlerException(Exception e) {
|
||||
@ExceptionHandler(Exception.class)
|
||||
public Object handleException(HttpServletRequest request, Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return RestResult.error();
|
||||
if (isJsonRequest(request)) {
|
||||
// 如果是REST请求,返回JSON格式的错误响应
|
||||
return RestResult.error();
|
||||
} else {
|
||||
//跳转页面过程中出现异常时统一跳转到404页面
|
||||
return new ModelAndView("404");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isJsonRequest(HttpServletRequest request) {
|
||||
String acceptHeader = request.getHeader("Accept");
|
||||
return acceptHeader != null && acceptHeader.contains(MediaType.APPLICATION_JSON_VALUE);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user