WP_Fatal_Error_Handler OR WP_Error OR try/catch

A process into my plugin provoke a fatal error in Linux Server which I have not access, for example to define a WP_DEBUG_LOG.

I could identified the conflict in the event related to generate an jpeg image from a graph (jpgraph - CanvasGraph()).

What I'm looking for :

First, extract any-to-specific info from the server or wordpress, that I can see in the response (like the string wordpress tipically send "../article/debbugin-in-wordpress/">learn more about wordpress debbuging") to figure out whats the especific problem and how solve it.

And second, catch this error and prevent to evolve into fatal error.

What I tried :

I try to catch this fatal error in more than one way. But always fires first the wordpress "fatal error handler" crashing the view.

I try first with a custom try/catch block (It doesnt work), then with WP_Error like is_wp_error() and if/else blocks to throw new Excepion (It doesnt work), after try to handle this with the WP_Fatal_Error_Handler but I couldn't find any example so I'm reading the entire class (which works with try/catch blocks) to figure out something (examples of real use case are welcome).

Resume :

Problem

  1. I got a fatal error in my plugin (only with Linux Server)
  2. I try to catch it first with try/catch, then is_wp_error, both without success
  3. Wordpress triggers it fatal error handler always first
  4. I haven't any info

Goals:

  1. I looking to prevent the fatal error crash
  2. Get any-to-specific info from server (Linux, which I have not direct access e.g to define WP_DEBUG_LOG in wp-config.php) or wordpress about the error (my enviroment of action is just my plugin files, I can upload it download it but nothing else)

This is my code : This chunk of code provoke a fatal error into Linux Server. I can't catch errors from the class neither the instance or Stroke method.

    try{

      $graph = new CanvasGraph( 330 , 100 );

      if ( is_wp_error( $graph ) ){ echo "graph is wp_error"; }

      if ( is_null( $graph ) ){ throw new Exception( "page4 technical_data : graph is_null  |  " ); var_dump( $graph ); }
      if ( !isset( $graph ) ){ throw new Exception( "page4 technical_data : graph is_not_set  |  " ); var_dump( $graph ); }
      if ( empty( $graph ) ){ throw new Exception( "page4 technical_data : graph is_empty  |  " ); var_dump( $graph ); }

      $data = array(
        array( utf8_decode( "technical data" ), utf8_decode( "Technical Data" ) ),
        array( utf8_decode( "ecommerce" ), $ecommerce  ),
        array( utf8_decode( "values" ), $values  ),
        array( utf8_decode( "years" ), utf8_decode( "0.0 years" )  )
      );

      if ( $graph ){

        try{
          $table = new GTextTable();
          if ( is_null( $table ) ){ throw new Exception( "page4 technical_data : table is_null  |  " ); var_dump( $table ); }
          if ( !isset( $table ) ){ throw new Exception( "page4 technical_data : table is_not_set  |  " ); var_dump( $table ); }
          if ( empty( $table ) ){ throw new Exception( "page4 technical_data : table is_empty  |  " ); var_dump( $table ); }

          if ( $table ){

            $table-Set( $data );

            // Merge all cells in row 0
            $table-MergeRow( 0 );

            // Setup font and color
            $table-SetRowFont( 0, FF_ARIAL, FS_NORMAL, 4 );
            $table-SetFont( FF_ARIAL, FS_NORMAL, 10 );
            $table-SetRowColor( 0, 'gray2' );
            $table-SetCellFillColor( 1 , 0 , 'lightblue@' );
            $table-SetCellFillColor( 2 , 0 , 'lightblue@' );
            $table-SetCellFillColor( 3 , 0 , 'lightblue@' );
            $table-SetCellAlign( 0 , 0 , "left" );

            // Add table to the graph
            $graph-Add($table);
            $graph-img-SetMargin( 20, 15, 20, 10 );

            // Display the graph
            $graph-img-SetImgFormat('JPEG');
            $graph-img-SetQuality(100);
            $graph-img-SetAntiAliasing();

            try{

              $set_image_name = WP_MYPLUGIN_PATH . "jpgraph/temp/img_graph" . rand( 0,10000 ) . ".jpeg";
              $pg_imag = $graph-Stroke( $set_image_name );                
              if ( is_null( $pg_imag ) ){ throw new Exception( "page4 pg_imag : pg_imag is_null  |  " ); var_dump( $table ); }
              if ( !isset( $pg_imag ) ){ throw new Exception( "page4 pg_imag : pg_imag is_not_set  |  " ); var_dump( $table ); }
              if ( empty( $pg_imag ) ){ throw new Exception( "page4 pg_imag : pg_imag is_empty  |  " ); var_dump( $table ); }

              if ( $pg_imag ){

                // Set position
                $val_x = 20; $real_scale = 90;
                $old_yaxis = $pdf-GetY(); $val_y = $old_yaxis;
                $pdf-Image( $set_image_name, $val_x, $val_y, $real_scale );

              }

            }
            catch ( Exception $error ){ echo "  - MY ERROR -| page4 [ image_stroke ] : " . $error-getMessage() . " (.end.) "; }

          }

        }
        catch ( Exception $error ){ echo "  - MY ERROR -| page4 [ technical_data ] : " . $error-getMessage() . " (.end.) "; }

      }

    }
    catch ( Exception $error ){ echo "  -init message-| MY ERROR : " . $error-getMessage() . " (.end.) "; }

Topic wp-error fatal-error linux plugin-development errors Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.