Download the PHP package wellrested/error-handling without Composer
On this page you can find all versions of the php package wellrested/error-handling. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wellrested/error-handling
More information about wellrested/error-handling
Files in wellrested/error-handling
Package error-handling
Short Description Classes to facilitate error handling with WellRESTed.
License MIT
Informations about the package error-handling
Error Handling
Provides classes to facilitate error handling with WellRESTed.
ErrorHandler and its subclasses provide human-readable default responses for responses with error status codes. The package includes concrete class that are ready to be dropped into you project as well as an abstract base class for creating completely custom error responses.
Catcher wraps a sequence of middleware in a try/catch block to allow recovery from exceptions. Catcher also provides response status codes for HttpExceptions.
Install
Add this package to your composer.json
Error Handler
ErrorHandler
and its concrete subclasses provide human-readable responses
for responses with status codes >= 400. To use, add an ErrorHandler
subclass
to your Server
before the middleware that may return an error response.
Text- and HtmlErrorHandler
When the router fails to match a route (or middleware returns a response with
a 404
status code), the TextErrorHandler
provides a plain/text response:
Or, if you're prefer HTML, use the HtmlErrorHandler
.
Custom Error Handlers
To provide your own custom error responses, subclass
WellRESTed\ErrorHandling\ErrorHandler
and implement provideResponseForError
.
This method expects a request and response as parameters and returns the updated
response.
You do not need to check whether or not to handle the response; the
ErrorHandler
instance calls provideResponseForError
only when:
- The middleware that follows it provides a response.
- That response has a status code >= 400.
- That response body is either not readable or has a size of 0.
Here's an example that provides a silly response for 404 errors, and normal responses for all other errors.
Catcher
A Catcher instance wraps a sequence of middleware in a try/catch block to allow for recovery from exceptions.
Catcher may be used "out-of-the-box" to provide response status codes for HttpExceptions, or you can subclass it to respond to other Exceptions.
Basic Usage
Add a Catcher
to your server and add middleware to it using the Catcher::add
method. Be sure to pass a reference to a dispatcher (an instance implementing
WellRESTed\Dispatching\DispatcherInterface
) to the constructor. The Server
provides access to the dispatcher it uses via Server::getDispatcher
.
When any middleware contained in the Catcher
throws an HttpException
, the
catcher will return a response with a status code matching the HttpException
's
code. For example, throwing a ConflictException
results in a 409 Conflict
status.
NOTE: When middleware throws an exception, the execution jumps directly to
the Catcher
and does not work its way back down through the chain of
middleware. This may be useful if you want to provide an immediate out for your
response.
Custom Exceptions
To catch exceptions other than HttpException
s, subclass Catcher
and
implement the getResponseForException
method.
Catching and Handling
Catcher
and ErrorHandler
make a great team. Add an ErrorHandler
in front
of your Catcher
to provide default responses for exceptions.
Copyright and License
Copyright © 2015 by PJ Dietz Licensed under the MIT license
All versions of error-handling with dependencies
wellrested/wellrested Version ^3.0.0
wellrested/http-exceptions Version ^1.0.0