Skip to content
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

Request.Response can only be called once per request #6

Open
agalera opened this issue Jan 31, 2017 · 5 comments
Open

Request.Response can only be called once per request #6

agalera opened this issue Jan 31, 2017 · 5 comments
Labels

Comments

@agalera
Copy link

agalera commented Jan 31, 2017

If an exception is generated in the response this error appears and the request is blocked

from japronto import Application


app = Application()

def hello(request):
    request.Response(json=['invalid', 'json'])

app.router.add_route(url, hello, method='GET')
app.run()
ValueError: View did not return Response instance
Unhandled exception in event loop
Traceback (most recent call last):
  File "uvloop/handles/stream.pyx", line 785, in uvloop.loop.__uv_stream_on_read_impl (uvloop/loop.c:76261)
  File "uvloop/handles/stream.pyx", line 559, in uvloop.loop.UVStream._on_read (uvloop/loop.c:73525)
  File "/root/japronto-env/lib/python3.6/site-packages/japronto/app/__init__.py", line 99, in error_handler
    return self.default_error_handler(request, exception)
  File "/root/japronto-env/lib/python3.6/site-packages/japronto/app/__init__.py", line 81, in default_error_handler
    text=tb if self._debug else 'Internval Server Error')
@ilia-khaustov
Copy link

def hello(request):
    return request.Response(json=['invalid', 'json'])

@agalera
Copy link
Author

agalera commented Feb 1, 2017

Is voluntary, a valid json is always a dict, here fails to make the conversion to json.

@squeaky-pl
Copy link
Owner

squeaky-pl commented Feb 1, 2017

It's a wart in the API design, I need to refactor this part. What is happening here is that an error is generated in an error handler and since request.Response was already constructed and it's immutable. It fails in a weird way swallowing the real reason.

So this is really about making Response mutable.

@squeaky-pl
Copy link
Owner

squeaky-pl commented Feb 1, 2017

@kianxineki The error doesnt happen because of invalid JSON. Python JSON module will happily transform a list in a JSON array. I don't impose any extra restrictions here. This happens because of missing return.

Anyway this is an error and needs to be fixed.

@ilia-khaustov
Copy link

Finally figured out what it is all about.

from japronto import Application

app = Application()

def hello(request):
    try:
        return request.Response(json=object())
    except TypeError:
        return request.Response(code=500)  # RuntimeError due to immutable Response

app.router.add_route('/', hello, method='GET')
app.run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants