Skip to content

Commit dd4d778

Browse files
committed
Factory and a whole lot of exceptions
1 parent 01c3fc6 commit dd4d778

File tree

83 files changed

+1345
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1345
-0
lines changed

src/AbstractException.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
abstract class AbstractException extends Exception
9+
{
10+
/**
11+
* @var ResponseInterface
12+
*/
13+
private $response;
14+
15+
/**
16+
* @param ResponseInterface $response
17+
* @return AbstractException
18+
*/
19+
protected function setResponse(ResponseInterface $response): AbstractException
20+
{
21+
$this->response = $response;
22+
return $this;
23+
}
24+
25+
/**
26+
* @return ResponseInterface
27+
*/
28+
public function getResponse(): ResponseInterface
29+
{
30+
return $this->response;
31+
}
32+
}

src/AcceptedException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class AcceptedException extends AbstractException
9+
{
10+
public static function create(ResponseInterface $response, Exception $exception = null)
11+
{
12+
return (new self('Accepted', 202, $exception))->setResponse($response);
13+
}
14+
}

src/AlreadyReportedException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class AlreadyReportedException extends AbstractException
9+
{
10+
public static function create(ResponseInterface $response, Exception $exception = null)
11+
{
12+
return (new self('Already Reported', 208, $exception))->setResponse($response);
13+
}
14+
}

src/BadGatewayException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class BadGatewayException extends AbstractException
9+
{
10+
public static function create(ResponseInterface $response, Exception $exception = null)
11+
{
12+
return (new self('Bad Gateway', 502, $exception))->setResponse($response);
13+
}
14+
}

src/BadRequestException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class BadRequestException extends AbstractException
9+
{
10+
public static function create(ResponseInterface $response, Exception $exception = null)
11+
{
12+
return (new self('Bad Request', 400, $exception))->setResponse($response);
13+
}
14+
}

src/ConflictException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class ConflictException extends AbstractException
9+
{
10+
public static function create(ResponseInterface $response, Exception $exception = null)
11+
{
12+
return (new self('Conflict', 409, $exception))->setResponse($response);
13+
}
14+
}

src/ContinueException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class ContinueException extends AbstractException
9+
{
10+
public static function create(ResponseInterface $response, Exception $exception = null)
11+
{
12+
return (new self('Continue', 100, $exception))->setResponse($response);
13+
}
14+
}

src/CreatedException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class CreatedException extends AbstractException
9+
{
10+
public static function create(ResponseInterface $response, Exception $exception = null)
11+
{
12+
return (new self('Created', 201, $exception))->setResponse($response);
13+
}
14+
}

