Plugin Directory

Changeset 1073459


Ignore:
Timestamp:
01/22/2015 04:39:49 PM (11 years ago)
Author:
aercolino
Message:

Added support for non catchable shutdown errors. They still make the page break badly but you can now see the error in the javascript console as any other catchable error.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • enzymes/trunk/src/Enzymes3.php

    r1068480 r1073459  
    299299        $this->init_expressions();
    300300        $this->options = new EnzymesOptions();
     301
     302        $this->has_eval_recovered = true;
     303        register_shutdown_function(array($this, 'echo_last_eval_error'));
    301304    }
    302305
     
    324327     * Echo a script HTML tag to write data to the javascript console of the browser.
    325328     *
    326      * @param mixed $data
    327      */
    328     protected
    329     function console_log( $data )
    330     {
    331         $json   = json_encode((is_array($data) || is_object($data))
    332                 ? $data
    333                 : trim($data));
    334         $output = "<script>if(window.console){if(window.console.log){window.console.log($json);}}</script>";
     329     * @param mixed $logs
     330     */
     331    protected
     332    function console_log( $logs )
     333    {
     334        if ( count($logs) == 0 ) {
     335            return;
     336        }
     337        $lines = array();
     338        foreach ($logs as $data) {
     339            $json    = json_encode((is_array($data) || is_object($data))
     340                    ? $data
     341                    : trim($data));
     342            $lines[] = "window.console.log($json);";
     343        }
     344        $lines  = implode('', $lines);
     345        $output = "<script>if(window.console){if(window.console.log){$lines}}</script>";
    335346        echo $output;
    336347    }
    337348
     349    protected $has_eval_recovered;
     350
     351    protected
     352    function decorate( $title, $output )
     353    {
     354        $logs[] = $title;
     355        $logs[] = sprintf(__('Post: %1$s - Enzyme: %3$s - Injection: {[%2$s]}'), $this->injection_post->ID,
     356                $this->current_sequence, $this->current_enzyme);
     357        $logs[] = $output;
     358        return $logs;
     359    }
     360
     361    public
     362    function echo_last_eval_error()
     363    {
     364        // Only execute after a bad eval.
     365        if ( $this->has_eval_recovered ) {
     366            return;
     367        }
     368        // We are shutting down, so $error is really the last (fatal) error.
     369        $error = error_get_last();
     370        echo "\n";
     371        $this->console_log($this->decorate(__('ENZYMES FATAL ERROR'),
     372                sprintf(__('Fatal error: %1$s on line %2$s'), $error['message'], $error['line'])));
     373        echo "\n";
     374    }
     375
    338376    /**
    339377     * Handle an error in eval.
    340378     *
    341      * @param $type
    342      * @param $message
    343      * @param $file
    344      * @param $line
     379     * @param int    $type
     380     * @param string $message
     381     * @param string $file
     382     * @param int    $line
     383     * @param array  $context
    345384     *
    346385     * @return bool
    347386     */
    348387    protected
    349     function set_last_eval_error( $type, $message, $file, $line )
    350     {
    351         $this->last_eval_error = array('type' => $type, 'message' => $message, 'file' => $file, 'line' => $line);
    352         return true;
     388    function set_last_eval_error( $type = null, $message = null, $file = null, $line = null, $context = null )
     389    {
     390        $this->last_eval_error = array(
     391                'type'    => $type,
     392                'message' => $message,
     393                'file'    => $file,
     394                'line'    => $line,
     395                'context' => $context
     396        );
     397        return true;  // True to consider the error handled and suppress bubbling.
    353398    }
    354399
     
    375420
    376421        $result = trim($result);
    377         $result = str_replace($filename, 'enzyme code', $result);
    378         $result = str_replace("\nErrors parsing enzyme code", '', $result);
     422        $result = str_replace("in $filename on", 'on', $result);
     423        $result = str_replace("\nErrors parsing $filename", '', $result);
    379424        return $result;
    380425    }
     
    395440    function safe_eval( $code, array $arguments = array() )
    396441    {
    397         $error        = $output = $exception = null;
    398         $previous_ini = array();
    399 
    400442        $previous_ini['scream.enabled'] = ini_set('scream.enabled', false);
    401         set_error_handler(array($this, 'set_last_eval_error'), E_ALL);
     443        set_error_handler(array($this, 'set_last_eval_error'), E_ALL | E_STRICT);
    402444        ob_start();
     445        $this->has_eval_recovered = false;
     446        $this->last_eval_error    = null;
     447        // -------------------------------------------------------------------------------------------------------------
    403448        try {
    404             $this->last_eval_error = null;
    405             // ---------------------------------------------------------------------------------------------------------
    406449            $result = @eval($code);
    407             // ---------------------------------------------------------------------------------------------------------
    408             $error                 = $this->last_eval_error;
    409             $this->last_eval_error = null;
     450            $error  = $this->last_eval_error;
    410451        } catch ( Exception $e ) {
    411             $result    = false;  // Let's force the same error treatment
    412             $exception = $e;     // but remember the exception now.
    413         }
    414         $output = ob_get_clean();
     452            $result = false;  // Let's force the same error treatment
     453            $error  = $e;     // and take the exception as the error.
     454        }
     455        // -------------------------------------------------------------------------------------------------------------
     456        $this->last_eval_error    = null;
     457        $this->has_eval_recovered = true;
     458        $output                   = ob_get_clean();
    415459        restore_error_handler();
    416460        ini_set('scream.enabled', $previous_ini['scream.enabled']);
    417461
    418462        if ( false === $result ) {
    419             if ( null === $error && null === $exception ) {
     463            if ( ! $error instanceof Exception ) {
    420464                $error = $this->php_lint($code);
    421             }
    422             if ( null === $error ) {
    423                 $error = $exception;
    424465            }
    425466        }
Note: See TracChangeset for help on using the changeset viewer.