create() public static méthode

public static create ( Exception $exception, $statusCode = null, array $headers = [] )
$exception Exception
$headers array
Exemple #1
0
 public function sendMail(\Exception $exception, Request $request, array $context, $needToFlush)
 {
     if (!$exception instanceof FlattenException) {
         $exception = FlattenException::create($exception);
     }
     if (!$this->_hasInitialized) {
         $this->_initialize();
     }
     $params = array('exception' => $exception, 'request' => $request, 'context' => $context, 'status_text' => Response::$statusTexts[$exception->getStatusCode()]);
     $preMailEvent = new GenericEvent($params, array('shouldSend' => true));
     $this->_eventDispatcher->dispatch('ehough.bundle.emailErrors.preMail', $preMailEvent);
     if (!$preMailEvent->getArgument('shouldSend')) {
         //mail was cancelled
         return;
     }
     $body = $this->_templatingEngine->render('EhoughEmailErrorsBundle::mail.html.twig', $params);
     $subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage();
     if (function_exists('mb_substr')) {
         $subject = mb_substr($subject, 0, 255);
     } else {
         $subject = substr($subject, 0, 255);
     }
     $mail = \Swift_Message::newInstance()->setSubject($subject)->setFrom($this->_fromAddress)->setTo($this->_toAddress)->setContentType('text/html')->setBody($body);
     $this->_mailer->send($mail);
     if ($needToFlush) {
         $this->_flushEmailer();
     }
 }
 /**
  * Clones the request for the exception.
  *
  * @param \Exception $exception
  * @param  Request   $request
  * @return Request   $request
  */
 protected function duplicateRequest(\Exception $exception, Request $request)
 {
     $attributes = ['_controller' => $this->controller, 'exception' => FlattenException::create($exception), 'logger' => $this->logger];
     $request = $request->duplicate(null, null, $attributes);
     $request->setMethod('GET');
     return $request;
 }
 public function sendException($exception)
 {
     if (!$this->isErrorFromBot()) {
         $recipients = Config::get("error-emailer::to");
         if (isset($recipients['address'])) {
             // this is a single recipient
             if ($recipients['address']) {
                 $recipients = array($recipients);
             } else {
                 $recipients = array();
             }
         }
         if (sizeof($recipients) > 0) {
             if ($exception instanceof FlattenException) {
                 $flattened = $exception;
             } else {
                 $flattened = FlattenException::create($exception);
             }
             $handler = new ExceptionHandler();
             $content = $handler->getContent($flattened);
             $model = array('trace' => $content, 'exception' => $exception, 'flattened' => $flattened);
             Mail::send(Config::get("error-emailer::error_template"), $model, function ($message) use($model, $recipients) {
                 $subject = View::make(Config::get("error-emailer::subject_template"), $model)->render();
                 $message->subject($subject);
                 foreach ($recipients as $to) {
                     $message->to($to['address'], $to['name']);
                 }
             });
         }
     }
 }
Exemple #4
0
 protected function convertExceptionToResponse(Exception $e)
 {
     $e = FlattenException::create($e);
     $handler = new KommerceExceptionHandler(config('app.debug'));
     $decorated = $this->decorate($handler->getContent($e), $handler->getStylesheet($e));
     return SymfonyResponse::create($decorated, $e->getStatusCode(), $e->getHeaders());
 }
Exemple #5
0
 /**
  * Render an exception into an HTTP response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Exception  $e
  * @return \Illuminate\Http\Response
  */
 public function render($request, Exception $e)
 {
     switch (get_class($e)) {
         case "Swift_TransportException":
         case "PDOException":
             $errorView = "errors.500_config";
             break;
         case "ErrorException":
             $errorView = "errors.500";
             break;
         case "Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException":
             return abort(400);
         default:
             $errorView = false;
             break;
     }
     if ($errorView) {
         // This makes use of a Symfony error handler to make pretty traces.
         $SymfonyDisplayer = new SymfonyDisplayer(config('app.debug'));
         $FlattenException = FlattenException::create($e);
         $SymfonyCss = $SymfonyDisplayer->getStylesheet($FlattenException);
         $SymfonyHtml = $SymfonyDisplayer->getContent($FlattenException);
         $response = response()->view($errorView, ['exception' => $e, 'error_class' => get_class($e), 'error_css' => $SymfonyCss, 'error_html' => $SymfonyHtml], 500);
         return $this->toIlluminateResponse($response, $e);
     } else {
         return parent::render($request, $e);
     }
 }