src/ExceptionFactory.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use ApiClients\Tools\Psr7\HttpStatusExceptions\Unofficial;
6+
use Exception as CoreException;
7+
use Psr\Http\Message\ResponseInterface;
8+
use function React\Promise\reject;
9+
10+
final class ExceptionFactory
11+
{
12+
const STATUS_CODE_EXCEPTION_MAP = [
13+
/**
14+
* 1xx codes
15+
*/
16+
100 => ContinueException::class,
17+
101 => SwitchingProtocolsException::class,
18+
102 => ProcessingException::class,
19+
20+
/**
21+
* 2xx codes
22+
*/
23+
200 => OKException::class,
24+
201 => CreatedException::class,
25+
202 => AcceptedException::class,
26+
203 => NonAuthoritativeInformationException::class,
27+
204 => NoContentException::class,
28+
205 => ResetContentException::class,
29+
206 => PartialContentException::class,
30+
207 => MultiStatusException::class,
31+
208 => AlreadyReportedException::class,
32+
226 => IMUsedException::class,
33+
34+
/**
35+
* 3xx codes
36+
*/
37+
300 => MultiChoicesException::class,
38+
301 => MovedPermanentlyException::class,
39+
302 => FoundException::class,
40+
303 => SeeOtherException::class,
41+
304 => NotModifiedException::class,
42+
305 => UseProxyException::class,
43+
306 => SwitchProxyException::class,
44+
307 => TemporaryRedirectException::class,
45+
308 => PermanentRedirectException::class,
46+
47+
/**
48+
* 4xx codes
49+
*/
50+
400 => BadRequestException::class,
51+
401 => UnauthorizedException::class,
52+
402 => PaymentRequiredException::class,
53+
403 => ForbiddenException::class,
54+
404 => NotFoundException::class,
55+
405 => MethodNotAllowedException::class,
56+
406 => NotAcceptableException::class,
57+
407 => ProxyAuthenticationRequiredException::class,
58+
408 => RequestTimeoutException::class,
59+
409 => ConflictException::class,
60+
410 => GoneException::class,
61+
411 => LengthRequiredException::class,
62+
412 => PreconditionFailedException::class,
63+
413 => PayloadTooLargeException::class,
64+
414 => URITooLongException::class,
65+
415 => UnsupportedMediaTypeException::class,
66+
416 => RangeNotSatisfiableException::class,
67+
417 => ExpectationFailedException::class,
68+
418 => ImATeapotException::class,
69+
421 => MisdirectedRequestException::class,
70+
422 => UnprocessableEntityException::class,
71+
423 => LockedException::class,
72+
424 => FailedDependencyException::class,
73+
426 => UpgradeRequiredException::class,
74+
428 => PreconditionRequiredException::class,
75+
429 => TooManyRequestsException::class,
76+
431 => RequestHeaderFieldsTooLargeException::class,
77+
451 => UnavailableForLegalReasonsException::class,
78+
79+
/**
80+
* 5xx codes
81+
*/
82+
500 => InternalServerErrorException::class,
83+
501 => NotImplementedException::class,
84+
502 => BadGatewayException::class,
85+
503 => ServiceUnavailableException::class,
86+
504 => GatewayTimeoutException::class,
87+
505 => HTTPVersionNotSupportedException::class,
88+
506 => VariantAlsoNegotiatesException::class,
89+
507 => InsufficientStorageException::class,
90+
508 => LoopDetectedException::class,
91+
510 => NotExtendedException::class,
92+
511 => NetworkAuthenticationRequiredException::class,
93+
94+
/**
95+
* IIS (Unofficial)
96+
*
97+
* Excluding the 451 as it conflicts with the official 451 code.
98+
*/
99+
440 => Unofficial\IIS\LoginTimeoutException::class,
100+
449 => Unofficial\IIS\RetryWithException::class,
101+
102+
/**
103+
* Cloudflare (Unofficial)
104+
*/
105+
520 => Unofficial\Cloudflare\UnknownErrorException::class,
106+
521 => Unofficial\Cloudflare\WebServerIsDownException::class,
107+
522 => Unofficial\Cloudflare\ConnectionTimedOutException::class,
108+
523 => Unofficial\Cloudflare\OriginIsUnreachableException::class,
109+
524 => Unofficial\Cloudflare\ATimeoutOccurredException::class,
110+
525 => Unofficial\Cloudflare\SSLHandshakeFailedException::class,
111+
526 => Unofficial\Cloudflare\InvalidSSLCertificateException::class,
112+
527 => Unofficial\Cloudflare\RailgunErrorException::class,
113+
];
114+
115+
public static function create(ResponseInterface $response, CoreException $previousException)
116+
{
117+
$exception = static::STATUS_CODE_EXCEPTION_MAP[$response->getStatusCode()];
118+
return $exception::create($response, $previousException);
119+
}
120+
}

src/ExpectationFailedException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tools\Psr7\HttpStatusExceptions;
4+
5+
use Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class ExpectationFailedException extends AbstractException
9+
{
10+
public static function create(ResponseInterface $response, Exception $exception = null)
11+
{
12+
return (new self('Expectation Failed', 417, $exception))->setResponse($response);
13+
}
14+
}

0 commit comments

Comments
 (0)