Base::error PHP Method

error() public method

Log error; Execute ONERROR handler if defined, else display default error page (HTML for synchronous requests, JSON string for AJAX requests)
public error ( $code, $text = '', array $trace = NULL, $level ) : null
$code int
$text string
$trace array array
$level int
return null
    function error($code, $text = '', array $trace = NULL, $level = 0)
    {
        $prior = $this->hive['ERROR'];
        $header = $this->status($code);
        $req = $this->hive['VERB'] . ' ' . $this->hive['PATH'];
        if (!$text) {
            $text = 'HTTP ' . $code . ' (' . $req . ')';
        }
        error_log($text);
        $trace = $this->trace($trace);
        foreach (explode("\n", $trace) as $nexus) {
            if ($nexus) {
                error_log($nexus);
            }
        }
        if ($highlight = !$this->hive['CLI'] && !$this->hive['AJAX'] && $this->hive['HIGHLIGHT'] && is_file($css = __DIR__ . '/' . self::CSS)) {
            $trace = $this->highlight($trace);
        }
        $this->hive['ERROR'] = ['status' => $header, 'code' => $code, 'text' => $text, 'trace' => $trace, 'level' => $level];
        $this->expire(-1);
        $handler = $this->hive['ONERROR'];
        $this->hive['ONERROR'] = NULL;
        $eol = "\n";
        if ((!$handler || $this->call($handler, [$this, $this->hive['PARAMS']], 'beforeroute,afterroute') === FALSE) && !$prior && !$this->hive['CLI'] && !$this->hive['QUIET']) {
            echo $this->hive['AJAX'] ? json_encode($this->hive['ERROR']) : '<!DOCTYPE html>' . $eol . '<html>' . $eol . '<head>' . '<title>' . $code . ' ' . $header . '</title>' . ($highlight ? '<style>' . $this->read($css) . '</style>' : '') . '</head>' . $eol . '<body>' . $eol . '<h1>' . $header . '</h1>' . $eol . '<p>' . $this->encode($text ?: $req) . '</p>' . $eol . ($this->hive['DEBUG'] ? '<pre>' . $trace . '</pre>' . $eol : '') . '</body>' . $eol . '</html>';
        }
        if ($this->hive['HALT']) {
            die;
        }
    }

Usage Example

Example #1
0
 /**
  * auth service callback
  * @param Base $f3
  * @param $params
  */
 function callback(\Base $f3, $params)
 {
     $Opauth = new \Opauth($this->config, false);
     switch ($Opauth->env['callback_transport']) {
         case 'session':
             $response = $f3->get('SESSION.opauth');
             $f3->clear('SESSION.opauth');
             break;
         case 'post':
             $response = unserialize(base64_decode($f3->get('POST.opauth')));
             break;
         case 'get':
             $response = unserialize(base64_decode($f3->get('GET.opauth')));
             break;
         default:
             $f3->error(400, 'Unsupported callback_transport');
             break;
     }
     if (isset($response['error'])) {
         $f3->call($this->abortFunc, array($response));
         return;
     }
     $data = $response['auth'];
     // validate
     if (empty($data) || empty($response['timestamp']) || empty($response['signature']) || empty($data['provider']) || empty($data['uid'])) {
         $f3->error(400, 'Invalid auth response: Missing key auth response components');
     } elseif (!$Opauth->validate(sha1(print_r($data, true)), $response['timestamp'], $response['signature'], $reason)) {
         $f3->error(400, 'Invalid auth response: ' . $reason);
     } else {
         // It's all good
         $f3->call($this->successFunc, array($data));
     }
 }
All Usage Examples Of Base::error