Skip to content

Instantly share code, notes, and snippets.

@kracekumar
Created July 24, 2016 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kracekumar/eef4a2feb052c69bc2a536215756d9c6 to your computer and use it in GitHub Desktop.
Save kracekumar/eef4a2feb052c69bc2a536215756d9c6 to your computer and use it in GitHub Desktop.
View with exception and decorator
@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