Skip to content

run_dev_server swallows falcon.HTTPError and returns 500 #7

@lmergner

Description

@lmergner

I've been trying out updraft. werkzeug server to swallow falcon's error handling and reraise it as a 500 error.

import falcon
from marshmallow.exceptions import ValidationError
from marshmallow import Schema, fields
from updraft.dev_server import run_dev_server
import logging

logger = logging.getLogger(__name__)


class ErrorSchema(Schema):
    oid = fields.Int(dump_only=True)
    text = fields.Str(required=True)


class Error(object):
    # TODO: Implement

    def on_get(self, req, resp):
        try:
            ErrorSchema().load({"blarg": "Should raise an exception"})
        except ValidationError as e:
            logger.exception(e)
            raise falcon.HTTPBadRequest()


api = falcon.API()
api.add_route("/", Error())
run_dev_server(api, hostname="0.0.0.0", port=5000, use_reloader=True)

Example output

(publicity-zhV7bCdV) lmerg@mergner-darwin in publicity $ http http://localhost:5000/
HTTP/1.0 500 Internal Server Error
Date: Sun, 14 Oct 2018 00:11:48 GMT
Server: BaseHTTP/0.6 Python/3.7.0
content-length: 971
content-type: application/json; charset=UTF-8
vary: Accept

{
    "description": "Traceback (most recent call last):\n  File \"handle.py\", line 23, in on_get\n    ErrorSchema().load({\"blarg\": \"Should raise an exception\"})\n  File \"/Users/lmerg/code/.virtualenvs/publicity-zhV7bCdV/lib/python3.7/site-packages/marshmallow/schema.py\", line 529, in load\n    postprocess=True,\n  File \"/Users/lmerg/code/.virtualenvs/publicity-zhV7bCdV/lib/python3.7/site-packages/marshmallow/schema.py\", line 682, in _do_load\n    raise exc\nmarshmallow.exceptions.ValidationError: {'text': ['Missing data for required field.'], 'blarg': ['Unknown field.']}\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/Users/lmerg/code/contrivers/publicity/falcon/falcon/api.py\", line 247, in __call__\n    responder(req, resp, **params)\n  File \"handle.py\", line 28, in on_get\n    raise falcon.HTTPBadRequest()\nfalcon.errors.HTTPBadRequest\n",
    "title": "500 Internal Server Error"
}

Example expected output (with gunicorn)

(venv) $ http http://localhost:5000/
HTTP/1.1 400 Bad Request
Connection: close
Date: Sun, 14 Oct 2018 00:18:53 GMT
Server: gunicorn/19.9.0
content-length: 28
content-type: application/json; charset=UTF-8
vary: Accept

{
    "title": "400 Bad Request"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions