Ejemplo n.º 1
0
 public static function handle($e, $send_response = true)
 {
     try {
         Hook::triggerAction('exception.handle', array(&$e));
         $log_message = sprintf('%-4s %s', "({$e->getCode()})", $e->getMessage());
         // get the channel manually so the introspection works properly.
         $level = Log::ERROR;
         if ($e instanceof ErrorException && isset(self::$levels[$e->getSeverity()])) {
             $level = self::$levels[$e->getSeverity()];
         }
         Log::getChannel('exception')->addRecord($level, $log_message, array('exception' => $e));
         $request = static::$request;
         if ($request === null) {
             $request = Request::createFromGlobals();
         }
         $action = Action::getDefaultAction(self::$_controller);
         $action->setRespondsWith(array('*'), false);
         $response = Controller::invoke($action, $request, array('exception' => $e));
         $response->prepare($request);
         if ($send_response) {
             $response->send();
         }
         return $response;
     } catch (\Exception $_e) {
         $response = new Response();
         $response->setStatusCode(500);
         if (Core::$_MODE === Core::MODE_PRODUCTION) {
             $response->setContent("Internal server error");
         } else {
             $response->setContent($e->__toString() . "\n\n\nAdditionally, the following exception occurred while trying to handle the error:\n\n" . $_e->__toString());
         }
         return $response;
     }
 }
Ejemplo n.º 2
0
 /**
  * @param Request $request
  *
  * @throws \LogicException
  * @throws Http\Exception\NotFoundException
  * @return Response
  */
 public function route(Request $request = null)
 {
     if (null === $request) {
         $request = Request::createFromGlobals();
     }
     $this->request = $request;
     $this->request_uri = $request->getPath();
     if (strrpos($this->request_uri, $request->getFormat()) !== false) {
         $this->request_uri = substr($this->request_uri, 0, -(strlen($request->getFormat()) + 1));
     }
     $this->request_method = $request->getMethod();
     Hook::triggerAction('router.before_routing', array(&$this));
     $url = $this->request_method . $this->request_uri;
     $node = $this->getRootNode()->findChild($url, $this->request);
     /** @var \Wave\Router\Action $action */
     if ($node instanceof Router\Node && ($action = $node->getAction())) {
         Hook::triggerAction('router.before_invoke', array(&$action, &$this));
         $this->request->setAction($action);
         $this->response = Controller::invoke($action, $this->request);
         Hook::triggerAction('router.before_response', array(&$action, &$this));
         if (!$this->response instanceof Response) {
             throw new \LogicException("Action {$action->getAction()} should return a \\Wave\\Http\\Response object", 500);
         } else {
             return $this->response->prepare($this->request);
         }
     } else {
         throw new NotFoundException('The requested URL ' . $url . ' does not exist', $this->request);
     }
 }