stayallive / wp-sentry

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

`Argument #1 ($options) must be of type Sentry\Options, null given` #172

Closed LordSimal closed 7 months ago

LordSimal commented 7 months ago

I got this error after updating the Sentry Plugin from 7.1.0 -> 7.2.1 and got that error:

Argument #1 ($options) must be of type Sentry\Options, null given, called in /var/www/html/wp-includes/class-wp-hook.php on line 310

image

I solved it via commenting out the

if (file_exists(__DIR__ . '/wp-content/plugins/wp-sentry-integration/wp-sentry.php')) {
  require_once __DIR__ . '/wp-content/plugins/wp-sentry-integration/wp-sentry.php';
}

inside the wp-config.php to catch errors inside plugins or while bootstrapping.

stayallive commented 7 months ago

That is very interesting.

Can you share a bit more info. PHP version, WordPress version, if you have a networks site, what other WP_SENTRY_* things do you have defined in your wp-config.php, any custom hooks or custom config for the Sentry plugin etc.?

I have put this on top of my wp-config.php:

define( 'WP_SENTRY_DSN', '...' );
require_once __DIR__ . '/wp-content/plugins/wp-sentry/wp-sentry.php';

But cannot see the error you are seeing.

LordSimal commented 7 months ago

PHP 8.1 Wordpress 6.3.2

wp-config.php

define( 'WP_SENTRY_PHP_DSN', '--my-dsn--' );
define( 'WP_SENTRY_ENV', 'production' );

also we have 1 additional filter inside the themes functions.php

if( class_exists('\Sentry\Options') ) {
  add_filter( 'wp_sentry_options', function ( \Sentry\Options $options ) {
    $options->setBeforeSendCallback( function ( \Sentry\Event $event ) {
      $exceptions = $event->getExceptions();

      if( isset( $exceptions[0] ) &&
          method_exists( $exceptions[0], 'getValue' ) &&
          $exceptions[0]->getValue() == 'Warning: preg_match(): Compilation failed: quantifier does not follow a repeatable item at offset 1' ) {
        return null;
      }

      return $event;
    });
  });
}

and as you can see i forgot to return the $options 🙈 so this fixed the issue for me:

if( class_exists('\Sentry\Options') ) {
  add_filter( 'wp_sentry_options', function ( \Sentry\Options $options ) {
    $options->setBeforeSendCallback( function ( \Sentry\Event $event ) {
      $exceptions = $event->getExceptions();

      if( isset( $exceptions[0] ) &&
          method_exists( $exceptions[0], 'getValue' ) &&
          $exceptions[0]->getValue() == 'Warning: preg_match(): Compilation failed: quantifier does not follow a repeatable item at offset 1' ) {
        return null;
      }

      return $event;
    });

    return $options;
  });
}
stayallive commented 7 months ago

That makes more sense 😅 Couldn't figure out how that value would ever be null but that explains it, thanks for the quick update!