Add 'on_error' callbacks, to eventually replace 'before_notify_callbacks' #608
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Goal
The main problem these solve is that they are global, rather than being scoped to the current thread. This means you can add a callback in a Rails initializer and it will run as expected. With the current
before_notify_callbacks
, they only run in the same thread as they are added and so won't run if created in an initializer (this is a problem in most frameworks, not just Rails)The
before_notify_callbacks
cause a lot of confusion because they are scoped to a single thread, e.g. see #487, #417. Typically callbacks are intended to be global to the application rather than specific to a route/worker, so the newon_error
mechanism should be a lot more intuitive. This is also how callbacks work in our libraries for other platforms, like JavaScript and AndroidThe new callbacks can also be removed if necessary:
You can also ignore an error and stop calling any subsequent callbacks by returning
false
:Tests
Most of the implementation is based on middleware, which is already covered by existing tests. I've added a bunch of tests for the new callbacks specifically and checking that they work as expected across threads. Some Maze Runner tests will be added in a separate PR
Review
For the submitter, initial self-review:
For the pull request reviewer(s), this changeset has been reviewed for: