stayallive / wp-sentry

A (unofficial) WordPress plugin reporting PHP and JavaScript errors to Sentry.
https://wordpress.org/plugins/wp-sentry-integration/
MIT License
305 stars 48 forks source link

`wp_sentry_options` first argument is always `null` #94

Closed codepuncher closed 3 years ago

codepuncher commented 3 years ago

I am having issues with using the wp_sentry_options filter. It seems that the 1st argument passed is always null rather than \Sentry\Options and as a result, the code I am writing is not being fired.

I have made an MU Plugin to load the plugin sooner, as instructed by the readme. This is also where I've added additional filters.

I'm just trying to prevent some errors from plugins from being sent to Sentry, following this.

However, it seems that instead of an Options object being passed, it's null.

use Sentry\Options;

add_filter('wp_sentry_options', function (?Options $options): void {
    if (null === $options) {
        return;
    }

    # My code is beneath
});

Is there perhaps a bug or misuse of the filters?

Thanks

WP 5.8 wp-sentry-integration 4.5.0 PHP 7.4

stayallive commented 3 years ago

The code that is called in the plugin is:

apply_filters( 'wp_sentry_options', $this->get_client()->getClient()->getOptions() );

And getOptions has Options as a return type, so it cannot be null (getClient could technically return null but that would result in a method call on null error stopping the application).

Do you have multiple wp_sentry_options filters perhaps and forget to end the filters with return $options? Because no return or just return; will result in the value passed down the filters to be null instead of the expected Options object like you are seeing.

Edit: I have updated the README example to include the return $options; to possibly prevent this in the future

codepuncher commented 3 years ago

Ah, yep. That might have been the issue; not returning the Options object. Thanks for updating the readme! All good now.