dbt exceptions macro 简单说明

dbt exceptions 实际属于一个namespace 变量可以进行一些代码可控的异常处理以及raise 以及warn

参考使用

  • raise_compiler_error 使用
{% if number < 0 or number > 100 %}
  {{ exceptions.raise_compiler_error("Invalid `number`. Got: " ~ number) }}
{% endif %}

内部实现

属于一个上下文属性

  • 参考代码
@contextproperty()
def exceptions(self) -> Dict[str, Any]:
    """The exceptions namespace can be used to raise warnings and errors in
    dbt userspace.
 
 
    ## raise_compiler_error
 
    The `exceptions.raise_compiler_error` method will raise a compiler
    error with the provided message. This is typically only useful in
    macros or materializations when invalid arguments are provided by the
    calling model. Note that throwing an exception will cause a model to
    fail, so please use this variable with care!
 
    Example usage:
 
    > exceptions.sql:
 
        {% if number < 0 or number > 100 %}
          {{ exceptions.raise_compiler_error("Invalid `number`. Got: " ~ number) }}
        {% endif %}
 
    ## warn
 
    The `exceptions.warn` method will raise a compiler warning with the
    provided message. If the `--warn-error` flag is provided to dbt, then
    this warning will be elevated to an exception, which is raised.
 
    Example usage:
 
    > warn.sql:
 
        {% if number < 0 or number > 100 %}
          {% do exceptions.warn("Invalid `number`. Got: " ~ number) %}
        {% endif %}
    """  # noqa
    return wrapped_exports(self.model)
  • wrapped_exports 处理
    实际上就是一个装饰器,可以将信息包装起来,方便显示
def wrapper(model):
    def wrap(func):
        @functools.wraps(func)
        def inner(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except DbtRuntimeError as exc:
                exc.add_node(model)
                raise exc
 
        return inner
 
    return wrap
 
 
def wrapped_exports(model):
    wrap = wrapper(model)
    return {name: wrap(export) for name, export in CONTEXT_EXPORTS.items()}

说明

exceptions 对于我们可以稳健的模型还是比较方便的,可以尽早失败,dbt 的return 实现了一种尽早返回数据的能力

参考资料

https://docs.getdbt.com/reference/dbt-jinja-functions/exceptions

posted on   荣锋亮  阅读(10)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-04-22 keydb 提供的ModJS 模块
2022-04-22 如何编写一个简单的TypeScriptToLua lua 模块定义包
2022-04-22 spring security nginx https proxy 问题
2021-04-22 cube.js 调度&&查询队参考参数
2021-04-22 cube.js 自定义首页显示信息
2020-04-22 How We Spotted and Fixed a Performance Degradation in Our Python Code
2020-04-22 nodejs 基础镜像的一个参考

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示