Download the PHP package phauthentic/error-response without Composer
On this page you can find all versions of the php package phauthentic/error-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package error-response
RFC 9457: Problem Details for HTTP APIs
This library is an implementation of RFC 9457.
HTTP status codes cannot always convey enough information about errors to be helpful. While humans using web browsers can often understand an HTML response content, non-human consumers of HTTP APIs have difficulty doing so.
To address that shortcoming, RFC 9457 defines simple JSON and XML document formats to describe the specifics of a problem encountered.
For example, consider a response indicating that the client's account doesn't have enough credit. The API's designer might decide to use the 403 Forbidden status code to inform generic HTTP software (such as client libraries, caches, and proxies) of the response's general semantics. API-specific problem details (such as why the server refused the request and the applicable account balance) can be carried in the response content so that the client can act upon them appropriately (for example, triggering a transfer of more credit into the account).
Installation
Documentation
It is recommended to read the RFC 9457 at least briefly to understand the RFC and how the actual implementation helps you.
Middleware
The ErrorResponseMiddleware
is a PSR-15 middleware designed to handle exceptions thrown during the execution of a request and convert them into RFC 9457 conforming error responses.
The middleware takes two argumens:
- A PSR7 Response Factory implementing
Psr\Http\Message\ResponseFactoryInterface
. - An array of exception class names that the middleware should intercept. If an exception is an instance of any of these classes, it will be converted into an error response.
Use a proper class hierarchy for your exceptions! For example, have a DatabaseAccessLayerException
from which you derive sub-types instead of declaring hundreds of exception classes and pass them to the middleware.
Error Responses
Error Responses can be constructed using one of the provided factories or by instantiating the ErrorResponse
directly.
License
Copyright Florian Krämer
Licensed under the MIT license.
All versions of error-response with dependencies
psr/http-factory Version ^1.0
psr/http-message Version ^2.0
psr/http-server-middleware Version ^1.0