Remove boilerplate code from api error handling

Registered by Radomir Dopieralski

If you look at the code for views in horizon dashboard, you will see a lot of this pattern repeating everywhere:

        flavors = []
        try:
            flavors = api.nova.flavor_list(request, None)
        except Exception:
            exceptions.handle(request, _('Unable to retrieve flavor list.'))

This code would be repeated everywhere, where api.nova.flavor_list is called, with the same exact error message and the same default value.

It would be nice to refactor that, for example using a decorator, like we did in Tuskar-UI: https://github.com/openstack/tuskar-ui/blob/master/tuskar_ui/handle_errors.py

Then the default error message and value can be specified once, on the api method itself, and later overriden if needed.

The above code then turns into:

    @handle_errors(_('Unable to retrieve flavor list.'), [])
    def flavor_list(...):...
    ...
    api.nova.flavor_list(request, None)

We can also use the decorator in-place explicitly, without having decorated the method:

    handle_errors(_('Unable to retrieve flavor list.'), [])(api.nova.flavor_list)(request, None)

Blueprint information

Status:
Complete
Approver:
None
Priority:
Undefined
Drafter:
Radomir Dopieralski
Direction:
Needs approval
Assignee:
Radomir Dopieralski
Definition:
Obsolete
Series goal:
None
Implementation:
Unknown
Milestone target:
None
Completed by
Rob Cresswell

Related branches

Sprints

Whiteboard

Gerrit topic: https://review.openstack.org/#q,topic:bp/horizon-boilerplate-error-handling,n,z

Addressed by: https://review.openstack.org/98049
    A decorated for handling errors

[peristeri 2014.11.11] The blueprint https://blueprints.launchpad.net/horizon/+spec/improve-error-message-details-for-usability overlaps with the goal of this blueprint. I did not get a chance to work on it much but the overall solution resembles much like yours, it breaks down the exception into more granular exceptions. That way it is easier to format the messages sent from different services. If you want I could share what I have so far.

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.