Skip to content

Commit a206086

Browse files
feat: Implemented new http client exceptions (418, 425 and 451).
Signed-off-by: Johannes Tegnér <johannes@jitesoft.com>
1 parent 1dbbb42 commit a206086

File tree

5 files changed

+135
-4
lines changed

5 files changed

+135
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3+
HttpImATeapotException.php - Part of the php-exceptions project.
4+
5+
© - Jitesoft
6+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7+
namespace Jitesoft\Exceptions\Http\Client;
8+
9+
use Jitesoft\Exceptions\Http\HttpException;
10+
use Jitesoft\Exceptions\Http\Server\HttpNotImplementedException;
11+
use Throwable;
12+
13+
/**
14+
* Class HttpImATeapotException
15+
*
16+
* Client http exception which is thrown when server is a coffee brewer hence can't make tea.
17+
*
18+
* This is a standardized joke, the http error is sometimes used to indicate that something is not
19+
* fully implemented (in which case @see HttpNotImplementedException would be preferable.
20+
*/
21+
class HttpImATeapotException extends HttpException {
22+
23+
/**
24+
* HttpImATeapotException constructor.
25+
*
26+
* @param string $message
27+
* @param int $code
28+
* @param Throwable|null $previous
29+
*/
30+
public function __construct(
31+
string $message = "I'm a teapot.",
32+
int $code = 418,
33+
?Throwable $previous = null
34+
) {
35+
parent::__construct($message, $code, $previous);
36+
}
37+
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3+
HttpToEarlyException.php - Part of the php-exceptions project.
4+
5+
© - Jitesoft
6+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7+
namespace Jitesoft\Exceptions\Http\Client;
8+
9+
use Jitesoft\Exceptions\Http\HttpException;
10+
use Throwable;
11+
12+
/**
13+
* Class HttptoEarlyException
14+
*
15+
* Client http exception which is used to indicate that the server is unwilling
16+
* to risk processing the request that might be replayed.
17+
*
18+
* This is an experimental http status code which might change.
19+
*/
20+
class HttpToEarlyException extends HttpException {
21+
22+
/**
23+
* HttpToEarlyException constructor.
24+
*
25+
* @param string $message
26+
* @param int $code
27+
* @param Throwable|null $previous
28+
*/
29+
public function __construct(
30+
string $message = 'To early.',
31+
int $code = 425,
32+
?Throwable $previous = null
33+
) {
34+
parent::__construct($message, $code, $previous);
35+
}
36+
37+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3+
HttpUnavailableForLegalReasonsException.php - Part of the php-exceptions project.
4+
5+
© - Jitesoft
6+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7+
namespace Jitesoft\Exceptions\Http\Client;
8+
9+
use Jitesoft\Exceptions\Http\HttpException;
10+
use Throwable;
11+
12+
/**
13+
* Class HttpUnavailableForLegalReasonsException
14+
*
15+
* Client http exception thrown when a requested resource is unavailable
16+
* due to legal reasons hence can't be served.
17+
*/
18+
class HttpUnavailableForLegalReasonsException extends HttpException {
19+
20+
private ?string $reason;
21+
22+
/**
23+
* Reason for unavailability.
24+
*
25+
* @return string|null
26+
*/
27+
public function getReason(): ?string {
28+
return $this->reason;
29+
}
30+
31+
/**
32+
* HttpUnavailableForLegalReasonsException constructor.
33+
*
34+
* @param string $message
35+
* @param string|null $reason
36+
* @param int $code
37+
* @param Throwable|null $previous
38+
*/
39+
public function __construct(
40+
string $message = 'Unavailable for Legal Reasons.',
41+
?string $reason = null,
42+
int $code = 451,
43+
?Throwable $previous = null
44+
) {
45+
parent::__construct($message, $code, $previous);
46+
47+
$this->reason = $reason;
48+
}
49+
50+
}

src/Http/Server/HttpBadGatewayException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
2+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3+
HttpBadGatewayException.php - Part of the php-exceptions project.
24
5+
© - Jitesoft
6+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
37
namespace Jitesoft\Exceptions\Http\Server;
48

59
use Jitesoft\Exceptions\Http\HttpException;
@@ -21,7 +25,7 @@ class HttpBadGatewayException extends HttpException {
2125
* @param Throwable|null $previous
2226
*/
2327
public function __construct(
24-
string $message = "Bad Gateway.",
28+
string $message = 'Bad Gateway.',
2529
int $code = 502,
2630
?Throwable $previous = null
2731
) {

src/Http/Server/HttpServiceUnavailableException.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* to retry the request after the given time.
1919
*/
2020
class HttpServiceUnavailableException extends HttpException {
21-
2221
protected ?int $retryAfter;
2322

2423
/**
@@ -39,12 +38,15 @@ public function getRetryAfter(): ?int {
3938
* @param Throwable|null $previous
4039
*/
4140
public function __construct(
42-
string $message =
43-
"Service is currently not able to handle the request.",
41+
string $message
42+
= 'Service is currently not able to handle the request.',
4443
int $code = 503,
4544
?int $retryAfter = null,
4645
?Throwable $previous = null
4746
) {
4847
parent::__construct($message, $code, $previous);
48+
49+
$this->retryAfter = $retryAfter;
4950
}
51+
5052
}

0 commit comments

Comments
 (0)