twisted / twisted/klein

errors aren't being handled on branched resources

Open
#119 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
838
Forks
123
Avg merge
7h 58m
Merged PRs (30d)
12

Description

Branched routes don't handle errors of the "base" Resource. Hopefully an example to start things off will be beneficial.

from klein import Klein
from werkzeug.exceptions import NotFound

app1 = Klein()
app2 = Klein()

#----- Catch Errors -----#
@app1.route('/<path:allpaths>')
def catchall(request, allpaths):
    request.setResponseCode(404)
    return 'Catch-All: paths'

@app1.handle_errors(ValueError)
def handle(request, failure):
    request.setResponseCode(400)
    return 'Catch: ValueError()'

@app1.handle_errors(NotFound)
def huh(request, failure):
    request.setResponseCode(413)
    return 'Catch-All: NotFound()'

#----- Routes -----#
@app1.route('/one')
def one(request):
    raise ValueError('raised a ValueError')

@app1.route('/two', branch=True)
def two(request):
    return app2.resource()

@app2.route('/blue')
def blue(request):
    raise ValueError('blue')        # this won't be handled by app1

if __name__ == '__main__':
    app1.run('localhost', 9000)

If a route isn't present, then either the catchall() or huh() function will execute. Similarly, if a ValueError is raised, then the handle() function is executed.

> curl localhost:9000/
# Catch-All: NotFound()

> curl localhost:9000/foo/baz/bar
# Catch-All: paths

> curl localhost:9000/one
# Catch: ValueError()

However, if an invalid route from a branched resource is used, then the errors are left unhandled. The following will result in a traceback and the default Twisted 404 page:

> curl localhost:9000/two/blue
> curl localhost:9000/two/red

This all makes it very tedious and very error prone when creating expansive web or RESTful applications because devs must then ensure each each Resource has all the appropriate error handling. Though there are ways around this, there should be a dedicated "Klein" way to map branched routes to the base set of routes.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with the branched-resource example using the listed curl requests, then trace Klein's route branching, resource(), and handle_errors entry points. Done should mean errors and missing routes under app2 are handled by the base app1 handlers instead of producing an unhandled traceback or default Twisted 404 page.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.