stayallive / wp-sentry

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

Add ability to define functions from PHP to JS #72

Closed kierenayima closed 4 years ago

kierenayima commented 4 years ago

Some JS sentry options are callback functions which currently cannot be configured with this plugin.

This code will check the options in the wp_sentry object and similar to the 'regex' implementation, it will check for strings starting with a 'function:' or 'function[param1, param2]:'. It will then use the JS native Function to convert the string to a function call.

In the case of the second approach(function[param1, param2])- it will define the arguments you expect the function to be called with, otherwise with the first approach you can simply used arguments[0]... etc.

The PHP defining the options looks like this (using the second approach):

"beforeSend" => "function[event,hint]: const error = hint.originalException; if(error && error.message) { if(error.message.match(/grecaptcha is not defined/i)) { return null; } if(error.message.match(/Non-Error promise rejection captured with value: .*/i)) { return null; } } return event; ",

Whereas with the first approach, not defining arguments, looks like this:

"beforeSend" => "function: const event = arguments[0]; const hint = arguments[1]; const error = hint.originalException; if(error && error.message) { if(error.message.match(/grecaptcha is not defined/i)) { return null; } if(error.message.match(/Non-Error promise rejection captured with value: .*/i)) { return null; } } return event; ",

I was considering defining a full JS and parsing out the params etc- but this seemed easiest option however if you reckon that approach might be better let me know.

stayallive commented 4 years ago

This was already discussed at length in #42. I have opted then to not include code like this since I feel it's very hacky.

However we do have a solution now that allows you to define a client side hook to modify the options however you want including adding functions if needed: https://github.com/stayallive/wp-sentry#advanced-client-side-hook.

I feel this already solves the problem for you 👍

Something like this should work for you:

add_action( 'wp_enqueue_scripts', function () {
    wp_add_inline_script( 'wp-sentry-browser', 'function wp_sentry_hook(options) { options.beforeSend = function (event) { /* YOUR CODE HERE! */ }; }', 'before' );
} );
kierenayima commented 4 years ago

@stayallive thank you very much- apologies as I just noticed that ticket (should have checked).

I will try the above method and see how I get on with it, thank you very much!

kierenayima commented 4 years ago

Actually @stayallive I have noticed an issue:

I have tried the following code:

add_action('wp_enqueue_scripts', function() {
    wp_add_inline_script( 'wp-sentry-browser', 'function wp_sentry_hook(options) { console.log("this was called", options); options.beforeSend = function (event, hint) { console.log("An error was triggered.", event, hint) }; }', 'before' );
});

And it doesn't seem to work. Console logging wp_sentry_hook in the wp-sentry-browser.min.js file shows that it is undefined- and looking at this page source I can't seem to find the code added.

I have managed to get this to work using this way though:

add_action('wp_print_scripts', function() {
echo <<<EXXEPT
    \n<script>
    function wp_sentry_hook(options) {
        console.log("this was called", options);
        options.beforeSend = function (event, hint) {
            console.log("An error was triggered.", event, hint);
        };
    }
</script>\n
EXXEPT;
});

Not sure why using the wp_add_inline_script function doesn't work.

stayallive commented 4 years ago

I am not sure either... it should work from testing, wp_add_inline_script was only introduces in WordPress 4.5 so maybe you are on an older version: https://developer.wordpress.org/reference/functions/wp_add_inline_script/.

But I appreciate you letting me know a workaround, that also seems like an acceptable solution.