Skip to content

Commit 14d46b8

Browse files
[HttpKernel] Add HttpException::fromStatusCode()
1 parent c221681 commit 14d46b8

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add method `isKernelTerminating()` to `ExceptionEvent` that allows to check if an exception was thrown while the kernel is being terminated
8+
* Add `HttpException::fromStatusCode()`
89

910
7.0
1011
---

src/Symfony/Component/HttpKernel/Exception/HttpException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ public function __construct(int $statusCode, string $message = '', \Throwable $p
2929
parent::__construct($message, $code, $previous);
3030
}
3131

32+
public static function fromStatusCode(int $statusCode, string $message = '', \Throwable $previous = null, array $headers = [], int $code = 0): self
33+
{
34+
return match ($statusCode) {
35+
400 => new BadRequestHttpException($message, $previous, $code, $headers),
36+
403 => new AccessDeniedHttpException($message, $previous, $code, $headers),
37+
404 => new NotFoundHttpException($message, $previous, $code, $headers),
38+
406 => new NotAcceptableHttpException($message, $previous, $code, $headers),
39+
409 => new ConflictHttpException($message, $previous, $code, $headers),
40+
410 => new GoneHttpException($message, $previous, $code, $headers),
41+
411 => new LengthRequiredHttpException($message, $previous, $code, $headers),
42+
412 => new PreconditionFailedHttpException($message, $previous, $code, $headers),
43+
423 => new LockedHttpException($message, $previous, $code, $headers),
44+
415 => new UnsupportedMediaTypeHttpException($message, $previous, $code, $headers),
45+
422 => new UnprocessableEntityHttpException($message, $previous, $code, $headers),
46+
428 => new PreconditionRequiredHttpException($message, $previous, $code, $headers),
47+
429 => new TooManyRequestsHttpException(null, $message, $previous, $code, $headers),
48+
503 => new ServiceUnavailableHttpException(null, $message, $previous, $code, $headers),
49+
default => new static($statusCode, $message, $previous, $headers, $code),
50+
};
51+
}
52+
3253
public function getStatusCode(): int
3354
{
3455
return $this->statusCode;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy