/** * Converts an Exception to a Response. * * @param Request $request The request * @param FlattenException $exception A FlattenException instance * @param DebugLoggerInterface $logger A DebugLoggerInterface instance * * @return Response * * @throws \InvalidArgumentException When the exception template does not exist */ public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) { $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); $code = $exception->getStatusCode(); if ($exception->GetClass() == 'UCI\\Boson\\ExcepcionesBundle\\Exception\\LocalException') { $arrayRutaFile = explode(DIRECTORY_SEPARATOR, $exception->getFile()); $strExtraer = ""; for ($i = count($arrayRutaFile) - 1; $i >= 0; $i--) { if (preg_match('/[a-zA-Z0-9]Bundle$/', $arrayRutaFile[$i]) == 1) { $direccionBundle = explode($strExtraer, $exception->getFile())[0]; $direccionFileExcepciones = $direccionBundle . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "config"; if (is_dir($direccionFileExcepciones)) { break; } } $strExtraer = DIRECTORY_SEPARATOR . $arrayRutaFile[$i] . $strExtraer; } $showInProd = false; $values = $this->getArrayExcepcionesInFile($direccionFileExcepciones); foreach ($values as $key => $excp) { if ($key == $exception->getCode() && array_key_exists('show_in_prod', $excp)) { $showInProd = $excp['show_in_prod']; } } return new Response($this->twig->render((string) $this->findTemplate($request, $request->getRequestFormat(), $code, $this->debug), array('status_code' => $code, 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', 'exception' => $exception, 'show_in_prod' => $showInProd, 'logger' => $logger, 'currentContent' => $currentContent))); } return new Response($this->twig->render((string) $this->findTemplate($request, $request->getRequestFormat(), $code, $this->debug), array('status_code' => $code, 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', 'exception' => $exception, 'logger' => $logger, 'currentContent' => $currentContent))); }
/** * Converts an Exception to a Response. * * @param Request $request The request * @param FlattenException $exception A FlattenException instance * @param DebugLoggerInterface $logger A DebugLoggerInterface instance * * @param string $_format * @return JsonResponse */ public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $_format = 'html') { $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); $wrapper = new ExceptionWrapper(); $wrapper->setCode($exception->getCode()); $wrapper->setMessage($exception->getMessage()); $wrapper->setStatusCode($exception->getStatusCode()); $wrapper->setTrace($this->debug ? $exception->getTrace() : array()); return $wrapper->getResponse(); }
/** * @param Request $request * @param FlattenException $exception * @param DebugLoggerInterface $logger * @return \GoIntegro\Hateoas\Http\JsonResponse */ public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = NULL) { $error = new ErrorObject(); $error->status = $exception->getStatusCode(); $error->title = $exception->getMessage(); $error->code = $exception->getCode(); $serializer = new ErrorSerializer($error); if (500 == $error->status && NULL != $logger) { $logger->error(sprintf(self::ERROR_LOG_MESSAGE_PATTERN, $error->title, $exception->getFile(), $exception->getLine(), $error->id)); } return $this->createNoCacheResponse($serializer->serialize(), $error->status, $exception->getHeaders()); }
/** * Converts an Exception to a Response. * * @param FlattenException $exception A FlattenException instance * @param DebugLoggerInterface $logger A DebugLoggerInterface instance * @param string $format The format to use for rendering (html, xml, ...) * @param Boolean $embedded Whether the rendered Response will be embedded or not * * @throws \InvalidArgumentException When the exception template does not exist */ public function exceptionAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html', $embedded = false) { $this->container->get('request')->setRequestFormat($format); $currentContent = ''; while (false !== ($content = ob_get_clean())) { $currentContent .= $content; } if ('Symfony\\Component\\Security\\Exception\\AccessDeniedException' === $exception->getClass()) { $exception->setStatusCode($exception->getCode()); } $templating = $this->container->get('templating'); $template = 'FrameworkBundle:Exception:' . ($this->container->get('kernel')->isDebug() ? 'exception.php' : 'error.php'); if (!$templating->exists($template)) { $this->container->get('request')->setRequestFormat('html'); } $response = $templating->renderResponse($template, array('exception' => $exception, 'logger' => $logger, 'currentContent' => $currentContent, 'embedded' => $embedded)); $response->setStatusCode($exception->getStatusCode()); return $response; }
/** * Converts an Exception to a Response. * * @param FlattenException $exception A FlattenException instance * @param DebugLoggerInterface $logger A DebugLoggerInterface instance * @param string $format The format to use for rendering (html, xml, ...) * * @throws \InvalidArgumentException When the exception template does not exist */ public function exceptionAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html') { $this->container->get('request')->setRequestFormat($format); $currentContent = ''; while (ob_get_level()) { $currentContent .= ob_get_clean(); } if ('Symfony\Component\Security\Exception\AccessDeniedException' === $exception->getClass()) { $exception->setStatusCode($exception->getCode()); } $template = $this->container->get('kernel')->isDebug() ? 'exception' : 'error'; if ($this->container->get('kernel')->isDebug() && 'html' == $format) { $template = 'exception_full'; } $template = 'FrameworkBundle:Exception:'.$template.'.twig'; $templating = $this->container->get('templating'); if (!$templating->exists($template)) { $this->container->get('request')->setRequestFormat('html'); } $response = $templating->renderResponse( $template, array( 'exception' => $exception, 'logger' => $logger, 'currentContent' => $currentContent, ) ); $response->setStatusCode($exception->getStatusCode()); return $response; }
/** * Retrieve a textual description of the given Exception, or FlattenException (as passed by Symfony HttpKernel). * * @param \Symfony\Component\HttpKernel\Exception\FlattenException|\Exception $exception * @param string|null $adminName * @param string|null $adminEmail * @param string $heading * @param null $stackTraceHeading * * @return string */ public static function exception($exception, $adminName = null, $adminEmail = null, $heading = null, $stackTraceHeading = null) { // Generate the error message. ob_start(); echo $heading ?: '<h1>Error</h1>', PHP_EOL; echo '<p><strong>', $exception->getMessage(), '</strong></p>', PHP_EOL; echo '<p>Error occurred at ', date('Y-m-d H:i:s'), ', code: ', $exception->getCode(), ', ', TypeUtilities::describe($exception), '</p>', PHP_EOL; echo '<p>Please contact your system administrator for assistance.</p>' . PHP_EOL; if (!empty($adminName) || !empty($adminEmail)) { echo '<ul>', PHP_EOL; if (!empty($adminName)) { echo '<li>Name: ', $adminName, '</li>', PHP_EOL; } if (!empty($adminEmail)) { echo '<li>Email Address: ', $adminEmail, '</li>', PHP_EOL; } echo '</ul>', PHP_EOL; } echo '<hr/>', PHP_EOL; echo $stackTraceHeading ?: '<h2>Stack Trace</h2>', PHP_EOL; echo '<ol>', PHP_EOL; foreach (array_reverse($exception->getTrace()) as $traceItem) { $file = isset($traceItem['file']) ? $traceItem['file'] : ''; $line = isset($traceItem['line']) ? sprintf('(%s)', $traceItem['line']) : ''; $function = isset($traceItem['function']) ? isset($traceItem['class']) && !empty($traceItem['class']) ? sprintf('%s->%s', $traceItem['class'], $traceItem['function']) : $traceItem['function'] : ''; echo '<li>', $file, $line, ': ', $function, '()</li>', PHP_EOL; } echo '</ol>'; return ob_get_clean(); }