Exemple #6
0
 /**
  * {@inheritdoc}
  */
 public function createResponse($exception)
 {
     if ($exception instanceof HttpException) {
         $exception = FlattenException::create($exception, $exception->getCode());
     }
     return parent::createResponse($exception);
 }
Exemple #7
0
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     $request = $event->getRequest();
     $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
     $attributes = array('_controller' => $this->controller, 'exception' => FlattenException::create($exception), 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null, 'format' => $request->getRequestFormat());
     $request = $request->duplicate(null, null, $attributes);
     $request->setMethod('GET');
     try {
         $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
     } catch (\Exception $e) {
         $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false);
         $wrapper = $e;
         while ($prev = $wrapper->getPrevious()) {
             if ($exception === ($wrapper = $prev)) {
                 throw $e;
             }
         }
         $prev = new \ReflectionProperty('Exception', 'previous');
         $prev->setAccessible(true);
         $prev->setValue($wrapper, $exception);
         throw $e;
     }
     $event->setResponse($response);
 }
 public function handleKernelException(GetResponseForExceptionEvent $event)
 {
     if ($this->container->get('kernel')->getEnvironment() !== 'dev') {
         $exception = FlattenException::create($event->getException());
         // First, log the exception to the standard error logs.
         $this->container->get('logger')->error(' In File ' . $exception->getFile() . ', on line ' . $exception->getLine() . ': ' . $exception->getMessage());
         // Determine what the HTTP status code should be.
         if ($event->getException() instanceof \Symfony\Component\HttpKernel\Exception\HttpException) {
             $httpStatusCode = $event->getException()->getStatusCode();
         } else {
             $httpStatusCode = $exception->getCode();
             if ($exception->getCode() < 100 || $exception->getCode() >= 600) {
                 $httpStatusCode = 500;
             }
         }
         $parameters = ['status_code' => $httpStatusCode, 'status_text' => $exception->getMessage(), 'exception' => $exception];
         if (in_array('application/json', $event->getRequest()->getAcceptableContentTypes())) {
             $errorContent = $this->container->get('templating')->render(':default:exception.json.twig', $parameters);
         } else {
             $errorContent = $this->container->get('templating')->render(':default:error.html.twig', $parameters);
         }
         $response = new Response($errorContent, $httpStatusCode);
         $response->setProtocolVersion('1.1');
         $event->setResponse($response);
     }
 }
 private function saveDebugInformation(\Exception $ex = null)
 {
     if (!$this->input->hasOption('jms-job-id') || null === ($jobId = $this->input->getOption('jms-job-id'))) {
         return;
     }
     $this->getConnection()->executeUpdate("UPDATE jms_jobs SET stackTrace = :trace, memoryUsage = :memoryUsage, memoryUsageReal = :memoryUsageReal WHERE id = :id", array('id' => $jobId, 'memoryUsage' => memory_get_peak_usage(), 'memoryUsageReal' => memory_get_peak_usage(true), 'trace' => serialize($ex ? class_exists(FlattenException::class) ? FlattenException::create($ex) : LegacyFlattenException::create($ex) : null)), array('id' => \PDO::PARAM_INT, 'memoryUsage' => \PDO::PARAM_INT, 'memoryUsageReal' => \PDO::PARAM_INT, 'trace' => \PDO::PARAM_LOB));
 }
 public function createResponse($exception, $onlyHtmlBodyContent = false)
 {
     if (!$exception instanceof FlattenException) {
         $exception = FlattenException::create($exception);
     }
     return Response::create($this->render($exception, $onlyHtmlBodyContent), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset);
 }
 /**
  * Render an exception into an HTTP response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Exception  $e
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function render($request, Exception $e)
 {
     $e = FlattenException::create($e);
     $handler = new SymfonyExceptionHandler(env('APP_DEBUG', false));
     $decorated = $this->decorate($handler->getContent($e), $handler->getStylesheet($e));
     return Response::create($decorated, $e->getStatusCode(), $e->getHeaders());
 }
Exemple #12
0
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     $flattenException = FlattenException::create($exception);
     $msg = 'Something went wrong! (' . $flattenException->getMessage() . ')';
     $event->setResponse(new Response($msg, $flattenException->getStatusCode()));
 }
 public function createApplication()
 {
     $app = new ApplicationTest();
     $app['route_class'] = 'Euskadi31\\Silex\\Controller\\RouteTest';
     $app['debug'] = true;
     //unset($app['exception_handler']);
     $app->mount('/', new AuthorizeControllerProvider());
     $app->error(function (Exception $exception, $code) use($app) {
         $e = FlattenException::create($exception);
         $headers = [];
         if ($exception instanceof HttpExceptionInterface) {
             $headers = $exception->getHeaders();
             $code = $exception->getStatusCode();
         } else {
             $code = $exception->getCode();
         }
         if ($code < 100 || $code >= 600) {
             $code = 500;
         }
         $error = ['error' => ['message' => $exception->getMessage(), 'type' => join('', array_slice(explode('\\', get_class($exception)), -1)), 'code' => $code]];
         if ($this->app['debug']) {
             $error['error']['exception'] = $e->toArray();
         }
         return new Response($app->json($error, $code, $headers));
     });
     return $app;
 }
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     static $handling;
     if (true === $handling) {
         return false;
     }
     $handling = true;
     $exception = $event->getException();
     $request = $event->getRequest();
     $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
     $attributes = array('_controller' => $this->controller, 'exception' => FlattenException::create($exception), 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null, 'format' => $request->getRequestFormat());
     $request = $request->duplicate(null, null, $attributes);
     $request->setMethod('GET');
     try {
         $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true);
     } catch (\Exception $e) {
         $this->logException($exception, sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()), false);
         // set handling to false otherwise it wont be able to handle further more
         $handling = false;
         // re-throw the exception from within HttpKernel as this is a catch-all
         return;
     }
     $event->setResponse($response);
     $handling = false;
 }
Exemple #15
0
 /**
  * Creates the error Response associated with the given Exception.
  *
  * @param \Exception|FlattenException $exception An \Exception instance
  *
  * @return Response A Response instance
  */
 public function createResponse($exception)
 {
     if (!$exception instanceof FlattenException) {
         $exception = FlattenException::create($exception);
     }
     return new Response($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders());
 }
 protected function handleWithSymfony(Exception $exception)
 {
     if (!$exception instanceof FlattenException) {
         $exception = FlattenException::create($exception);
     }
     $handler = new ExceptionHandler($this->debug);
     return Response::create($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders());
 }
 /**
  * Rename for local development by removing underscores.
  */
 public function _____testSimpleException()
 {
     $request = $this->buildRequest();
     $exception = new \RuntimeException('Hello', 404, new \InvalidArgumentException('Invalid argument, or something', 500));
     $exception = FlattenException::create($exception);
     $context = array('a' => 'b', 'c' => array('x' => 'y', array('t' => 'q')));
     $body = $this->_kernel->getContainer()->get('templating')->render('EhoughEmailErrorsBundle::mail.html.twig', array('exception' => $exception, 'request' => $request, 'context' => $context, 'status_text' => Response::$statusTexts[$exception->getStatusCode()]));
     file_put_contents('/tmp/out.txt', $body);
 }
