HTTP Exceptions
Implementing The HttpException Interfaces
There's a HttpException interface for each class of HTTP Status code.
Each of these HttpException interfaces:
- extends GanbaroDigital\HttpStatus\Interfaces\HttpException
- extends GanbaroDigital\HttpStatus\Interfaces\HttpStatusProvider
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();
    // ...
}