/** * Display the given exception to the user. * * @param \Exception $exception */ public function display(Exception $exception) { if (!$this->runningInConsole and !headers_sent()) { header('HTTP/1.1 500 Internal Server Error'); } $this->whoops->handleException($exception); }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { try { return $next($request, $response); } catch (Exception $e) { if ($this->logger) { $this->logger->error($e->getMessage(), ['exception' => $e]); } $type = $this->type($request); $response = $response->withHeader('Content-Type', $type); try { if (method_exists($e, 'getHttpStatus')) { $code = $e->getHttpStatus(); } else { $code = $e->getCode(); } $response = $response->withStatus($code); } catch (InvalidArgumentException $_) { // Exception did not contain a valid code $response = $response->withStatus(500); } if ($e instanceof HttpException) { $response = $e->withResponse($response); } $handler = $this->handler($type); $this->whoops->pushHandler($handler); $body = $this->whoops->handleException($e); $response->getBody()->write($body); $this->whoops->popHandler(); return $response; } }
/** * Handle an exception. * * Calls on prepareWhoopsHandler() to inject additional data tables into * the generated payload, and then injects the response with the result * of whoops handling the exception. * * @param \Exception $exception * @param Request $request * @param Response $response * @return Response */ protected function handleException(\Exception $exception, Request $request, Response $response) { $this->prepareWhoopsHandler($request); $this->whoops->pushHandler($this->whoopsHandler); $response->getBody()->write($this->whoops->handleException($exception)); return $response; }
/** * Render an exception using Whoops. * * @param \Exception $e * @return \Illuminate\Http\Response */ protected function renderExceptionWithWhoops(Exception $e) { $whoops = new RunWhoops(); $whoops->pushHandler(new PrettyPageHandler()); if ($e instanceof HttpException) { return new Response($whoops->handleException($e), $e->getStatusCode(), $e->getHeaders()); } return new Response($whoops->handleException($e)); }
/** * Handle http exceptions * * @param UnknownHttpExceptionEvent $event */ public function onException(UnknownHttpExceptionEvent $event) { if (!$this->environment->equals(Environment::DEVELOPMENT)) { return; } $event->stopPropagation(); $this->whoops->writeToOutput(false); $this->whoops->allowQuit(false); $content = $this->whoops->handleException($event->getHttpException()); $event->getResponse()->setContent($content); }
/** * Handle errors thrown in the application. * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { $hasUser = $this->session->isStarted() && $this->session->has('authentication'); if (!$hasUser && !$this->showWhileLoggedOff) { return; } $exception = $event->getException(); ob_start(); $this->whoops->handleException($exception); $response = ob_get_clean(); $code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR; $event->setResponse(new Response($response, $code)); }
/** * 自定义异常处理 * * @param mixed $e 异常对象 */ public function appException(&$e) { if (Cml::$debug) { $run = new Run(); $run->pushHandler(Request::isCli() ? new PlainTextHandler() : new PrettyPageHandler()); $run->handleException($e); } else { $error = []; $error['message'] = $e->getMessage(); $trace = $e->getTrace(); $error['files'][0] = $trace[0]; if (substr($e->getFile(), -20) !== '\\Tools\\functions.php' || $e->getLine() !== 90) { array_unshift($error['files'], ['file' => $e->getFile(), 'line' => $e->getLine(), 'type' => 'throw']); } //正式环境 只显示‘系统错误’并将错误信息记录到日志 Log::emergency($error['message'], [$error['files'][0]]); $error = []; $error['message'] = Lang::get('_CML_ERROR_'); if (Request::isCli()) { \Cml\pd($error); } else { header('HTTP/1.1 500 Internal Server Error'); View::getEngine('html')->reset()->assign('error', $error); Cml::showSystemTemplate(Config::get('html_exception')); } } exit; }
/** * Handle an exception. * * Calls on prepareWhoopsHandler() to inject additional data tables into * the generated payload, and then injects the response with the result * of whoops handling the exception. * * @param \Throwable $exception * @param Request $request * @param Response $response * @return Response */ protected function handleException($exception, Request $request, Response $response) { // Push the whoops handler if any if ($this->whoopsHandler !== null) { $this->whoops->pushHandler($this->whoopsHandler); } // Walk through all handlers foreach ($this->whoops->getHandlers() as $handler) { // Add fancy data for the PrettyPageHandler if ($handler instanceof PrettyPageHandler) { $this->prepareWhoopsHandler($request, $handler); } } $response->getBody()->write($this->whoops->handleException($exception)); return $response; }
/** * @param \Exception $e * @return \Illuminate\Http\Response */ protected function debugRender(Exception $e) { $whoops = new WhoopsRun(); $whoops->pushHandler(new WhoopsPrettyPageHandler()); $response = $this->convertExceptionToResponse($e); return new HttpResponse($whoops->handleException($e), $response->getStatusCode(), $response->headers); }
/** * @param \Exception|HttpException $exception * @throws \InvalidArgumentException * @throws \RuntimeException */ public function handleException(\Exception $exception) { $this->app['Logger']->error($exception->getMessage(), ['class' => get_class($exception), 'file' => $exception->getFile(), 'line' => $exception->getLine()]); $code = 500; if ($exception instanceof HttpException) { $code = $exception->getStatusCode(); } if ($this->app['Config']->offsetExists('debug') && $this->app['Config']->offsetGet('debug') === true) { ob_start(); $this->whoops->handleException($exception); $html = ob_get_clean(); $this->writeToOutput($html, $code, $this->contentType); } else { $this->writeToOutput('whoops something went wrong'); } }
/** * Handle errors thrown in the application. * * Note: * - We don't want to show Whoops! screens for requests that trigger a 404. * - Our priority is set just above Symfony's, as we are giving up and * displaying the error to the user, so that should be a low priority * compared to error handlers that do something else. * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { // We (generally) do not want to show Whoops! screens when the user isn't logged on. $hasUser = $this->session->isStarted() && $this->session->has('authentication'); if (!$hasUser && !$this->showWhileLoggedOff) { return; } // Register Whoops as an error handler $this->whoops->register(); $exception = $event->getException(); ob_start(); $this->whoops->handleException($exception); $response = ob_get_clean(); $code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR; $event->setResponse(new Response($response, $code)); }
/** * Convert the view to a rendered string. * * @return string A string of HTML, e.g. to be `echo`'d. */ public function __toString() { static $run, $handler; if (!isset($run, $handler)) { $run = new Run(); $handler = new PrettyPageHandler(); $handler->handleUnconditionally(true); $run->pushHandler($handler); $run->allowQuit(false); $run->writeToOutput(false); $run->register(); } // __toString cannot throw an exception, so we need to handle those // manually to prevent PHP from barfing: try { return $this->render(); } catch (Exception $e) { self::$didWhoops = true; if (!static::$swallowError) { return $run->handleException($e); } else { return static::$swallowError; } } }
/** * @param Exception $e * * @return string */ private function debugException(Exception $e) { $whoops = new Run(); $whoops->pushHandler(new PrettyPageHandler()); $content = $whoops->handleException($e); return $content; }
/** * Create a Whoops response for the given exception * * @param Exception $e * @return \Illuminate\Http\Response */ protected function convertExceptionToWhoops(Exception $e) { $whoops = new Whoops(); $whoops->pushHandler($this->getWhoopsHandler()); $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500; $headers = method_exists($e, 'getHeaders') ? $e->getHeaders() : []; return response()->make($whoops->handleException($e), $statusCode, $headers); }
protected function handleWithWhoops(Exception $exception) { $whoops = new Run(); $whoops->allowQuit(false); $whoops->writeToOutput(false); $whoops->pushHandler(new PrettyPageHandler()); return $whoops->handleException($exception); }
/** * Render an exception using Whoops. * * @param \Exception $e * @return \Illuminate\Http\Response */ protected function renderExceptionWithWhoops(Exception $e) { $whoops = new Whoops\Run(); $whoops->allowQuit(false); $whoops->writeToOutput(false); $whoops->pushHandler(new Whoops\Handler\PrettyPageHandler()); return response($whoops->handleException($e), 500); }
/** * @param \Illuminate\Http\Request $request * @param \Exception $e * * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function renderExceptionWithWhoops($request, Exception $e) { $whoops = new Run(); if ($request->ajax() || $request->wantsJson()) { $whoops->pushHandler(new JsonResponseHandler()); } else { $whoops->pushHandler(new PrettyPageHandler()); } return response($whoops->handleException($e), $e->getStatusCode(), $e->getHeaders()); }
/** * Create a Symfony response for the given exception. * * @param \Exception $e * @return \Symfony\Component\HttpFoundation\Response */ protected function convertExceptionToResponse(Exception $e) { if (config('app.debug')) { $whoops = new Whoops(); $whoops->pushHandler(new PrettyPage()); $whoops->register(); return SymfonyResponse::create($whoops->handleException($e), $e->getStatusCode(), $e->getHeaders()); } return parent::convertExceptionToResponse($e); }
/** * Render an exception using Whoops. * * @param \Exception $e The Exception object * @param string $json Whether to use the JsonResponseHandler instead * of the PrettyPageHandler. * @return \Illuminate\Http\Response */ protected function renderExceptionWithWhoops(Exception $e, $json = false) { $whoops = new Whoops(); if ($json) { $whoops->pushHandler(new JsonResponseHandler()); } else { $whoops->pushHandler(new PrettyPageHandler()); } return new Response($whoops->handleException($e), $e->getStatusCode(), $e->getHeaders()); }
public function runDefaultHandler($exception) { if ($this->needPrettyHandler()) { $run = new Run(); $run->pushHandler(new PrettyPageHandler()); $run->handleException($exception); } else { $this->handleError(OuzoExceptionData::forException(500, $exception)); } }
/** * Render an exception using Whoops. * * @param $request * @param \Exception $e * @return Response */ protected function renderExceptionWithWhoops($request, Exception $e) { $whoops = new Whoops\Run(); if ($request->ajax()) { $whoops->pushHandler(new Whoops\Handler\JsonResponseHandler()); } else { $whoops->pushHandler(new Whoops\Handler\PrettyPageHandler()); } return new Response($whoops->handleException($e), $e->getStatusCode(), $e->getHeaders()); }
/** * Handle exception (uncatched). * * @param \Throwable $exception */ public function handleException($exception) { if (!$exception instanceof \Exception) { $exception = new \Exception("Can't handle exception thrown!"); } // Log \Logger::error((string) $exception); // Whoops if ($this->whoops !== false) { $this->whoops->handleException($exception); } }
/** * @param Request $request * @param Exception $exception * @return Response * @throws \InvalidArgumentException */ protected function renderExceptionWithWhoops(Request $request, Exception $exception) { $whoops = new Whoops(); if ($request->isJson() || $request->wantsJson()) { $whoops->pushHandler(new JsonResponseHandler()); } else { $whoops->pushHandler(new PrettyPageHandler()); } $code = $exception instanceof HttpException ? $exception->getStatusCode() : 500; $headers = $exception instanceof HttpException ? $exception->getHeaders() : []; return new Response($whoops->handleException($exception), $code, $headers); }
/** * Render an exception into an HTTP response. * * When debugging is enabled, we make the error pretty using Whoops * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { if ($this->isHttpException($e)) { return $this->renderHttpException($e); } if (config('app.debug')) { $whoops = new Run(); $whoops->pushHandler($request->ajax() ? new JsonResponseHandler() : new PrettyPageHandler()); $whoops->allowQuit(false); $whoops->writeToOutput(false); return response($whoops->handleException($e), $whoops->sendHttpCode()); } return parent::render($request, $e); }
/** * Render an exception using Whoops. * * @param Exception $e * * @return Response */ protected function renderExceptionWithWhoops(Exception $e) { $statusCode = 500; if (method_exists($e, 'getStatusCode')) { $statusCode = $e->getStatusCode(); } $headers = []; if (method_exists($e, 'getHeaders')) { $headers = $e->getHeaders(); } $whoops = new Run(); $whoops->pushHandler(new PrettyPageHandler()); return new Response($whoops->handleException($e), $statusCode, $headers); }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { try { return $next($request, $response); } catch (Exception $e) { $type = $this->type($request); $response = $response->withHeader('Content-Type', $type); try { $response = $response->withStatus($e->getCode()); } catch (InvalidArgumentException $_) { // Exception did not contain a valid code $response = $response->withStatus(500); } if ($e instanceof HttpException) { $response = $e->withResponse($response); } $handler = $this->handler($type); $this->whoops->pushHandler($handler); $body = $this->whoops->handleException($e); $response->getBody()->write($body); $this->whoops->popHandler(); return $response; } }
/** * Handles an exception, ultimately generating a Whoops error page. * * @param \Exception $exception */ public function handleException(\Exception $exception) { // If there are registered patterns, only handle errors if error matches one of the patterns. if ($this->registeredPatterns) { foreach ($this->registeredPatterns as $entry) { $pathMatches = (bool) preg_match($entry["pattern"], $exception->getFile()); if ($pathMatches) { return parent::handleException($exception); } } } // Propagate error to the next handler. if ($this->oldExceptionHandler) { call_user_func_array($this->oldExceptionHandler, [&$exception]); } }
/** * 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 ModelNotFoundException) { $e = new NotFoundHttpException($e->getMessage(), $e); } if (config('app.debug')) { $whoops = new Whoops(); if ($request->ajax() || str_contains($request->header('Accept'), 'json')) { $whoops->pushHandler(new JsonResponseHandler()); } else { $whoops->pushHandler(new PrettyPageHandler()); } return response($whoops->handleException($e), $e->getStatusCode(), $e->getHeaders()); } return parent::render($request, $e); }
/** * Whoops handle exceptions * @param MvcEvent $e */ public function prepareException(MvcEvent $e) { if ($e->getRequest() instanceof Request) { $error = $e->getError(); if (!empty($error) && !$e->getResult() instanceof Response) { switch ($error) { case Application::ERROR_CONTROLLER_NOT_FOUND: case Application::ERROR_CONTROLLER_INVALID: case Application::ERROR_ROUTER_NO_MATCH: // Specifically not handling these return; case Application::ERROR_EXCEPTION: default: /** @var Exception $exception */ $exception = $e->getParam('exception'); // Filter exceptions that we must except foreach ($this->whoopsConfig['blacklist'] as $except) { if ($exception instanceof $except) { return; } } if ($this->whoopsConfig['handler']['options_type'] === 'prettyPage') { $response = $e->getResponse(); if (!$response || $response->getStatusCode() === 200) { header('HTTP/1.0 500 Internal Server Error', true, 500); } ob_clean(); } $this->run->handleException($e->getParam('exception')); break; } } } }
/** * Whoops handle exceptions * @param MvcEvent $e */ public function prepareException(MvcEvent $e) { $error = $e->getError(); if (!empty($error) && !$e->getResult() instanceof Response) { switch ($error) { case Application::ERROR_CONTROLLER_NOT_FOUND: case Application::ERROR_CONTROLLER_INVALID: case Application::ERROR_ROUTER_NO_MATCH: // Specifically not handling these return; case Application::ERROR_EXCEPTION: default: if (in_array(get_class($e->getParam('exception')), $this->noCatchExceptions)) { // No catch this exception return; } $response = $e->getResponse(); if (!$response || $response->getStatusCode() === 200) { header('HTTP/1.0 500 Internal Server Error', true, 500); } ob_clean(); $this->run->handleException($e->getParam('exception')); break; } } }