本文整理汇总了PHP中Exception::getStatusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Exception::getStatusCode方法的具体用法?PHP Exception::getStatusCode怎么用?PHP Exception::getStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Exception
的用法示例。
在下文中一共展示了Exception::getStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getExceptionStatusCode
/**
* @return int
*/
protected function getExceptionStatusCode()
{
if ($this->exception instanceof HttpException) {
return $this->exception->getStatusCode();
} elseif (array_key_exists($this->exception->getCode(), Response::$statusTexts) && $this->exception->getCode() >= Response::HTTP_BAD_REQUEST) {
return $this->exception->getCode();
}
return Response::HTTP_INTERNAL_SERVER_ERROR;
}
示例2: getHttpException
/**
* {@inheritdoc}
*/
public function getHttpException()
{
$code = 500;
$message = '';
if (!$this->isProd()) {
$message = $this->currentException->getMessage() . '<pre>' . $this->currentException->getTraceAsString() . '</pre>';
}
if ($this->currentException instanceof HttpException) {
$code = $this->currentException->getStatusCode();
}
return new HttpException($code, $message, $this->currentException);
}
示例3: getExceptionStatusCode
/**
* @return int
*/
protected function getExceptionStatusCode()
{
if ($this->exception instanceof HttpException) {
return $this->exception->getStatusCode();
}
if ($this->exception instanceof AbstractValidationException) {
return Response::HTTP_BAD_REQUEST;
}
if ($this->isValidHttpStatusCode($this->exception->getCode())) {
return $this->exception->getCode();
}
return Response::HTTP_INTERNAL_SERVER_ERROR;
}
示例4: getStatusCode
/**
* @return mixed
*/
public function getStatusCode()
{
if (isset($this->exception)) {
if ($this->exception instanceof HttpException) {
return $this->exception->getStatusCode();
} elseif (array_key_exists($this->exception->getCode(), Response::$statusTexts) && $this->exception->getCode() >= 400) {
return $this->exception->getCode();
} else {
return Response::HTTP_INTERNAL_SERVER_ERROR;
}
}
return $this->statusCode;
}
示例5: error
/**
* Return an error into an HTTP or JSON data array.
*
* @param string $title
* @return array
*/
public function error($title = null)
{
if ($title == null) {
$title = $this->debug ? 'The application could not run because of the following error:' : 'A website error has occurred. Sorry for the temporary inconvenience.';
}
$type = $this->request->getHeader('Content-Type');
$mesg = $this->exception->getMessage();
$file = $this->exception->getFile();
$line = $this->exception->getLine();
$code = $this->exception->getCode();
$statusCode = method_exists($this->exception, 'getStatusCode') ? $this->exception->getStatusCode() : null;
// Check status code is null
if ($statusCode == null) {
$statusCode = $code >= 100 && $code <= 500 ? $code : 400;
}
$this->response->withStatus($statusCode);
// Check logger exist
if ($this->logger !== null) {
// Send error to log
$this->logger->addError($this->exception->getMessage());
}
$this->isJson = isset($type[0]) && $type[0] == 'application/json';
// Check content-type is application/json
if ($this->isJson) {
// Define content-type to json
$this->response->withHeader('Content-Type', 'application/json');
$error = ['status' => 'error', 'status_code' => $statusCode, 'error' => $title, 'details' => []];
// Check debug
if ($this->debug) {
$error['details'] = ['message' => $mesg, 'file' => $file, 'line' => $line, 'code' => $code];
}
return $error;
}
// Define content-type to html
$this->response->withHeader('Content-Type', 'text/html');
$message = sprintf('<span>%s</span>', htmlentities($mesg));
$error = ['type' => get_class($this->exception), $error['status_code'] = $statusCode, 'message' => $message];
// Check debug
if ($this->debug) {
$trace = $this->exception->getTraceAsString();
$trace = sprintf('<pre>%s</pre>', htmlentities($trace));
$error['file'] = $file;
$error['line'] = $line;
$error['code'] = $code;
$error['trace'] = $trace;
}
$error['debug'] = $this->debug;
$error['title'] = $title;
return $error;
}
示例6: createResponse
/**
* Creates the error Response associated with the given Exception.
*
* @param \Exception $exception An \Exception instance
*
* @return Response A Response instance
*/
public function createResponse(\Exception $exception)
{
$content = '';
$title = '';
try {
$code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
$exception = FlattenException::create($exception);
switch($code) {
case 404:
$title = 'Sorry, the page you are looking for could not be found.';
break;
default:
$title = 'Whoops, looks like something went wrong.';
}
if ($this->debug) {
$content = $this->getContent($exception);
}
} catch (\Exception $e) {
// something nasty happened and we cannot throw an exception here anymore
if ($this->debug) {
$title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($exception), $exception->getMessage());
} else {
$title = 'Whoops, looks like something went wrong.';
}
}
return new Response($this->decorate($content, $title), $code);
}
示例7: create
public static function create(\Exception $exception, $statusCode = null, array $headers = array())
{
$e = new static();
$e->setMessage($exception->getMessage());
$e->setCode($exception->getCode());
if ($exception instanceof HttpExceptionInterface) {
$statusCode = $exception->getStatusCode();
$headers = array_merge($headers, $exception->getHeaders());
} elseif ($exception instanceof RequestExceptionInterface) {
$statusCode = 400;
}
if (null === $statusCode) {
$statusCode = 500;
}
$e->setStatusCode($statusCode);
$e->setHeaders($headers);
$e->setTraceFromException($exception);
$e->setClass(get_class($exception));
$e->setFile($exception->getFile());
$e->setLine($exception->getLine());
$previous = $exception->getPrevious();
if ($previous instanceof \Exception) {
$e->setPrevious(static::create($previous));
} elseif ($previous instanceof \Throwable) {
$e->setPrevious(static::create(new FatalThrowableError($previous)));
}
return $e;
}
示例8: create
public static function create(\Exception $exception, $statusCode = null, array $headers = array())
{
$e = new static();
$e->setMessage($exception->getMessage());
$e->setCode($exception->getCode());
if ($exception instanceof HttpExceptionInterface) {
$statusCode = $exception->getStatusCode();
$headers = array_merge($headers, $exception->getHeaders());
}
if (null === $statusCode) {
$statusCode = 500;
}
$e->setStatusCode($statusCode);
$e->setHeaders($headers);
$e->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
$e->setClass(get_class($exception));
$e->setFile($exception->getFile());
$e->setLine($exception->getLine());
if ($exception->getPrevious()) {
$e->setPrevious(static::create($exception->getPrevious()));
}
return $e;
}
示例9: echoExceptionWeb
/**
* Echoes an exception for the web.
*
* @param \Exception $exception The exception
* @return void
*/
protected function echoExceptionWeb(\Exception $exception)
{
if ($exception instanceof Exception) {
$statusCode = 400;
$json = ['status' => 'invalid_request', 'reason' => $exception->getMessage()];
} elseif ($exception instanceof \TYPO3\Flow\Security\Exception) {
$statusCode = 403;
$json = ['status' => 'unauthorized', 'reason' => $exception->getMessage()];
} else {
$statusCode = 500;
if ($exception instanceof FlowException) {
$statusCode = $exception->getStatusCode();
}
$json = ['status' => 'error', 'reason' => $exception->getMessage(), 'errorClass' => get_class($exception)];
}
if ($exception->getPrevious() !== NULL) {
$json['previous'] = $exception->getPrevious()->getMessage();
}
$json['stacktrace'] = explode("\n", $exception->getTraceAsString());
$statusMessage = Response::getStatusMessageByCode($statusCode);
if (!headers_sent()) {
header(sprintf('HTTP/1.1 %s %s', $statusCode, $statusMessage));
header('Content-Type: application/json');
}
print json_encode($json);
}
示例10: renderControllerException
/**
* Render an exception using ErrorController.
*
* @param \Exception $e
*
* @return \Illuminate\Http\Response
*/
protected function renderControllerException(\Exception $e)
{
$code = 500;
if ($e instanceof HttpResponseException) {
$code = $e->getStatusCode();
} else {
if ($e->getCode() > 0) {
$code = $e->getCode();
}
}
try {
/** @var ErrorController $controller */
$controller = app()->make(ErrorController::class);
if (method_exists($controller, 'error' . $code)) {
$action = 'error' . $code;
} else {
$action = 'errorDefault';
}
$response = $controller->callAction($action, [$e]);
if (!$response instanceof Response) {
$response = new Response($response);
}
return $this->toIlluminateResponse($response, $e);
} catch (\Exception $ex) {
return $this->toIlluminateResponse($this->convertExceptionToResponse($ex), $ex);
}
}
示例11: format
/**
* @param \Exception $exception
* @return \Illuminate\Http\JsonResponse
*/
public function format($exception)
{
// Define the response
$result = ['errors' => trans('messages.sorry')];
// Default response of 400
$statusCode = 400;
$addDebugData = $this->isDebugEnabled();
switch (true) {
case $exception instanceof HttpException:
$statusCode = $exception->getStatusCode();
$result['errors'] = $exception->getMessage();
break;
case $exception instanceof ValidationException:
$result['errors'] = $exception->errors();
$addDebugData = false;
break;
}
// Prepare response
$response = ['success' => false, 'result' => $result, 'meta' => ['version' => config('app.version.api'), 'request' => \Request::method() . ' ' . \Request::url(), 'debug' => $this->isDebugEnabled()]];
// If the app is in debug mode && not Validation exception
if ($addDebugData) {
$response['debug'] = ['exception' => get_class($exception), 'message' => $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTrace()];
}
// Return a JSON response with the response array and status code
return response()->json($response, $statusCode);
}
示例12: testInstantiation
public function testInstantiation()
{
$exception = new Exception('test message', 9, 201, [1, 2, 3]);
$this->assertEquals('test message', $exception->getMessage());
$this->assertEquals(9, $exception->getCode());
$this->assertEquals(201, $exception->getStatusCode());
$this->assertEquals([1, 2, 3], $exception->getDetails());
}
示例13: errorHandler
/**
* Handle errors thrown in the application.
*
* @todo: according to the docs of flint it should handle exceptions, but for some reason it doesn't seem to work.
*
* @param \Exception $exception
* @return Response
*/
public function errorHandler(\Exception $exception)
{
if ($exception instanceof HttpException && $exception->getStatusCode() == 404) {
$template = 'error.404.html.twig';
} else {
$template = 'error.html.twig';
}
return $this['twig']->render($template);
}
示例14: logException
/**
* Logs an exception.
*
* @param \Exception $exception The \Exception instance
* @param string $message The error message to log
*/
protected function logException(\Exception $exception, $message)
{
if (null !== $this->logger) {
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->critical($message, array('exception' => $exception));
} else {
$this->logger->error($message, array('exception' => $exception));
}
}
}
示例15: extractStatus
private static function extractStatus(\Exception $exception)
{
if ($exception instanceof HttpExceptionInterface) {
return $exception->getStatusCode();
}
if ($exception instanceof \Symfony\Component\Security\Core\Exception\AccessDeniedException) {
return 403;
}
return 500;
}