π₯Handling Failures
Saloon has a powerful exception handler with many descriptive exceptions you can use in your application.
When you send a request, Saloon won't do anything if the request fails, but by default, it will use the status code to determine if a request is successful or not. The only exception to this is if Saloon cannot connect to an API, which will throw a FatalRequestException
.
Always throw exceptions on failed requests
You may wish to throw an exception whenever a request fails (4xx or 5xx response). You can add the AlwaysThrowOnErrors
trait on your connector, and then every request that fails will throw an exception.
Using the throw method
On a per-response basis, you may use the throw
method after sending your response. This method will throw an exception if the response has a "failed" HTTP status code like 4xx or 5xx.
Response Exceptions
Saloon's default exception handler contains the following exceptions based on the status code and severity of the exception. These are thrown depending on the method you use below.
Handling failures with promises
When sending requests using sendAsync
or using request pooling, you will receive a PromiseInterface
instance. Since this class catches exceptions, Saloon will automatically pass the request exception in the otherwise
block, and you do not have to use the throw
method.
Customising when Saloon thinks a request has failed
You may integrate with an API which returns a 200 response status but with an error message in the response body. To handle this, you can extend the hasRequestFailed
method on your connector or request.
Customising the request exception
By default, Saloon will use the exceptions listed above, but you may choose to return your own exception if a request has failed. Just extend the getRequestException
method on either your connector or request. You will receive an instance of the response and a sender exception, which may be nullable.
Priority is given to the request when you extend the getRequestException
method on both your connector and request.
Last updated