Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于自定义 success、exception 和全局异常处理的顺序问题 #82

Open
duanluan opened this issue Aug 1, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@duanluan
Copy link
Contributor

duanluan commented Aug 1, 2023

目前createLog方法中LogDTO.exception获取的仅为e.getMessage(),而我想获取的是e.printStackTrace()的内容。

大多项目都会有一个全局异常处理。
GlobalExceptionHandler:

/**
 * 全局异常处理
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler  {

  @ExceptionHandler(value = Exception.class)
  @ResponseBody
  public ApiResponse<String> handlerException(Exception e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    LogRecordContext.putVariable("exception", sw.toString());

    // ……
  }
  1. 上文代码中的LogRecordContext.putVariable("exception", sw.toString());仅为我的猜想,能否通过这种方式去设置LogDTO.exception的值(put 中可以特殊点叫 $exception 或者加新方法),不然每个@OperationLog都要加属性(当然现在还没有可以配置exception的属性)。

  2. 假设可以自定义LogDTO.exception的值的话,GlobalExceptionHandler.handlerException的执行顺序也总是在createLog方法后,@Order注解无效。或者 LogDTO 可以获取到 Exception 对象也行。

@qqxx6661
Copy link
Owner

qqxx6661 commented Aug 3, 2023

收到了

@qqxx6661 qqxx6661 added the enhancement New feature or request label Aug 3, 2023
@xiangzi1
Copy link

这个现在有解决方案吗?我也遇到了同样的问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants