Exemplo n.º 1
0
 def test_with_nothing(self):
     hook = ErrorHook()
     app = namedtuple("App", "debug")
     content = hook.on_error(AssertionError(), app).content
     data = json.loads(content)
     assert data["code"] == 999
     assert data["message"] == "Unknown error"
Exemplo n.º 2
0
 def test_error_register(self):
     errors = {123: "测试错误"}
     ErrorHook.register(errors)
     hook = ErrorHook()
     app = namedtuple("App", "debug")
     content = hook.on_error(AssertionError(123), app).content
     data = json.loads(content)
     assert data["code"] == 123
     assert data["message"] == errors[123]
Exemplo n.º 3
0
 def test_only_message(self):
     hook = ErrorHook()
     app = namedtuple("App", "debug")
     app.debug = False
     content = hook.on_error(AssertionError("test error"), app).content
     data = json.loads(content)
     assert data["code"] == 999
     assert data["message"] == "test error"
     assert data["extra"] is None
     assert "detail" not in data
Exemplo n.º 4
0
def test_error_hook1():
    hook = ErrorHook()
    app = namedtuple("App", "debug")
    app.debug = True
    content = hook.on_error(AssertionError([123, "error", {"id": 1111}]), app).content
    data = json.loads(content)
    assert data["code"] == 123
    assert data["message"] == "error"
    assert data["extra"] == {"id": 1111}
    assert "detail" in data
Exemplo n.º 5
0
def test_error_hook5():
    hook = ErrorHook()
    app = namedtuple("App", "debug")
    app.debug = False
    content = hook.on_error(AssertionError(["test error", {"id": 2}]), app).content
    data = json.loads(content)
    assert data["code"] == 999
    assert data["message"] == "test error"
    assert data["extra"] == {"id": 2}
    assert "detail" not in data
Exemplo n.º 6
0
def test_error_hook3():
    hook = ErrorHook()
    app = namedtuple("App", "debug")
    app.debug = False
    content = hook.on_error(AssertionError(123), app).content
    data = json.loads(content)
    assert data["code"] == 123
    assert data["message"] == "Not configured error"
    assert data["extra"] is None
    assert "detail" not in data
Exemplo n.º 7
0
 def test_with_code_message_and_extra_not_debug(self):
     hook = ErrorHook()
     app = namedtuple("App", "debug")
     app.debug = False
     content = hook.on_error(AssertionError([123, "error", {
         "id": 1111
     }]), app).content
     data = json.loads(content)
     assert data["code"] == 123
     assert data["message"] == "error"
     assert data["extra"] == {"id": 1111}
     assert "detail" not in data
Exemplo n.º 8
0
def application(app_name,
                packages=None,
                static_url='/static/',
                settings_path="settings",
                debug=True,
                current_dir="."):
    """
       可以动态发现当前项目根目录下所有controller中的handler
    """
    logger = logging.getLogger(app_name)
    os.chdir(current_dir)
    with AppLogPainter(logger.debug, current_dir,
                       settings_path).paint() as routes:
        components = find_children(Component)
        # ComposeTypeComponent是用来兜底的,所以要放到最后
        components.append(ComposeTypeComponent())
        custom_hooks = sorted(find_children(Hook), key=lambda x: x.order)
        hooks = [
            WebContextHook(),
            AccessLogHook(),
            SessionHook(),
            ErrorHook()
        ] + custom_hooks
        app = FixedAsyncApp(routes,
                            template_dir=settings.get("TEMPLATE_DIR"),
                            static_dir=settings.get("STATIC_DIR"),
                            packages=packages,
                            schema_url=None,
                            docs_url=None,
                            static_url=static_url,
                            components=components,
                            event_hooks=hooks)
        app.debug = debug
        return app
Exemplo n.º 9
0
 def test_with_response(self):
     hook = ErrorHook()
     app = namedtuple("App", "debug")
     content = hook.on_error(Exception(Response(content="abc")),
                             app).content
     assert content == b"abc"