|
@@ -1,10 +1,10 @@
|
|
-package cn.jlsxwkj.common.R;
|
|
|
|
|
|
+package cn.jlsxwkj.common.handler;
|
|
|
|
|
|
import cn.dev33.satoken.exception.SaTokenException;
|
|
import cn.dev33.satoken.exception.SaTokenException;
|
|
|
|
+import cn.jlsxwkj.common.R.ResponseError;
|
|
import cn.jlsxwkj.common.utils.Log;
|
|
import cn.jlsxwkj.common.utils.Log;
|
|
import cn.jlsxwkj.moudles.logerror.LogError;
|
|
import cn.jlsxwkj.moudles.logerror.LogError;
|
|
import cn.jlsxwkj.moudles.logerror.LogErrorService;
|
|
import cn.jlsxwkj.moudles.logerror.LogErrorService;
|
|
-import io.netty.handler.codec.http.HttpResponseStatus;
|
|
|
|
import jakarta.annotation.Resource;
|
|
import jakarta.annotation.Resource;
|
|
import org.springframework.web.ErrorResponse;
|
|
import org.springframework.web.ErrorResponse;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
@@ -28,30 +28,31 @@ public class GlobalRestExceptionHandler {
|
|
* @return 封装异常对象
|
|
* @return 封装异常对象
|
|
*/
|
|
*/
|
|
@ExceptionHandler(Throwable.class)
|
|
@ExceptionHandler(Throwable.class)
|
|
- public ErrorHandler exception(Exception e) {
|
|
|
|
- ErrorHandler errorHandler = new ErrorHandler();
|
|
|
|
- errorHandler.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
|
|
|
|
- errorHandler.setMessage("未知异常");
|
|
|
|
- errorHandler.setException(e.getClass().getName());
|
|
|
|
|
|
+ public ResponseError exception(Exception e) {
|
|
|
|
+ ResponseError errorHandler = ResponseError.data(e.getClass().getName());
|
|
|
|
+
|
|
if (e instanceof LoginException) {
|
|
if (e instanceof LoginException) {
|
|
errorHandler.setMessage(e.getMessage());
|
|
errorHandler.setMessage(e.getMessage());
|
|
}
|
|
}
|
|
if (e instanceof ErrorResponse) {
|
|
if (e instanceof ErrorResponse) {
|
|
- errorHandler.setStatus(((ErrorResponse)e).getStatusCode().value());
|
|
|
|
|
|
+ errorHandler.setCode(((ErrorResponse)e).getStatusCode().value());
|
|
errorHandler.setMessage(e.getMessage());
|
|
errorHandler.setMessage(e.getMessage());
|
|
}
|
|
}
|
|
if (e instanceof SaTokenException) {
|
|
if (e instanceof SaTokenException) {
|
|
- errorHandler.setStatus(((SaTokenException)e).getCode());
|
|
|
|
|
|
+ errorHandler.setCode(((SaTokenException)e).getCode());
|
|
errorHandler.setMessage("账户未登录");
|
|
errorHandler.setMessage("账户未登录");
|
|
}
|
|
}
|
|
|
|
+
|
|
LogError errorHandlerToLogError = new LogError().castErrorHandlerToLogError(errorHandler);
|
|
LogError errorHandlerToLogError = new LogError().castErrorHandlerToLogError(errorHandler);
|
|
errorHandlerToLogError.setErrorInfo(e.getMessage());
|
|
errorHandlerToLogError.setErrorInfo(e.getMessage());
|
|
errorHandlerToLogError.setErrorStackTrace(Arrays.toString(e.getStackTrace()));
|
|
errorHandlerToLogError.setErrorStackTrace(Arrays.toString(e.getStackTrace()));
|
|
logErrorService.insertOne(errorHandlerToLogError);
|
|
logErrorService.insertOne(errorHandlerToLogError);
|
|
- Log.error(e.getClass(), "Code ====> {}", errorHandler.getStatus());
|
|
|
|
|
|
+
|
|
|
|
+ Log.error(e.getClass(), "Code ====> {}", errorHandler.getCode());
|
|
Log.error(e.getClass(), "Exception ====> {}", errorHandler.getException());
|
|
Log.error(e.getClass(), "Exception ====> {}", errorHandler.getException());
|
|
Log.error(e.getClass(), "Message ====> {}", errorHandler.getMessage());
|
|
Log.error(e.getClass(), "Message ====> {}", errorHandler.getMessage());
|
|
Log.error(e.getClass(), "ErrInfo ====> {}", e.getMessage());
|
|
Log.error(e.getClass(), "ErrInfo ====> {}", e.getMessage());
|
|
|
|
+
|
|
return errorHandler;
|
|
return errorHandler;
|
|
}
|
|
}
|
|
}
|
|
}
|