HTTP Exceptions

Implementing The HttpException Interfaces

There's a HttpException interface for each class of HTTP Status code.

HTTP Status Code Exception Interface
1xx GanbaroDigital\HttpStatus\Interfaces\HttpInformationalException
2xx GanbaroDigital\HttpStatus\Interfaces\HttpSuccessfulStatusException
3xx GanbaroDigital\HttpStatus\Interfaces\HttpRedirectionException
4xx GanbaroDigital\HttpStatus\Interfaces\HttpRequestErrorException
5xx GanbaroDigital\HttpStatus\Interfaces\HttpRuntimeErrorException

Each of these HttpException interfaces:

Use this in combination with one of the HTTP Status Provider Traits to map your exception onto a HTTP status code:

use GanbaroDigital\HttpStatus\Interfaces\HttpRequestErrorException;
use GanbaroDigital\HttpStatus\StatusProviders\RequestError\MethodNotAllowedStatusProvider;

class UpdateNotAllowed extends Exception implements HttpRequestErrorException
{
    // adds `getHttpStatus()` to the exception
    use MethodNotAllowedStatusProvider;
}

Catching HTTP Exceptions

This gives you several different ways to catch any exception that uses the HttpException interfaces.

// example 1: catch a specific exception
try {
    throw UpdateNotAllowed("notifications are read-only");
}
catch (UpdateNotAllowed $e) {
    $httpStatus = $e->getHttpStatus();
    // ...
}
// example 2: catch all exceptions that are request errors
use GanbaroDigital\HttpStatus\Interfaces\HttpRequestErrorException;

try {
    throw new UpdateNotAllowed("notifications are read-only");
}
catch (HttpRequestErrorException $e) {
    $httpStatus = $e->getHttpStatus();
    // ...
}
// example 3: catch all exceptions that map onto a HTTP status code
use GanbaroDigital\HttpStatus\Interfaces\HttpException;

try {
    throw new UpdateNotAllowed("notifications are read-only");
}
catch (HttpException $e) {
    $httpStatus = $e->getHttpStatus();
    // ...
}