Exemple #18
0
 /**
  * Render an exception into an HTTP response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Exception  $e
  * @return \Illuminate\Http\Response
  */
 public function render($request, Exception $e)
 {
     if (!env('APP_DEBUG', true)) {
         if (!$e instanceof FlattenException) {
             $e = FlattenException::create($e);
         }
         return response()->json(['error' => $e->getStatusCode() == 404 ? 'Method not found' : 'Unknown error occurred'], $e->getStatusCode());
     }
     return parent::render($request, $e);
 }
 public function onSilexError(GetResponseForExceptionEvent $event)
 {
     $handler = new DebugExceptionHandler($this->debug);
     $exception = $event->getException();
     if (!$exception instanceof FlattenException) {
         $exception = FlattenException::create($exception);
     }
     $response = Response::create($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders())->setCharset(ini_get('default_charset'));
     $event->setResponse($response);
 }
 /**
  * Render an exception into a response.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Exception               $e
  *
  * @return \Illuminate\Http\Response
  */
 public function render($request, Exception $e)
 {
     $id = $this->container->make(ExceptionIdentifier::class)->identify($e);
     $e = $this->getTransformed($e);
     $flattened = FlattenException::create($e);
     $code = $flattened->getStatusCode();
     $headers = $flattened->getHeaders();
     $response = $this->getDisplayer($e)->display($e, $id, $code, $headers);
     return $this->toIlluminateResponse($response, $e);
 }
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        static $handling;

        if (true === $handling) {
            return false;
        }

        $handling = true;

        $exception = $event->getException();
        $request = $event->getRequest();

        $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));

        $attributes = array(
            '_controller' => $this->controller,
            'exception' => FlattenException::create($exception),
            'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
            // keep for BC -- as $format can be an argument of the controller callable
            // see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
            // @deprecated in 2.4, to be removed in 3.0
            'format' => $request->getRequestFormat(),
        );

        $request = $request->duplicate(null, null, $attributes);
        $request->setMethod('GET');

        try {
            $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true);
        } catch (\Exception $e) {
            $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false);

            // set handling to false otherwise it wont be able to handle further more
            $handling = false;

            $wrapper = $e;

            while ($prev = $wrapper->getPrevious()) {
                if ($exception === $wrapper = $prev) {
                    throw $e;
                }
            }

            $prev = new \ReflectionProperty('Exception', 'previous');
            $prev->setAccessible(true);
            $prev->setValue($wrapper, $exception);

            throw $e;
        }

        $event->setResponse($response);

        $handling = false;
    }
 /**
  * Create a Symfony response for the given exception.
  *
  * @param  \Exception $exception
  * @return Response
  */
 protected function convertExceptionToResponse(Exception $exception)
 {
     if (App::runningInConsole()) {
         // display exception response in console readable form
         $message = get_class($exception) . PHP_EOL;
         $message .= $exception->getMessage() . PHP_EOL . $exception->getTraceAsString();
         $statusCode = FlattenException::create($exception)->getStatusCode();
         return \Illuminate\Http\Response::create($message, $statusCode);
     }
     return parent::convertExceptionToResponse($exception);
 }
 public static function create(\Exception $exception, $statusCode = null, array $headers = array())
 {
     if (!$exception instanceof BaseException) {
         throw new \RuntimeException(sprintf('You should only try to create an instance of "%s" with a "JavierEguiluz\\Bundle\\EasyAdminBundle\\Exception\\BaseException" instance, or subclass. "%s" given.', __CLASS__, get_class($exception)));
     }
     /** @var FlattenException $e */
     $e = parent::create($exception, $statusCode, $headers);
     $e->setStatusCode($exception->getStatusCode());
     $e->setSafeMessage($exception->getSafeMessage());
     return $e;
 }
