public static function create(\Exception $exception, $statusCode = null, array $headers = array()) { $e = new static(); $e->setMessage($exception->getMessage()); $e->setCode($exception->getCode()); if ($exception instanceof HttpExceptionInterface) { $statusCode = $exception->getStatusCode(); $headers = array_merge($headers, $exception->getHeaders()); } elseif ($exception instanceof RequestExceptionInterface) { $statusCode = 400; } if (null === $statusCode) { $statusCode = 500; } $e->setStatusCode($statusCode); $e->setHeaders($headers); $e->setTraceFromException($exception); $e->setClass(get_class($exception)); $e->setFile($exception->getFile()); $e->setLine($exception->getLine()); $previous = $exception->getPrevious(); if ($previous instanceof \Exception) { $e->setPrevious(static::create($previous)); } elseif ($previous instanceof \Throwable) { $e->setPrevious(static::create(new FatalThrowableError($previous))); } return $e; }
private static function extractHeaders(\Exception $exception) { if ($exception instanceof HttpExceptionInterface) { return $exception->getHeaders(); } return []; }
public static function create(\Exception $exception, $statusCode = null, array $headers = array()) { $e = new static(); $e->setMessage($exception->getMessage()); $e->setCode($exception->getCode()); if ($exception instanceof HttpExceptionInterface) { $statusCode = $exception->getStatusCode(); $headers = array_merge($headers, $exception->getHeaders()); } if (null === $statusCode) { $statusCode = 500; } $e->setStatusCode($statusCode); $e->setHeaders($headers); $e->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine()); $e->setClass(get_class($exception)); $e->setFile($exception->getFile()); $e->setLine($exception->getLine()); if ($exception->getPrevious()) { $e->setPrevious(static::create($exception->getPrevious())); } return $e; }
/** * {@inheritdoc} */ public function createErrorResponseByException(\Exception $ex = null) { //TODO - handle more content types if (null !== $ex && $ex instanceof HttpExceptionInterface) { $statusCode = $ex->getStatusCode(); $headers = (array) $ex->getHeaders(); } else { $statusCode = 500; $headers = array('Content-Type' => array('text/html; charset=UTF-8'), 'Cache-Control' => array('max-age=0', 'must-revalidate', 'no-cache', 'no-store', 'private')); } return $this->messageFactory->createResponse($this->errorPageLoader->loadContentByStatusCode($statusCode), $statusCode, $headers); }
/** * Format an exception as HTML and send appropriate exception info as HTTP headers * @param \Exception $e * @param array $title_tag * @param array $message_tag * @param array $trace_tag * @return string */ public static function htmlException(\Exception $e, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) { if ($e instanceof \http\Controller\Exception) { $code = $e->getCode() ?: 500; foreach ($e->getHeaders() as $key => $val) { HTTP::setResponseHeader($key, $val); } } else { $code = 500; } for ($html = ""; $e; $e = $e->getPrevious()) { $html .= static::htmlError(HTTP::getResponseStatusForCode($code), $e->getMessage(), $code, $e->getTraceAsString(), $title_tag, $message_tag, $trace_tag); } return $html; }
/** * create response * with a HttpException status code and headers are considered * other exceptions default to status code 500 * a error_docs/error_{status_code}.php template is parsed, when found * otherwise the exception data is decorated and dumped * * @param \Exception $e * @return \vxPHP\Http\Response */ protected function createResponse(\Exception $e) { if ($e instanceof HttpException) { $status = $e->getStatusCode(); $headers = $e->getHeaders(); } else { $status = Response::HTTP_INTERNAL_SERVER_ERROR; $headers = array(); } $config = Application::getInstance()->getConfig(); if (isset($config->paths['tpl_path'])) { $path = ($config->paths['tpl_path']['absolute'] ? '' : rtrim(Application::getInstance()->getRootPath(), DIRECTORY_SEPARATOR)) . $config->paths['tpl_path']['subdir']; if (file_exists($path . 'error_docs' . DIRECTORY_SEPARATOR . 'error_' . $status . '.php')) { $tpl = SimpleTemplate::create('error_docs' . DIRECTORY_SEPARATOR . 'error_' . $status . '.php'); $content = $tpl->assign('exception', $e)->assign('status', $status)->display(); } else { $content = $this->decorateException($e, $status); } } else { $content = $this->decorateException($e, $status); } return new Response($content, $status, $headers); }
public static function create(\Exception $exception, $statusCode = null, array $headers = array()) { $e = new static(); $e->setMessage($exception->getMessage()); $e->setCode($exception->getCode()); if ($exception instanceof HttpExceptionInterface) { //TODO: Despues quitar la dependencia a esta excepcion de symfony $statusCode = $exception->getStatusCode(); $headers = array_merge($headers, $exception->getHeaders()); } if (null === $statusCode) { $statusCode = 500; } $e->setStatusCode($statusCode); $e->setHeaders($headers); $e->setTraceFromException($exception); $e->setClass(get_class($exception)); $e->setFile($exception->getFile()); $e->setLine($exception->getLine()); if ($exception->getPrevious()) { $e->setPrevious(static::create($exception->getPrevious())); } return $e; }
/** * Converts an exception into a response. * * @param \Exception $e * An exception * @param Request $request * A Request instance * @param int $type * The type of the request (one of HttpKernelInterface::MASTER_REQUEST or * HttpKernelInterface::SUB_REQUEST) * * @return Response * A Response instance * * @throws \Exception * If the passed in exception cannot be turned into a response. */ protected function handleException(\Exception $e, $request, $type) { if ($e instanceof HttpExceptionInterface) { $response = new Response($e->getMessage(), $e->getStatusCode()); $response->headers->add($e->getHeaders()); return $response; } else { throw $e; } }
/** * Converts an exception into a response. * * @param \Exception $e * An exception * @param Request $request * A Request instance * @param int $type * The type of the request (one of HttpKernelInterface::MASTER_REQUEST or * HttpKernelInterface::SUB_REQUEST) * * @return Response * A Response instance */ protected function handleException(\Exception $e, $request, $type) { if ($e instanceof HttpExceptionInterface) { $response = new Response($e->getMessage(), $e->getStatusCode()); $response->headers->add($e->getHeaders()); return $response; } else { // @todo: _drupal_log_error() and thus _drupal_exception_handler() prints // the message directly. Extract a function which generates and returns it // instead, then remove the output buffer hack here. ob_start(); try { // @todo: The exception handler prints the message directly. Extract a // function which returns the message instead. _drupal_exception_handler($e); } catch (\Exception $e) { $message = Settings::get('rebuild_message', 'If you have just changed code (for example deployed a new module or moved an existing one) read <a href="https://www.drupal.org/documentation/rebuild">https://www.drupal.org/documentation/rebuild</a>'); if ($message && Settings::get('rebuild_access', FALSE)) { $rebuild_path = $GLOBALS['base_url'] . '/rebuild.php'; $message .= " or run the <a href=\"{$rebuild_path}\">rebuild script</a>"; } print $message; } return new Response(ob_get_clean(), 500); } }
/** * Return adapted (status code) Exception response from exception * @param \Exception $exception * @return \Symfony\Component\HttpFoundation\Response */ private function getAdaptedExceptionResponse(\Exception $exception) { $response = new Response(); if ($exception instanceof HttpExceptionInterface) { $response->setStatusCode($exception->getStatusCode()); $response->headers->replace($exception->getHeaders()); } else { $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); } return $response; }
/** * Get exception headers * * @param \Exception $exception * * @return array */ private function getHeaders(\Exception $exception) { $headers = $this->default['headers']; if ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) { $headers = $exception->getHeaders(); } return $headers; }