Skip to content

Commit d91ddc8

Browse files
feat: New http exceptions (502 and 503).
Signed-off-by: Johannes Tegnér <johannes@jitesoft.com>
1 parent 053b002 commit d91ddc8

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Jitesoft\Exceptions\Http\Server;
4+
5+
use Jitesoft\Exceptions\Http\HttpException;
6+
use Throwable;
7+
8+
/**
9+
* Class HttpBadGatewayException
10+
*
11+
* Server http exception thrown when the server - working as a gateway - encounters
12+
* an error in the response from the server.
13+
*/
14+
class HttpBadGatewayException extends HttpException {
15+
16+
/**
17+
* HttpBadGatewayException constructor.
18+
*
19+
* @param string $message
20+
* @param int $code
21+
* @param Throwable|null $previous
22+
*/
23+
public function __construct(
24+
string $message = "Bad Gateway.",
25+
int $code = 502,
26+
?Throwable $previous = null
27+
) {
28+
parent::__construct($message, $code, $previous);
29+
}
30+
31+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3+
HttpServiceUnavailableException.php - Part of the php-exceptions project.
4+
5+
© - Jitesoft
6+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7+
namespace Jitesoft\Exceptions\Http\Server;
8+
9+
use Jitesoft\Exceptions\Http\HttpException;
10+
use Throwable;
11+
12+
/**
13+
* Class HttpServiceUnavailableException
14+
*
15+
* Server http exception thrown when a request is not possible to be handled.
16+
*
17+
* It is recommended to set the retry-after header in the response to allow the client
18+
* to retry the request after the given time.
19+
*/
20+
class HttpServiceUnavailableException extends HttpException {
21+
22+
protected ?int $retryAfter;
23+
24+
/**
25+
* Retry-After header value which should be set if possible.
26+
*
27+
* @return int|null
28+
*/
29+
public function getRetryAfter(): ?int {
30+
return $this->retryAfter;
31+
}
32+
33+
/**
34+
* HttpServiceUnavailableException constructor.
35+
*
36+
* @param string $message
37+
* @param int $code
38+
* @param ?int $retryAfter
39+
* @param Throwable|null $previous
40+
*/
41+
public function __construct(
42+
string $message =
43+
"Service is currently not able to handle the request.",
44+
int $code = 503,
45+
?int $retryAfter = null,
46+
?Throwable $previous = null
47+
) {
48+
parent::__construct($message, $code, $previous);
49+
}
50+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Jitesoft\Exceptions\Tests\Http\Server;
4+
5+
use Jitesoft\Exceptions\Http\Server\HttpBadGatewayException;
6+
use Jitesoft\Exceptions\Tests\Http\HttpExceptionTest;
7+
8+
class HttpBadGatewayExceptionTest extends HttpExceptionTest {
9+
10+
protected $expectedErrorCode = 502;
11+
12+
protected function throwDefaultException(): void {
13+
throw new HttpBadGatewayException();
14+
}
15+
16+
public function throwMessageException(string $message): void {
17+
throw new HttpBadGatewayException($message);
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Jitesoft\Exceptions\Tests\Http\Server;
3+
4+
use Jitesoft\Exceptions\Http\Server\HttpServiceUnavailableException;
5+
use Jitesoft\Exceptions\Tests\Http\HttpExceptionTest;
6+
7+
/**
8+
* @group Exceptions
9+
* @group HttpExceptions
10+
* @group RuntimeExceptions
11+
* @group HttpServerExceptions
12+
*/
13+
class HttpServiceUnavailableExceptionTest extends HttpExceptionTest {
14+
15+
protected $expectedErrorCode = 503;
16+
17+
protected function throwDefaultException(): void {
18+
throw new HttpServiceUnavailableException();
19+
}
20+
21+
public function throwMessageException(string $message): void {
22+
throw new HttpServiceUnavailableException($message);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)