studiow/http-exceptions

v1.0.3 2022-08-01 12:37 UTC

This package is auto-updated.

Last update: 2024-12-29 06:41:03 UTC


README

A helper package for throwing HTTP Exceptions (4xx - 5xx)

Installation

The easiest way to install this package is using composer:

composer require studiow/http-exceptions

Throwing exceptions

The package provides dedicated Exceptions for each of the 4xx and 5xx error status codes. You throw them like you would throw any other exception:

if (! file_exists($my_file)){
  throw new \Studiow\Http\Exceptions\ClientError\NotFoundException();
}

Outputting to Json

All of the exceptions have a toJsonResponse method. This method expects a PSR-7 ResponseInterface object and will return a a PSR-7 ResponseInterface object with the appropriate headers, status code and reason phrase set.

$exception = new \Studiow\Http\Exceptions\ClientError\NotFoundException();

/* convert the exception to a ResponseInterfaceObject */
$response = $exception->toJsonResponse(new \GuzzleHttp\Psr7\Response());

Exception overview

Client Error (4xx)

Server Error (5xx)