Search code examples
wordpressnginxfastcgifatal-errorhhvm

HHVM outputs status code 200 on fatal errors and I can't use fastcgi_next_upstream because of that


I wanted to fallback to next upstream (php5-fpm) on nginx when HHVM fails. This isn't currently possible because HHVM outputs 200 OK response code after fatal error. At least I wanted to give more informative status code.

PHP Error-log:

\nFatal error: $this is null in /data/wordpress/htdocs/wp-content/plugins/woocommerce-gateway-klarna/classes/class-klarna-account.php on line 1231

And I know for sure that it works in php5-fpm.

HHVM version:

hhvm --version
HipHop VM 3.4.0 (rel)
Compiler: tags/HHVM-3.4.0-0-g817b3a07fc4e509ce15635dbc87778e5b3496663
Repo schema: 0e12aaa31fae66b5591f65603de50c9d62caafac
Extension API: 20140829

Solution

  • I fixed this by injecting custom error handler for hhvm.

    My index.php for wordpress:

    <?php
    
    /**
     * HHVM outputs http status: 200 OK even on fatal errors.
     * Catch fatal errors on HHVM and put right response code.
     */
    
    //Is this HHVM?
    if (defined('HHVM_VERSION')) {
      set_error_handler('catch_fatal_error_hhvm',E_ERROR);
    }
    function catch_fatal_error_hhvm() {
      http_response_code(500);
      die();
    }
    
    // WordPress view bootstrapper
    define('WP_USE_THEMES', true);
    require(dirname( __FILE__ ) . '/wordpress/wp-blog-header.php');