本文整理汇总了Python中sanic.exceptions.ServerError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ServerError方法的具体用法?Python exceptions.ServerError怎么用?Python exceptions.ServerError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sanic.exceptions
的用法示例。
在下文中一共展示了exceptions.ServerError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: callback
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def callback(request):
if request.args.get("error"):
error = {
"error": request.args.get("error"),
"error_description": request.args.get("error_description"),
}
return response.json(error)
elif request.args.get("code"):
returned_state = request.args["state"][0]
# Check state
if returned_state != state:
raise ServerError("NO")
# Step D & E (D send grant code, E receive token info)
full_user_creds = await aiogoogle.oauth2.build_user_creds(
grant=request.args.get("code"), client_creds=CLIENT_CREDS
)
return response.json(full_user_creds)
else:
# Should either receive a code or an error
return response.text("Something's probably wrong with your callback")
示例2: exception
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def exception(request):
raise ServerError("It's dead jim")
示例3: handler_2
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def handler_2(request):
raise ServerError("OK")
示例4: handler_5
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def handler_5(request):
class CustomServerError(ServerError):
pass
raise CustomServerError("Custom server error")
示例5: test_exception_handler_lookup
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def test_exception_handler_lookup():
class CustomError(Exception):
pass
class CustomServerError(ServerError):
pass
def custom_error_handler():
pass
def server_error_handler():
pass
def import_error_handler():
pass
try:
ModuleNotFoundError
except Exception:
class ModuleNotFoundError(ImportError):
pass
handler = ErrorHandler()
handler.add(ImportError, import_error_handler)
handler.add(CustomError, custom_error_handler)
handler.add(ServerError, server_error_handler)
assert handler.lookup(ImportError()) == import_error_handler
assert handler.lookup(ModuleNotFoundError()) == import_error_handler
assert handler.lookup(CustomError()) == custom_error_handler
assert handler.lookup(ServerError("Error")) == server_error_handler
assert handler.lookup(CustomServerError("Error")) == server_error_handler
# once again to ensure there is no caching bug
assert handler.lookup(ImportError()) == import_error_handler
assert handler.lookup(ModuleNotFoundError()) == import_error_handler
assert handler.lookup(CustomError()) == custom_error_handler
assert handler.lookup(ServerError("Error")) == server_error_handler
assert handler.lookup(CustomServerError("Error")) == server_error_handler
示例6: exception
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def exception(request):
raise ServerError("yep")
示例7: test_invalid_response
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def test_invalid_response(app):
@app.exception(ServerError)
def handler_exception(request, exception):
return text("Internal Server Error.", 500)
@app.route("/")
async def handler(request):
return "This should fail"
request, response = app.test_client.get("/")
assert response.status == 500
assert response.text == "Internal Server Error."
示例8: test_invalid_response_asgi
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def test_invalid_response_asgi(app):
@app.exception(ServerError)
def handler_exception(request, exception):
return text("Internal Server Error.", 500)
@app.route("/")
async def handler(request):
return "This should fail"
request, response = await app.asgi_client.get("/")
assert response.status == 500
assert response.text == "Internal Server Error."
示例9: test_bp_exception_handler
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [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
示例10: _get_args
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def _get_args(self, request):
params = request.raw_args
if not params:
raise ServerError("invalid params", status_code=400)
args = {
'mp_token': Config.WEIXINMP_TOKEN,
'signature': params.get('signature'),
'timestamp': params.get('timestamp'),
'echostr': params.get('echostr'),
'nonce': params.get('nonce'),
}
return args
示例11: handle_exception
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def handle_exception(request) -> User:
"""
API Description: Handle exception. This will show in the swagger page (localhost:8000/api/v1/).
"""
raise ServerError("Something bad happened", status_code=500)
示例12: request_validate
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def request_validate(view):
@wraps(view)
def wrapper(*args, **kwargs):
request = args[1]
endpoint = _path_to_endpoint(request.uri_template)
current.request = request
# scope
if (endpoint, request.method) in scopes and not set(
scopes[(endpoint, request.method)]).issubset(set(security.scopes)):
raise ServerError('403', status_code=403)
# data
method = request.method
if method == 'HEAD':
method = 'GET'
locations = validators.get((endpoint, method), {})
for location, schema in locations.items():
value = getattr(request, location, MultiDict())
if value is None:
value = MultiDict()
validator = SanicValidatorAdaptor(schema)
result, errors = validator.validate(value)
if errors:
raise ServerError('Unprocessable Entity', status_code=422)
return view(*args, **kwargs)
return wrapper
示例13: add_routes
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def add_routes(app):
#@app.route('/test_no_acl_abort_404')
#@app.route('/test_acl_abort_404')
def test_acl_abort_404(request):
raise NotFound("")
app.route('/test_no_acl_abort_404')(test_acl_abort_404)
app.route('/test_acl_abort_404')(test_acl_abort_404)
#@app.route('/test_no_acl_async_abort_404')
#@app.route('/test_acl_async_abort_404')
async def test_acl_async_abort_404(request):
raise NotFound("")
app.route('/test_no_acl_async_abort_404')(test_acl_async_abort_404)
app.route('/test_acl_async_abort_404')(test_acl_async_abort_404)
#@app.route('/test_no_acl_abort_500')
#@app.route('/test_acl_abort_500')
def test_acl_abort_500(request):
raise ServerError("")
app.route('/test_no_acl_abort_500')(test_acl_abort_500)
app.route('/test_acl_abort_500')(test_acl_abort_500)
@app.route('/test_acl_uncaught_exception_500')
def test_acl_uncaught_exception_500(request):
raise Exception("This could've been any exception")
@app.route('/test_no_acl_uncaught_exception_500')
def test_no_acl_uncaught_exception_500(request):
raise Exception("This could've been any exception")
示例14: test_acl_exception_with_error_handler
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def test_acl_exception_with_error_handler(self):
'''
If a 500 handler is setup by the user, responses should have
CORS matching rules applied, regardless of whether or not
intercept_exceptions is enabled.
'''
return_string = "Simple error handler"
@self.app.exception(NotFound, ServerError, Exception)
def catch_all_handler(request, exception):
'''
This error handler catches 404s and 500s and returns
status 200 no matter what. It is not a good handler.
'''
return text(return_string)
acl_paths = [
'/test_acl_abort_404',
'/test_acl_abort_500',
'/test_acl_uncaught_exception_500'
]
no_acl_paths = [
'/test_no_acl_abort_404',
'/test_no_acl_abort_500',
'/test_no_acl_uncaught_exception_500'
]
def get_with_origins(path):
response = self.get(path, origin='www.example.com')
return response
for resp in map(get_with_origins, acl_paths):
self.assertEqual(resp.status, 200)
self.assertTrue(ACL_ORIGIN in resp.headers)
for resp in map(get_with_origins, no_acl_paths):
self.assertEqual(resp.status, 200)
self.assertFalse(ACL_ORIGIN in resp.headers)
示例15: test
# 需要导入模块: from sanic import exceptions [as 别名]
# 或者: from sanic.exceptions import ServerError [as 别名]
def test(request):
raise ServerError("asunk")