Factoring fatal error handling out of flush function #18
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.
Our team are using this library to catch errors and report them to Rollbar, but only on certain environments. On others, we use different reporting methods. As such, we use our own error handler which sits in front of Rollbar, and passes on any errors it catches to Rollbar where appropriate. This is easy to do as we can pass
false
as the second and third parameters of theRollbar::init()
function to prevent it from registering it's own error and exception handlers.The only problem with this approach is that this doesn't apply to fatal errors - there is no way currently to stop the
Rollbar
object from intercepting and reporting fatal errors independently. Another issue is that fatal errors are only reported if batching is switched on.Fortunately, register_shutdown_function is stackable, meaning we can register multiple functions to run in order at shutdown. As such, I've split the fatal error catching code out from the
Rollbar::flush()
function, and added an extra parameter to the end of theRollbar::init()
function, mimicking the behaviour of the other two handlers.Potential Gotcha: Registered shutdown functions are called in the order they were registered
Provided our own shutdown function is registered before the Rollbar library is initialized,
E_ERROR
will be reported:Note that if Rollbar was initialized before
Custom_Error_Handler::catch_fatal_error
was registered as a shutdown function, the finalRollbar::flush()
will occur before ourcatch_fatal_error
has passed the error on to theRollbar
object. In this situation, in batch mode, the error may not be reported to the Rollbar API.Backwards compatibility
Like the other two parameters, the new parameter has a default value of true, so should be backwards compatible, and also solves the issue of fatal errors not being reported when batched mode is not being used.