コード例 #1
0
def test_get_exc_type(exc, type_):
    if isinstance(type_, str):
        assert get_exc_type(type(exc)) == type_
    else:
        with pytest.raises(type_) as exc_info:
            get_exc_type(type(exc))
        assert isinstance(exc_info.value, type_)
コード例 #2
0
def error_dict(exc: Exception, config: Type["BaseConfig"],
               loc: "Loc") -> Dict[str, Any]:
    type_ = get_exc_type(exc.__class__)
    msg_template = (
        config.error_msg_templates.get(f"{type_}.{'.'.join(map(str, loc))}")
        or config.error_msg_templates.get(type_)
        or getattr(exc, "msg_template", None))
    ctx = exc.__dict__
    if msg_template:
        msg = msg_template.format(**ctx)
    else:
        msg = str(exc)

    d: Dict[str, Any] = {"loc": loc, "msg": msg, "type": type_}

    if ctx:
        d["ctx"] = ctx

    return d