New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError in function routes get swallowed in _execute_route #194
Comments
This is by design, currently. |
Since I seem to make a lot of mistakes resulting in # errortmp.py
import traceback
class RouteTypeError(Exception):
pass
class TypeErrorWrapper:
def __init__(self, fn):
self._real_fn = fn
def _error_is_from_view(self, stack_entry):
if stack_entry.name == "__call__" and stack_entry.filename == __file__:
return False
return True
def __call__(self, *args, **kwargs):
try:
return self._real_fn(*args, **kwargs)
except TypeError as e:
summary = traceback.extract_tb(e.__traceback__)
if self._error_is_from_view(summary[-1]):
raise RouteTypeError(e).with_traceback(e.__traceback__)
else:
raise e
def __getattribute__(self, attr):
if attr in ['_real_fn', '_error_is_from_view']:
return object.__getattribute__(self, attr)
return getattr(object.__getattribute__(self, '_real_fn'), attr)
def route_patcher(api):
original = api.route
def route(*args, **kwargs):
decorator = original(*args, **kwargs)
def inner(f):
return decorator(TypeErrorWrapper(f))
return inner
api.route = route The way I use this: # api.py
import responder
from myapp.lib.errortmp import route_patcher
api = responder.API(title="my app api", vversion="1.0", secret_key=os.environ['SECRET_KEY'])
route_patcher(api) In my routes I import the api object from |
If I implement a function view for a route which raises a TypeError for some reason, it seems to be caught by line 328 of
responder/api.py
(inside _execute_route), and the server returns 200 OK, with a jsonnull
body.Meanwhile the server log looks like this:
INFO: ('127.0.0.1', 65440) - "GET /raise_me_up HTTP/1.1" 200
If however another type of exception is raised, the result is a 500 error:
And the server log contains the stack trace:
I'd like to fix this myself (and also dig into why I'm not seeing the stacktrace in my curl output, since the ExceptionMiddleware is hooked up since #157) but I'm not sure why a TypeError would be raised in this block:
https://github.com/kennethreitz/responder/blob/master/responder/api.py#L321-L328
My first thought was that this happens if the view isn't a function, in which case I wonder why the
route.is_function
in line 319 evaluates to true. I'd like to understand more before I attempt to fix this (probably by not relying on exceptions for control flow).The text was updated successfully, but these errors were encountered: