Last active
March 30, 2016 17:08
-
-
Save derwolfe/fa71d055ace0525d873b75b292e2efd3 to your computer and use it in GitHub Desktop.
klein capture error routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
klein |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run
pip intall -r requirements.txt
python runner.py