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

定义脚本执行 API 返回报错的静态替换模板,来给用户提供自定义错误消息的功能 #1806

Closed
ZhengshuaiPENG opened this issue Aug 5, 2022 · 1 comment
Assignees

Comments

@ZhengshuaiPENG
Copy link
Contributor

ZhengshuaiPENG commented Aug 5, 2022

背景

当前 Byzer 引擎执行脚本时,返回的报错信息大部分都是 stacktrace,可读性很差,用户无法获得具体信息

需求

做一套基于 文本的配置模板的方式,能够让用户定义 key - value,其中 key 可以匹配 api 返回中的error message 的文本,将value中的文件进行替代返回,增强 API 错误信息的可读性

注意

  • 该文本需要在 byzer.properties 中可配置,默认为 $BYZER_HOME/conf/err-msg-template.%{format}, format 可以在技术实现中定义
  • 需要考虑是否保留原始报错(比如 stacktrace)
    • 返回消息中只包含模板中定义的消息,完全替换原有报错信息
    • 返回消息中既能够包含新定义的消息,也包含原有报错信息,由api调用方自行选择是否展示
  • 要求将报错中出现的 MLSQL error, 统一替换为 Byzer-lang error
@chncaesar
Copy link
Contributor

chncaesar commented Aug 9, 2022

实现思路

Byzer-lang 提供了 ExceptionRender 接口,新类实现该接口,并注册至 ExceptionRenderManager/run/script 接口返回错误信息前,调用 ExceptionRenderManager.call() 方法,该方法会自动调用各个注册的实现类。从而达到灵活扩展的目的。

配置文件: $BYZER_HOME/conf/err-msg-template.json
json 格式:

[
  { "regexp": "MLSQL Parser error in .*?", "msg": "MLSQL Parser error"}
]

修改点如下:

  • 修改 Byzer-lang RestController/run/script , 同步和异步调用时都需要调用 ExceptionRenderManager.call()
  • Byzer-extension 新增 ExceptionRender 实现类,实现需求描述中的逻辑
  • 新增一个插件 App,启动 Byzer-lang 时注册至 ExceptionRenderManager

ExceptionRender 实现类主要逻辑:

  1. 读取 环境变量 $BYZER_HOME, 则打印日志继续。Fallback 至 Byzer-lang 的 DefaultExceptionRender
  2. is_match() 匹配函数,e.getMessageregexp 匹配,成功者取 msg 。由于 is_match(e: Exception): Boolean 限制,msg 保存在 ThreadLocal 对象。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants