ErrorPlugin
in package
implements
Plugin
Throw exception when the response of a request is not acceptable.
Status codes 400-499 lead to a ClientErrorException, status 500-599 to a ServerErrorException.
Warning
Throwing an exception on a valid response violates the PSR-18 specification. This plugin is provided as a convenience when writing a small application. When providing a client to a third party library, this plugin must not be included, or the third party library will have problems with error handling.
Tags
Table of Contents
Interfaces
- Plugin
- A plugin is a middleware to transform the request and/or the response.
Properties
- $onlyServerException : bool
Methods
- __construct() : mixed
- handleRequest() : Promise
- Handle the request and return the response coming from the next callable.
- transformResponseToException() : ResponseInterface
- Transform response to an error if possible.
Properties
$onlyServerException
private
bool
$onlyServerException
Whether this plugin should only throw 5XX Exceptions (default to false).
If set to true 4XX Responses code will never throw an exception
Methods
__construct()
public
__construct([array{'only_server_exception'?: bool} $config = [] ]) : mixed
Parameters
- $config : array{'only_server_exception'?: bool} = []
-
Configuration options:
- only_server_exception: Whether this plugin should only throw 5XX Exceptions (default to false)
handleRequest()
Handle the request and return the response coming from the next callable.
public
handleRequest(RequestInterface $request, callable $next, callable $first) : Promise
Parameters
- $request : RequestInterface
- $next : callable
-
Next middleware in the chain, the request is passed as the first argument
- $first : callable
-
First middleware in the chain, used to to restart a request
Return values
Promise —Resolves a PSR-7 Response or fails with an Http\Client\Exception (The same as HttpAsyncClient)
transformResponseToException()
Transform response to an error if possible.
private
transformResponseToException(RequestInterface $request, ResponseInterface $response) : ResponseInterface
Parameters
- $request : RequestInterface
-
Request of the call
- $response : ResponseInterface
-
Response of the call
Tags
Return values
ResponseInterface —If status code is not in 4xx or 5xx return response