Skip to content

Commit 064b495

Browse files
author
Greg Bowler
authored
Http response exceptions (#35)
* Build up request URI in parts for greater compatibility * Created HTTP response exceptions up to 408 * Http 4xx exceptions * Http 5xx exceptions * Missing StatusCode constants
1 parent 04de39a commit 064b495

Some content is hidden

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

50 files changed

+718
-10
lines changed

src/HttpRedirectException.php

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\HttpException;
5+
6+
abstract class AbstractResponseStatusException extends HttpException {
7+
public function __construct(string $message = "") {
8+
parent::__construct(
9+
$message,
10+
$this->getHttpCode(),
11+
null
12+
);
13+
}
14+
15+
abstract protected function getHttpCode():int;
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\StatusCode;
5+
6+
/**
7+
* The server was acting as a gateway or proxy and received an invalid response
8+
* from the upstream server.
9+
* @link https://httpstatuses.com/502
10+
*/
11+
class HttpBadGateway extends AbstractResponseStatusException {
12+
protected function getHttpCode():int {
13+
return StatusCode::BAD_GATEWAY;
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\StatusCode;
5+
6+
/**
7+
* The server cannot or will not process the request due to an apparent client
8+
* error (e.g., malformed request syntax, size too large, invalid request
9+
* message framing, or deceptive request routing).
10+
* @link https://httpstatuses.com/400
11+
*/
12+
class HttpBadRequest extends AbstractResponseStatusException {
13+
protected function getHttpCode():int {
14+
return StatusCode::BAD_REQUEST;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\StatusCode;
5+
6+
class HttpConflict extends AbstractResponseStatusException {
7+
protected function getHttpCode():int {
8+
return StatusCode::CONFLICT;
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\StatusCode;
5+
6+
class HttpExpectationFailed extends AbstractResponseStatusException {
7+
protected function getHttpCode():int {
8+
return StatusCode::EXPECTATION_FAILED;
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\StatusCode;
5+
6+
class HttpFailedDependency extends AbstractResponseStatusException {
7+
protected function getHttpCode():int {
8+
return StatusCode::FAILED_DEPENDENCY;
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\StatusCode;
5+
6+
/**
7+
* The request contained valid data and was understood by the server, but the
8+
* server is refusing action. This may be due to the user not having the
9+
* necessary permissions for a resource or needing an account of some sort, or
10+
* attempting a prohibited action (e.g. creating a duplicate record where only
11+
* one is allowed). This code is also typically used if the request provided
12+
* authentication via the WWW-Authenticate header field, but the server did not
13+
* accept that authentication. The request should not be repeated.
14+
* @link https://httpstatuses.com/403
15+
*/
16+
class HttpForbidden extends AbstractResponseStatusException {
17+
protected function getHttpCode():int {
18+
return StatusCode::FORBIDDEN;
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\StatusCode;
5+
6+
/**
7+
* Tells the client to look at (browse to) another URL. 302 has been superseded
8+
* by 303 and 307. This is an example of industry practice contradicting the
9+
* standard. The HTTP/1.0 specification (RFC 1945) required the client to
10+
* perform a temporary redirect (the original describing phrase was
11+
* "Moved Temporarily"), but popular browsers implemented 302 with the
12+
* functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303
13+
* and 307 to distinguish between the two behaviours. However, some Web
14+
* applications and frameworks use the 302 status code as if it were the 303.
15+
* @link https://httpstatuses.com/302
16+
*/
17+
class HttpFound extends AbstractResponseStatusException {
18+
protected function getHttpCode():int {
19+
return StatusCode::FOUND;
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Gt\Http\ResponseStatusException;
3+
4+
use Gt\Http\StatusCode;
5+
6+
/**
7+
* The server was acting as a gateway or proxy and did not receive a timely
8+
* response from the upstream server.
9+
* @link https://httpstatuses.com/504
10+
*/
11+
class HttpGatewayTimeout extends AbstractResponseStatusException {
12+
protected function getHttpCode():int {
13+
return StatusCode::GATEWAY_TIMEOUT;
14+
}
15+
}

0 commit comments

Comments
 (0)