Created
December 5, 2014 03:38
-
-
Save RaviH/a922034a8dbf74c7019e to your computer and use it in GitHub Desktop.
This file contains 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 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