/** * Write ClientException content into response. * * @param Request $request * @param Response $response * @param ClientException $exception * @return Request */ public function writeException(Request $request, Response $response, ClientException $exception) { //Has to contain valid http code $response = $response->withStatus($exception->getCode()); if ($request->getHeaderLine('Accept') == 'application/json') { //Json got requested return $this->writeJson($response, ['status' => $exception->getCode()]); } if (!$this->config->hasView($exception->getCode())) { //We don't or can't render http error view return $response; } $errorPage = $this->views->render($this->config->errorView($exception->getCode()), ['httpConfig' => $this->config, 'request' => $request]); $response->getBody()->write($errorPage); return $response; }
/** * @param ClientException $exception * @param ViewInterface|null $view */ public function __construct(ClientException $exception, ViewInterface $view = null) { $this->exception = $exception; $headers = []; if (!empty($this->view = $view)) { $headers['Content-Type'] = 'text/html'; } //We will write to memory on demand, response can be freely modified until body is touched parent::__construct('php://memory', $exception->getCode(), $headers); }
/** * Add error to error log. * * @param Request $request * @param ClientException $exception */ private function logError(Request $request, ClientException $exception) { $remoteAddress = '-undefined-'; if (!empty($request->getServerParams()['REMOTE_ADDR'])) { $remoteAddress = $request->getServerParams()['REMOTE_ADDR']; } $this->logger()->error(\Spiral\interpolate(static::LOG_FORMAT, ['scheme' => $request->getUri()->getScheme(), 'host' => $request->getUri()->getHost(), 'path' => $request->getUri()->getPath(), 'code' => $exception->getCode(), 'message' => $exception->getMessage() ?: '-not specified-', 'remote' => $remoteAddress])); }
/** * Add error to http log. * * @param ClientException $exception * @param ServerRequestInterface $request */ private function logError(ClientException $exception, ServerRequestInterface $request) { $remoteAddress = '-undefined-'; if (!empty($request->getServerParams()['REMOTE_ADDR'])) { $remoteAddress = $request->getServerParams()['REMOTE_ADDR']; } $this->logger()->warning("{scheme}://{host}{path} caused the error {code} ({message}) by client {remote}.", ['scheme' => $request->getUri()->getScheme(), 'host' => $request->getUri()->getHost(), 'path' => $request->getUri()->getPath(), 'code' => $exception->getCode(), 'message' => $exception->getMessage() ?: '-not specified-', 'remote' => $remoteAddress]); }