server/rest: fix reporting parameter type errors
This commit is contained in:
parent
ff7bbbdd8a
commit
e89a086d58
1 changed files with 29 additions and 27 deletions
|
@ -87,38 +87,40 @@ def _create_context(env):
|
||||||
|
|
||||||
def application(env, start_response):
|
def application(env, start_response):
|
||||||
try:
|
try:
|
||||||
ctx = _create_context(env)
|
try:
|
||||||
if 'application/json' not in ctx.get_header('Accept'):
|
ctx = _create_context(env)
|
||||||
raise errors.HttpNotAcceptable(
|
if 'application/json' not in ctx.get_header('Accept'):
|
||||||
'This API only supports JSON responses.')
|
raise errors.HttpNotAcceptable(
|
||||||
|
'This API only supports JSON responses.')
|
||||||
|
|
||||||
for url, allowed_methods in routes.routes.items():
|
for url, allowed_methods in routes.routes.items():
|
||||||
match = re.fullmatch(url, ctx.url)
|
match = re.fullmatch(url, ctx.url)
|
||||||
if not match:
|
if not match:
|
||||||
continue
|
continue
|
||||||
if ctx.method not in allowed_methods:
|
if ctx.method not in allowed_methods:
|
||||||
raise errors.HttpMethodNotAllowed(
|
raise errors.HttpMethodNotAllowed(
|
||||||
'Allowed methods: %r' % allowed_methods)
|
'Allowed methods: %r' % allowed_methods)
|
||||||
|
|
||||||
for hook in middleware.pre_hooks:
|
for hook in middleware.pre_hooks:
|
||||||
hook(ctx)
|
|
||||||
handler = allowed_methods[ctx.method]
|
|
||||||
try:
|
|
||||||
response = handler(ctx, match.groupdict())
|
|
||||||
except Exception as ex:
|
|
||||||
for exception_type, handler in errors.error_handlers.items():
|
|
||||||
if isinstance(ex, exception_type):
|
|
||||||
handler(ex)
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
for hook in middleware.post_hooks:
|
|
||||||
hook(ctx)
|
hook(ctx)
|
||||||
|
handler = allowed_methods[ctx.method]
|
||||||
|
try:
|
||||||
|
response = handler(ctx, match.groupdict())
|
||||||
|
finally:
|
||||||
|
for hook in middleware.post_hooks:
|
||||||
|
hook(ctx)
|
||||||
|
|
||||||
start_response('200', [('content-type', 'application/json')])
|
start_response('200', [('content-type', 'application/json')])
|
||||||
return (_dump_json(response).encode('utf-8'),)
|
return (_dump_json(response).encode('utf-8'),)
|
||||||
|
|
||||||
raise errors.HttpNotFound(
|
raise errors.HttpNotFound(
|
||||||
'Requested path ' + ctx.url + ' was not found.')
|
'Requested path ' + ctx.url + ' was not found.')
|
||||||
|
|
||||||
|
except Exception as ex:
|
||||||
|
for exception_type, handler in errors.error_handlers.items():
|
||||||
|
if isinstance(ex, exception_type):
|
||||||
|
handler(ex)
|
||||||
|
raise
|
||||||
|
|
||||||
except errors.BaseHttpError as ex:
|
except errors.BaseHttpError as ex:
|
||||||
start_response(
|
start_response(
|
||||||
|
|
Loading…
Reference in a new issue