Skip to content

Commit

Permalink
Merge pull request #3403 from girder/rest-failed-event
Browse files Browse the repository at this point in the history
Wrap route handler and dispatch `failed` event
  • Loading branch information
zachmullen committed Apr 5, 2022
2 parents ec43f02 + 6691b19 commit f209377
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
8 changes: 8 additions & 0 deletions docs/plugin-development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ You may change the access level of the route in your handler, but you will
need to do so explicitly by declaring a different decorator than the underlying
route handler.

* **Exception during REST call**

This event is fired if an exception is raised while the default handler is executing.
Like the before REST call event, this event recieves the same kwargs as the default
route handler. The caught exception is raised again after this event is handled.
The identifier for this event is, e.g., ``rest.get.item/:id.failed``.


* **After REST call**

Just like the before REST call event, but this is fired after the default
Expand Down
26 changes: 19 additions & 7 deletions girder/api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,12 +919,17 @@ def handleRoute(self, method, path, params):
``rest.get.item/:id.after``
would be fired after the route handler returns. The query params are
passed in the info of the before and after event handlers as
event.info['params'], and the matched route tokens are passed in
as dict items of event.info, so in the previous example event.info would
also contain an 'id' key with the value of 123. For endpoints with empty
sub-routes, the trailing slash is omitted from the event name, e.g.:
would be fired after the route handler returns. If the request fails,
instead of firing the after event,
``rest.get.item/:id.failed``
would be fired. The query params are passed in the info of the before,
after, and failed event handlers as event.info['params'], and the matched
route tokens are passed in as dict items of event.info, so in the previous
example event.info would also contain an 'id' key with the value of 123.
For endpoints with empty sub-routes, the trailing slash is omitted from
the event name, e.g.:
``rest.post.group.before``
Expand Down Expand Up @@ -965,7 +970,14 @@ def handleRoute(self, method, path, params):
val = event.responses[0]
else:
self._defaultAccess(handler)
val = handler(**kwargs)

# Attempt to handle and trigger failed event if an
# exception is raised.
try:
val = handler(**kwargs)
except Exception:
events.trigger('.'.join((eventPrefix, 'failed')), kwargs)
raise

# Fire the after-call event that has a chance to augment the
# return value of the API method that was called. You can
Expand Down

0 comments on commit f209377

Please sign in to comment.