Skip to content

Instantly share code, notes, and snippets.

@derwolfe
Last active March 30, 2016 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derwolfe/fa71d055ace0525d873b75b292e2efd3 to your computer and use it in GitHub Desktop.
Save derwolfe/fa71d055ace0525d873b75b292e2efd3 to your computer and use it in GitHub Desktop.
klein capture error routes
from klein import Klein
class NotFound(Exception):
pass
class ItemStore(object):
app = Klein()
@app.handle_errors(NotFound)
def notfound(self, request, failure):
request.setResponseCode(404)
return 'Not found, I say'
@app.route('/droid/<string:name>')
def droid(self, request, name):
if name in ['R2D2', 'C3P0']:
raise NotFound()
return 'Droid found'
@app.route('/bounty/<string:target>')
def bounty(self, request, target):
if target == 'Han Solo':
return '150,000'
raise NotFound()
# all other routes should dispatch the 404
# if not already handled
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def all_unmatched_routes(self, request, path):
raise NotFound()
if __name__ == '__main__':
store = ItemStore()
store.app.run('localhost', 8080)
@derwolfe
Copy link
Author

To run

  1. Make a virtualenv
  2. activate the virtualenv
  3. pip intall -r requirements.txt
  4. python runner.py
  5. look at urls that should generate 404s, look at urls that should return results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment