/**
 * Generate a fatal error, printing the $error and sending the header
 * associated with the $code. By default, this function renders the
 * error message with the error template. If you provide an ErrorController
 * that responds to an action called _$code, it will use that instead.
 * This lets you use the default error handling while developing an app
 * and switch to a more polished solution later.
 * This method can also be handy for testing - call it without args to see
 * all the data associated with a request.
 *
 * @see send_header()
 * @param string $error
 * @param integer $code
 * @return void
 *
 */
function fatal($error = 'Fatal error.', $code = 500)
{
    send_header($code);
    error_log($error);
    // do a mock request to see if we can handle this error with a controller
    $manager = new PathManager(WEB_ROOT . "/error/_{$code}");
    $route = $manager->build_route();
    $instance = $manager->controller_instance($route->controller);
    if ($instance !== FALSE && method_exists($instance, $route->action) !== FALSE) {
        $route->params = array('error' => $error, 'code' => $code);
        $action = $route->action;
        $instance->{$action}($route->params);
    } else {
        template('error');
        sys()->data->error = $error;
        sys()->data->code = $code;
    }
    render($route);
    exit;
}