/** * @param Exception $exception */ public function exception(Exception $exception) { $this->request = Cordillera::$instance->{'request'}; $this->controller = Cordillera::$instance->{'controller'}; $this->config = Cordillera::$instance->{'config'}; $this->logger = Cordillera::$instance->{'logger'}; $this->response = Cordillera::$instance->{'response'}; if ($this->request->isAjax() || isset($this->controller) && ($this->controller->response_type == 'json' || $this->controller->is_rest)) { $response = ['error' => true, 'message' => $exception->getMessage()]; if (CORDILLERA_DEBUG) { $response['trace'] = $exception->getAllTraces(); } if ($this->config->get('exception.show_log_id') && $this->logger->last_log_id) { $response['log_id'] = logger()->last_log_id; } $this->response->json($response); } else { $layout = new Layout('error'); if (CORDILLERA_DEBUG) { $layout->properties['title'] = Exception::$types[$exception->getCode()]; } if (ob_get_length()) { ob_end_clean(); } echo $layout->render($exception->toHtml()); } exit; }
/** * @param View|array|string $response * * @throws Exception */ public function setResponse($response) { if (!$this->is_rest) { $this->response->setSecurityHeaders(); } if ($response instanceof View && $this->response_type == 'html' && !$this->is_rest) { $this->response->raw($response->render()); } elseif ($this->is_rest || $this->response_type == 'json' || is_array($response) || is_object($response) && !$response instanceof View) { if ($response instanceof View) { throw new Exception(translate('Response can not be a instance of cordillera\\middlewares\\View object'), 500, Exception::BADARGUMENTS); } $this->response->json($response); } }