From 3ef2cc7e71fc89b99baac73500757839aa21923f Mon Sep 17 00:00:00 2001 From: Cédric Picard Date: Fri, 28 Nov 2014 19:32:30 +0100 Subject: Better error gestion with correct status code --- pasteme.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pasteme.py b/pasteme.py index 744b07c..f155502 100755 --- a/pasteme.py +++ b/pasteme.py @@ -25,7 +25,7 @@ def route_paste_post(): pid = identigen.generate(content) except AttributeError as e: print(e) - return bottle.template('internal_error') + bottle.abort(500) path = pathbase / pid with path.open(mode='wb') as fd: fd.write(content.encode('utf8')) @@ -46,7 +46,7 @@ def route_paste_get(pid, pformat='colored'): content = fd.read() except IOError: # use this template for all file based exception - return bottle.template('not_found') + bottle.abort(404) if pformat == 'colored': try: lexer = pygments.lexers.guess_lexer(content) @@ -56,6 +56,14 @@ def route_paste_get(pid, pformat='colored'): return bottle.template('paste', content=content) return content +@bottle.error(404) +def error404(error): + return bottle.template("not_found") + +@bottle.error(500) +def error500(error): + return bottle.template("internal_error") + if __name__ == '__main__': print('I: Starting application with development server') bottle.run(host='0.0.0.0', port=8080, debug=True, reloader=True) -- cgit v1.2.3