Exemple #24
0
 public function onException(GetResponseForExceptionEvent $event)
 {
     $request = Request::create($this->error_path);
     $request->attributes->add(array('exception' => FlattenException::create($event->getException())));
     try {
         $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true);
     } catch (\Exception $e) {
         return;
     }
     $event->setResponse($response);
 }
 /**
  * Process exception
  *
  * @param  \Exception $exception
  * @return array
  */
 protected function processException(\Exception $exception)
 {
     $error = ['message' => $exception->getMessage(), 'type' => join('', array_slice(explode('\\', get_class($exception)), -1)), 'code' => $exception->getCode()];
     if ($exception instanceof Exception\InvalidParameterExceptionInterface) {
         $error['parameter'] = $exception->getParameter();
     }
     if ($this->app['debug']) {
         $error['exception'] = FlattenException::create($exception)->toArray();
     }
     return $error;
 }
 public function previewErrorPageAction(Request $request, $code)
 {
     $exception = FlattenException::create(new \Exception('Something has intentionally gone wrong.'), $code);
     /*
      * This Request mimics the parameters set by
      * \Symfony\Component\HttpKernel\EventListener\ExceptionListener::duplicateRequest, with
      * the additional "showException" flag.
      */
     $subRequest = $request->duplicate(null, null, array('_controller' => $this->controller, 'exception' => $exception, 'logger' => null, 'format' => $request->getRequestFormat(), 'showException' => false));
     return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
 }
 public function testFallbackToHtmlIfNoTemplateForRequestedFormat()
 {
     $twig = new \Twig_Environment(new \Twig_Loader_Array(array('TwigBundle:Exception:error.html.twig' => 'html')));
     $request = Request::create('whatever');
     $request->headers->set('X-Php-Ob-Level', 1);
     $request->setRequestFormat('txt');
     $exception = FlattenException::create(new \Exception());
     $controller = new ExceptionController($twig, false);
     $response = $controller->showAction($request, $exception);
     $this->assertEquals('html', $request->getRequestFormat());
 }
 public function testActionWithUncatchableException()
 {
     $serializerException = $this->prophesize(ExceptionInterface::class);
     $serializerException->willExtend(\Exception::class);
     $flattenException = FlattenException::create($serializerException->reveal());
     $serializer = $this->prophesize(SerializerInterface::class);
     $serializer->serialize($flattenException, 'jsonproblem')->willReturn();
     $exceptionAction = new ExceptionAction($serializer->reveal(), ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']]);
     $request = new Request();
     $request->setFormat('jsonproblem', 'application/problem+json');
     $expected = new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, ['Content-Type' => 'application/problem+json; charset=utf-8', 'X-Content-Type-Options' => 'nosniff', 'X-Frame-Options' => 'deny']);
     $this->assertEquals($expected, $exceptionAction($flattenException, $request));
 }
 /**
  * Render an exception into an HTTP response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Exception  $e
  * @return \Illuminate\Http\Response
  */
 public function render($request, Exception $e)
 {
     if ($e instanceof ErrorException) {
         // This makes use of a Symfony error handler to make pretty traces.
         $SymfonyDisplayer = new SymfonyDisplayer(config('app.debug'));
         $FlattenException = FlattenException::create($e);
         $SymfonyCss = $SymfonyDisplayer->getStylesheet($FlattenException);
         $SymfonyHtml = $SymfonyDisplayer->getContent($FlattenException);
         return response()->view('errors.500', ['error' => $e, 'error_css' => $SymfonyCss, 'error_html' => $SymfonyHtml], 500);
     } else {
         return parent::render($request, $e);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function render(\Exception $exception, BlockInterface $block, Response $response = null)
 {
     $response = $response ?: new Response();
     // enforce debug mode or ignore silently
     if (!$this->debug) {
         return $response;
     }
     $flattenException = FlattenException::create($exception);
     $code = $flattenException->getStatusCode();
     $parameters = array('exception' => $flattenException, 'status_code' => $code, 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', 'logger' => false, 'currentContent' => false, 'block' => $block, 'forceStyle' => $this->forceStyle);
     $content = $this->templating->render($this->template, $parameters);
     $response->setContent($content);
     return $response;
 }