|
|
@ -21,13 +21,13 @@ public class GlobalExceptionHandler { |
|
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
|
|
|
public Result<?> handleMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request) { |
|
|
|
String method = request.getMethod(); |
|
|
|
String uri = request.getRequestURI(); |
|
|
|
String url = request.getRequestURL().toString(); |
|
|
|
String allowed = ex.getSupportedHttpMethods() != null |
|
|
|
? ex.getSupportedHttpMethods().toString() |
|
|
|
: "[]"; |
|
|
|
String msg = String.format( |
|
|
|
"请求方法 '%s' 不支持 (URL: %s)。支持的方法有:%s", |
|
|
|
method, uri, allowed); |
|
|
|
method, url, allowed); |
|
|
|
log.error(msg, ex); |
|
|
|
return Result.failed(ResultCode.METHOD_NOT_ALLOWED.getCode(), msg); |
|
|
|
} |
|
|
@ -64,12 +64,18 @@ public class GlobalExceptionHandler { |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(Exception.class) |
|
|
|
public Result<?> handleException(Exception ex) { |
|
|
|
public Result<?> handleException(Exception ex, HttpServletRequest request) { |
|
|
|
// 获取请求方法和请求路径 |
|
|
|
String method = request.getMethod(); |
|
|
|
String url = request.getRequestURL().toString(); |
|
|
|
|
|
|
|
if (ex instanceof AppException ae) { |
|
|
|
log.warn("AppException:", ae); |
|
|
|
log.warn("AppException at {} {}:", method, url, ae); |
|
|
|
return Result.failed(ae.getResultCode()); |
|
|
|
} |
|
|
|
log.error("Unhandled exception:", ex); |
|
|
|
|
|
|
|
// 在日志中打印出请求地址 |
|
|
|
log.error("Unhandled exception at {} {}:", method, url, ex); |
|
|
|
return Result.failed(ResultCode.SYSTEM_ERROR.getCode(), ex.getMessage()); |
|
|
|
} |
|
|
|
|