Пример #1
0
 /**
  * Register or run error
  *
  * @param string   $type
  * @param callable $route
  * @throws \InvalidArgumentException
  * @throws \Exception
  */
 public function handleError($type, $route = null)
 {
     if (!isset($this->injectors['errors'][$type])) {
         throw new \InvalidArgumentException('Unknown error type "' . $type . '" to call');
     }
     if (is_string($route) && class_exists($route) || $route instanceof \Closure) {
         // Set error handle
         $this->injectors['errors'][$type][2] = $route;
     } else {
         if (!$this->injectors['exception'] && $route instanceof \Exception) {
             // Not handle exception and throw it
             throw $route;
         } else {
             // Clean the output buffer
             ob_get_level() && ob_clean();
             // Check handle route and run it
             if (empty($this->injectors['errors'][$type][2]) || !$this->router->run($this->injectors['errors'][$type][2], array('error' => array('type' => $type, 'error' => $route, 'message' => $this->injectors['errors'][$type])))) {
                 /**
                  * Get error information
                  */
                 $error = $route instanceof \Exception ? $route : null;
                 $title = $error ? $error->getMessage() : $this->injectors['errors'][$type][1];
                 $code = $this->injectors['errors'][$type][0];
                 $message = $route ? $error ? $error->getFile() . ' [' . $error->getLine() . ']' : (string) $route : '';
                 if ($this->injectors['cli']) {
                     // Cli error show
                     $this->halt($code, Console::text('[' . $title . '] ' . ($message ? $message : '"' . $this->input->path() . '"') . ' ', 'redbg') . "\n" . ($this->injectors['debug'] && $error ? Console::text("\n[Error]\n", "yellow") . $error . "\n" : ''));
                 } else {
                     // Http error show
                     $this->output->status($this->injectors['errors'][$type][0]);
                     if ($this->injectors['debug']) {
                         // Debug error show
                         $this->renderView('Error', array('title' => $title, 'message' => $message ? $message : 'Could not ' . $this->input->method() . ' ' . $this->input->path(), 'stacks' => $this->injectors['debug'] && $error ? $error->getTraceAsString() : null));
                     } else {
                         // Normal error show
                         $this->renderView('Error', array('title' => $title, 'message' => $message ? $message : 'Could not ' . $this->input->method() . ' ' . $this->input->path()));
                     }
                     $this->stop();
                 }
             }
         }
     }
 }
Пример #2
0
Файл: App.php Проект: pagon/core
 /**
  * Register or run error
  *
  * @param string   $type
  * @param callable $route
  * @throws \InvalidArgumentException
  */
 public function handleError($type, $route = null)
 {
     if (!isset($this->injectors['errors'][$type])) {
         throw new \InvalidArgumentException('Unknown error type "' . $type . '" to call');
     }
     if (is_string($route) && is_subclass_of($route, Route::_CLASS_, true) || $route instanceof \Closure) {
         $this->injectors['errors'][$type][2] = $route;
     } else {
         ob_get_level() && ob_clean();
         ob_start();
         if (empty($this->injectors['errors'][$type][2]) || !$this->router->run($this->injectors['errors'][$type][2], array('error' => array('type' => $type, 'error' => $route, 'message' => $this->injectors['errors'][$type])))) {
             if ($this->injectors['cli']) {
                 $this->halt($this->injectors['errors'][$type][0], $this->injectors['errors'][$type][1]);
             } else {
                 $this->output->status($this->injectors['errors'][$type][0]);
                 $this->render('pagon/views/error.php', array('title' => $this->injectors['errors'][$type][1], 'message' => $route ? $route instanceof \Exception ? $route->getMessage() : (string) $route : 'Could not ' . $this->input->method() . ' ' . $this->input->path()));
                 $this->stop();
             }
         }
     }
 }