Skip to content

Instantly share code, notes, and snippets.

@RaviH
Created December 5, 2014 03:38
Show Gist options
  • Save RaviH/a922034a8dbf74c7019e to your computer and use it in GitHub Desktop.
Save RaviH/a922034a8dbf74c7019e to your computer and use it in GitHub Desktop.
from bottle import Bottle, HTTPResponse
class CustomHttpException(BaseException):
def __init__(self, status_code, message):
self.status_code = status_code
self.message = message
APP1 = Bottle()
def access_status_correctly(callback):
def wrapper(*args, **kwargs):
try:
print "Before making the actual method call"
body = callback(*args, **kwargs)
print "After method call: Yay!"
return body
except CustomHttpException as exc:
print "Correct error status code here: {}".format(exc.status_code)
raise HTTPResponse(status=exc.status_code, body={'message': exc.message})
except Exception:
print "A general exception occurred"
raise HTTPResponse(status=500)
return wrapper
APP1.install(access_status_correctly)
@APP1.route('/error')
def error():
raise CustomHttpException(400, "Throwing an exception here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment