chiron/http-exceptions

Exception classes for Http Exception.

Fund package maintenance!
ncou

2.03 2020-10-04 12:46 UTC

This package is auto-updated.

Last update: 2024-12-18 18:34:10 UTC


README

Build Status Coverage Status CodeCov

Total Downloads Monthly Downloads

StyleCI PHP-Eye PHPStan

HttpExceptions

All HTTP statuses from RFC 7231 implemented as separated Exceptions.

These Exceptions support a straightforward implementation of the IETF Problem Details for HTTP APIs RFC 7807.

abstract HttpException class and its subclasses provide exceptions corresponding to HTTP error status codes. The most common are included, but you can create exceptions for other status codes by using (or subclassing) HttpException and providing the reason phrase as the $title and the status code as the $statusCode.

This package provides the following exception classes in the Chiron\Http\Exception\Client namespace for 4xx http errors. And Chiron\Http\Exception\Server namespace for 5xx http errors.

4xx: Client Error - The request contains bad syntax or cannot be fulfilled 5xx: Server Error - The server failed to fulfill an apparently valid request

Client errors :

Server errors :

References for HTTP status code

Basic Usage

Throw an exception.

throw new \Chiron\Http\Exception\Client\BadRequestHttpException(); 

Throw an exception with a custom message.

$e = new \Chiron\Http\Exception\Client\BadRequestHttpException("Invalid syntax !");
throw $e->setHeaders(['X-Custom-Header' => 'foobar']);

Catch an exception and output an HTML response.

try {
    // ... 
} catch (\Chiron\Http\Exception\HttpException $e) {
    http_response_code($e->getStatusCode());
    header("Content-type: text/html");
    print "<h1>" . $e->getMessage() . "</h1>";
}

Or, if you're using PSR7 response :

try {
    // ... 
} catch (\Chiron\Http\Exception\HttpExceptionInterface $e) {
    $response = $response
        ->withStatus($e->getStatusCode())
        ->withHeader("Content-type", "text/html")
        ->getBody()->write("<h1>" . $e->getMessage() . "</h1>");
}

Api Problem

The following properties are available to use it for API Problem RFC 7807.

$e = new \Chiron\Http\Exception\Client\ForbiddenHttpException();

$e->setTitle('You do not have enough credit.');
$e->setDetail('Your current balance is 30, but that costs 50.');
$e->setType('https://example.com/probs/out-of-credit');
$e->setIntance('https://example.net/account/12345/msgs/abc');
$e->setAdditionalData(['balance' => 30, 'accounts' => ['https://example.net/account/12345', 'https://example.net/account/67890']]);

throw $e;

And output the API Problem response like this :

try {
    // ... 
} catch (\Chiron\Http\Exception\HttpException $e) {
    http_response_code($e->getStatusCode());
    header("Content-type: application/problem+json");
    print json_encode($e);
}

For XML output you can use the function $e->toArray() and serialize the data.

Install

Add Chiron/http-exceptions to your composer.json

{
    "require": {
        "chiron/http-exceptions": "^2.0"
    }
}

License

Licensed under the MIT license

Suggestions

Tips : Usage Suggestion