If the error is an exception with a code between 400 and 599, returns
the exception code.
Otherwise, retrieves the code from the response; if not present, or
less than 400 or greater than 599, returns 500; otherwise, returns it.
public static getStatusCode ( mixed $error, Psr\Http\Message\ResponseInterface $response ) : integer | ||
$error | mixed | |
$response | Psr\Http\Message\ResponseInterface | |
return | integer |
/** * Create/update the response representing the error. * * @param Throwable|Exception $e * @param ServerRequestInterface $request * @param ResponseInterface $response * @return ResponseInterface */ public function __invoke($e, ServerRequestInterface $request, ResponseInterface $response) { $response = $response->withStatus(Utils::getStatusCode($e, $response)); $body = $response->getBody(); if ($this->isDevelopmentMode) { $escaper = new Escaper(); $body->write($escaper->escapeHtml((string) $e)); return $response; } $body->write($response->getReasonPhrase() ?: 'Unknown Error'); return $response; }
/** * Handles an error. Similar to Stratigilities except we're rendering a template. * * @param mixed $error * @param Http\Request $request * @param Http\Response $response * @return Http\Response */ private function handleError($error, Http\Request $request, Http\Response $response) { $response = $response->withStatus(Utils::getStatusCode($error, $response)); $vars = ['request' => $request, 'response' => $response]; if ($error instanceof Exception) { $vars['exception'] = $error; $vars['message'] = $error->getMessage(); } else { $vars['message'] = $error; } return $response->render('error/error', $vars); }
/** * Handle an error condition * * Use the $error to create details for the response. * * @param mixed $error * @param RequestInterface $request Request instance. * @param ResponseInterface $response Response instance. * @return Http\Response */ private function handleError($error, RequestInterface $request, ResponseInterface $response) { $response = $response->withStatus(Utils::getStatusCode($error, $response)); $message = $response->getReasonPhrase() ?: 'Unknown Error'; if (!isset($this->options['env']) || $this->options['env'] !== 'production') { $message = $this->createDevelopmentErrorMessage($error); } $response = $this->completeResponse($response, $message); $this->triggerError($error, $request, $response); return $response; }
/** * Handle an error response. * * Marshals the response status from the error. * * If the error is not an exception, it then proxies to handleError(); * otherwise, it proxies to handleException(). * * @param mixed $error * @param Request $request * @param Response $response * @return Response */ private function handleErrorResponse($error, Request $request, Response $response) { $response = $response->withStatus(Utils::getStatusCode($error, $response)); if (!$error instanceof \Exception) { return $this->handleError($error, $request, $response); } return $this->handleException($error, $request, $response); }