當前位置: 首頁>>代碼示例>>Python>>正文


Python exceptions.InvalidUsage方法代碼示例

本文整理匯總了Python中sanic.exceptions.InvalidUsage方法的典型用法代碼示例。如果您正苦於以下問題:Python exceptions.InvalidUsage方法的具體用法?Python exceptions.InvalidUsage怎麽用?Python exceptions.InvalidUsage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sanic.exceptions的用法示例。


在下文中一共展示了exceptions.InvalidUsage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _500

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def _500(request, exception):
    error_id = secrets.token_urlsafe(32)
    if exception.__class__ is UserException:
        debug("User exception: %s" % exception.message, exc_info=True)
        message = exception.message
    elif exception.__class__ is json.JSONDecodeError:
        debug(ERROR_INVALID_JSON, exc_info=True,error_id=error_id)
        message = ERROR_INVALID_JSON
    elif exception.__class__ is InvalidUsage :
        debug(ERROR_INVALID_USAGE, exc_info=True)
        message = ERROR_INVALID_USAGE
    else:
        warning("Exception in API", exc_info=True)
        message = ERROR_TEXT
    return jsonify({'success': False, 'result': {'message': message,'errorId':error_id}},
                   status=500, headers=generate_cors_headers(request)) 
開發者ID:bloomsburyai,項目名稱:cape-webservices,代碼行數:18,代碼來源:errors_core.py

示例2: handler_1

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def handler_1(request):
    raise InvalidUsage("OK") 
開發者ID:huge-success,項目名稱:sanic,代碼行數:4,代碼來源:test_exceptions_handler.py

示例3: test_composition_view_rejects_incorrect_methods

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def test_composition_view_rejects_incorrect_methods():
    def foo(request):
        return text("Foo")

    view = CompositionView()

    with pytest.raises(InvalidUsage) as e:
        view.add(["GET", "FOO"], foo)

    assert str(e.value) == "FOO is not a valid HTTP method." 
開發者ID:huge-success,項目名稱:sanic,代碼行數:12,代碼來源:test_views.py

示例4: test_composition_view_rejects_duplicate_methods

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def test_composition_view_rejects_duplicate_methods():
    def foo(request):
        return text("Foo")

    view = CompositionView()

    with pytest.raises(InvalidUsage) as e:
        view.add(["GET", "POST", "GET"], foo)

    assert str(e.value) == "Method GET is already registered." 
開發者ID:huge-success,項目名稱:sanic,代碼行數:12,代碼來源:test_views.py

示例5: test_improper_websocket_connection

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def test_improper_websocket_connection(transport, send, receive):
    with pytest.raises(InvalidUsage):
        transport.get_websocket_connection()

    transport.create_websocket_connection(send, receive)
    connection = transport.get_websocket_connection()
    assert isinstance(connection, WebSocketConnection) 
開發者ID:huge-success,項目名稱:sanic,代碼行數:9,代碼來源:test_asgi.py

示例6: test_bp_exception_handler

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def test_bp_exception_handler(app):
    blueprint = Blueprint("test_middleware")

    @blueprint.route("/1")
    def handler_1(request):
        raise InvalidUsage("OK")

    @blueprint.route("/2")
    def handler_2(request):
        raise ServerError("OK")

    @blueprint.route("/3")
    def handler_3(request):
        raise NotFound("OK")

    @blueprint.exception(NotFound, ServerError)
    def handler_exception(request, exception):
        return text("OK")

    app.blueprint(blueprint)

    request, response = app.test_client.get("/1")
    assert response.status == 400

    request, response = app.test_client.get("/2")
    assert response.status == 200
    assert response.text == "OK"

    request, response = app.test_client.get("/3")
    assert response.status == 200 
開發者ID:huge-success,項目名稱:sanic,代碼行數:32,代碼來源:test_blueprints.py

示例7: add

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def add(self, methods, handler, stream=False):
        if stream:
            handler.is_stream = stream
        for method in methods:
            if method not in HTTP_METHODS:
                raise InvalidUsage(f"{method} is not a valid HTTP method.")

            if method in self.handlers:
                raise InvalidUsage(f"Method {method} is already registered.")
            self.handlers[method] = handler 
開發者ID:huge-success,項目名稱:sanic,代碼行數:12,代碼來源:views.py

示例8: load_json

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def load_json(self, loads=json_loads):
        try:
            self.parsed_json = loads(self.body)
        except Exception:
            if not self.body:
                return None
            raise InvalidUsage("Failed when parsing body as json")

        return self.parsed_json 
開發者ID:huge-success,項目名稱:sanic,代碼行數:11,代碼來源:request.py

示例9: get_websocket_connection

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def get_websocket_connection(self) -> WebSocketConnection:
        try:
            return self._websocket_connection
        except AttributeError:
            raise InvalidUsage("Improper websocket connection.") 
開發者ID:huge-success,項目名稱:sanic,代碼行數:7,代碼來源:asgi.py

示例10: get_body

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def get_body(request, id):
    '''
    Returns the body (image contents) of a JPEG output
    '''
    output = _get_output(request, id)
    if type(output) != ImageOutput:
        raise InvalidUsage('Output is not an image')

    try:
        return await sanic.response.file_stream(
            output.location,
            headers={'Cache-Control': 'max-age=1'}
        )
    except FileNotFoundError:
        raise InvalidUsage('No such body') 
開發者ID:bbc,項目名稱:brave,代碼行數:17,代碼來源:route_handler.py

示例11: restart

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def restart(request):
    if 'config' not in request.json:
        raise InvalidUsage('Body must contain "config" key')
    if request.json['config'] not in ['original', 'current']:
        raise InvalidUsage('Body "config" key must have value "original" or "current"')
    run_on_master_thread_when_idle(request['session'].end, restart=True,
                                   use_current_config=request.json['config'] == 'current')
    return _status_ok_response() 
開發者ID:bbc,項目名稱:brave,代碼行數:10,代碼來源:route_handler.py

示例12: _get_output

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def _get_output(request, id):
    if id not in request['session'].outputs or request['session'].outputs[id] is None:
        raise InvalidUsage('no such output ID')
    return request['session'].outputs[id] 
開發者ID:bbc,項目名稱:brave,代碼行數:6,代碼來源:route_handler.py

示例13: _get_input

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def _get_input(request, id):
    if id not in request['session'].inputs or request['session'].inputs[id] is None:
        raise InvalidUsage('no such input ID')
    return request['session'].inputs[id] 
開發者ID:bbc,項目名稱:brave,代碼行數:6,代碼來源:route_handler.py

示例14: _get_overlay

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def _get_overlay(request, id):
    if id not in request['session'].overlays or request['session'].overlays[id] is None:
        raise InvalidUsage('no such overlay ID')
    return request['session'].overlays[id] 
開發者ID:bbc,項目名稱:brave,代碼行數:6,代碼來源:route_handler.py

示例15: _get_connection

# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def _get_connection(request, id, create_if_not_made):
    if 'uid' not in request.json:
        raise InvalidUsage('Requires "uid" field in JSON body')

    source = request['session'].uid_to_block(request.json['uid'])
    if source is None:
        raise InvalidUsage('No such item "%s"' % request.json['uid'])

    connection = _get_mixer(request, id).connection_for_source(source, create_if_not_made=create_if_not_made)
    if not connection and create_if_not_made is True:
        raise InvalidUsage('Unable to connect "%s" to mixer %d' % (request.json['uid'], id))
    return connection, request.json 
開發者ID:bbc,項目名稱:brave,代碼行數:14,代碼來源:route_handler.py


注:本文中的sanic.exceptions.InvalidUsage方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。