Created
July 24, 2016 14:27
-
-
Save kracekumar/eef4a2feb052c69bc2a536215756d9c6 to your computer and use it in GitHub Desktop.
View with exception and decorator
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
@wrapt.decorator | |
def handle_http_and_validation_exceptions(wrapped, instance, args, kwargs): | |
try: | |
return wrapped(*args, **kwargs) | |
except HTTPException as e: | |
return make_response(status=e.status, data=e.data) | |
except ValidationError as e: | |
return make_response(status=400, data=e.errors) | |
class TodoBucketListAPIView(BaseMethodView): | |
@handle_http_and_validation_exceptions | |
def post(self): | |
user = get_user() | |
validator = NewBucketValidator(request.POST) | |
validator.validate(raise_exception=True) | |
bucket = check_and_create_new_bucket(validator.name, user=user) | |
resp_data = format_new_bucket(bucket) | |
return make_response(data=resp_data, status=201) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment