本文整理匯總了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))
示例2: handler_1
# 需要導入模塊: from sanic import exceptions [as 別名]
# 或者: from sanic.exceptions import InvalidUsage [as 別名]
def handler_1(request):
raise InvalidUsage("OK")
示例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."
示例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."
示例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)
示例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
示例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
示例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
示例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.")
示例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')
示例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()
示例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]
示例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]
示例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]
示例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