Support » Plugin: Query Monitor » Clickable error messages for fatal errors

  • I use the clickable error messages of QM very ofter – thanks a lot for them!!!

    But unfortunately, for fatal errors, I don’t get them. But if I place the following code to the begin of the WordPress root index.php, I get them for fatal errors too. Maybe you have an idea how to use this code in QM to offer it to other developers too.

    And if not, maybe you can add a note for this solution inside your QM blog?

    
    
    // Make fatal errors clickable
    register_shutdown_function( 'shutdown_function' );
    function shutdown_function() {
      $last_error = error_get_last();
      if ( $last_error ) {
        switch ( $last_error['type'] ) {
          case E_WARNING:
            $message = '<strong>Warning</strong>: ' . $last_error['message'];
            break;
          case E_PARSE:
            $message = '<strong>Parser error</strong> (' . $last_error['type'] . '): ' . $last_error['message'];
            break;
          case E_NOTICE::
            $message = '<strong>Notice</strong> (' . $last_error['type'] . '): ' . $last_error['message'];
          default:
            $message = '<strong>Error ' . $last_error['type'] . '</strong>: ' . $last_error['message'];
            break;
        }
    
        if ( $last_error['file'] )
          $message .= ' in <a>' . $last_error['file'] . '</strong> on line <strong>' . $last_error['line'] . '</strong>';
        echo '<br/>' . $message;
        die ();
      }
    }
    

    … it’s works with VS Code. For the other editors, the links has to be modified.

    • This topic was modified 4 months, 1 week ago by nilshoyer.
    • This topic was modified 4 months, 1 week ago by Jan Dembowski. Reason: Formatting
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    Thanks. I’ll have a look and see what I can do. Unfortunately with fatal error handling you need to be careful what functions you call, in case the fatal was triggered very early. But yeah this sounds useful and I’ll have a look into it.

    Yes, getting the link format will be a problem with apply_filters. Maybe hardcoded in a file in the QM plugin folder.

    … better than nothing. 😉

    … since, I wish to get the clickable links, I tried to implement it in QM, php-errors.php, function shutdown_handler as follow.

    After this, I renamed my plugin file and replaced it as the below described code.

    For me, it works in the moment. But I still not happy to place the add_filter in a separated file. So maybe you have a better idea?

    if ( $e['type'] & E_RECOVERABLE_ERROR ) {
      $error = 'Catchable fatal error';
    } else {
      switch ( $e['type'] ) {
        case E_WARNING:
          $error = 'Warning';
          break;
        case E_PARSE:
          $error = 'Parser error';
          break;
        case E_NOTICE::
          $error = 'Notice';
        default:
          $error = 'Fatal error';
          break;
      }
    }
    
    if ( $clickable ) {
      register_shutdown_function(function() {
        QM_Collector_PHP_Errors::shutdown_handler( false );
      });
    
      include( realpath( dirname( __FILE__) . '/..' ) . '/output/Html.php' );
    
      printf( // WPCS: XSS ok.
        '<br><b>%1$s</b>: %2$s<br>',
        htmlentities( $error ),
        QM_Output_Html::output_filename( $e['message'], $e['file'], $e['line'] )
      );
      die();
    } else {
      printf( // WPCS: XSS ok.
        '<br><b>%1$s</b>: %2$s in <b>%3$s</b> on line <b>%4$d</b><br>',
        htmlentities( $error ),
        nl2br( htmlentities( $e['message'] ), false ),
        htmlentities( $e['file'] ),
        intval( $e['line'] )
      );
    }
    

    This is my replaced plugin file:

    <?php
    add_filter( 'qm/output/file_link_format', function( $format ) {
      return 'vscode://file/%f:%l';
    } );
    require( ABSPATH . '/wp-content/plugins/my-plugin/renamed_file.php');
    ?>
    

    The replacement of my plugin file is needed, since it will be executed only, if there is no parser error inside of it. But if inside the renamed_file.php is a parser error, I get clickable error messages.

    Indeed you are the professional, if this solution would be helpful for other developer too.

    Instead of setting the clickable format with a filter, I would prefer a solution to store it inside a setting page of QM. Than I would not need to replace my file…

    … I forgot to mention the changed function header:

    public function shutdown_handler( $clickable = true ) {
    
    Plugin Author John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    With the Throwable interface in PHP7+ it’s possible to catch a fatal error and then access its stack trace programatically instead of having to parse it out of the error message text from a fatal via set_error_handler().

    This allows QM to show a complete, clickable stack trace for fatal errors. I hope to get this into the next version of QM.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.