1. Go to this page and download the library: Download wellrested/error-handling library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wellrested / error-handling example snippets
// Create a server.
$server = new \WellRESTed\Server();
// Add an error handler BEFORE the router and anything else that you want to
// provide error responses for.
$server->add(new \WellRESTed\ErrorHandling\TextErrorHandler());
// Add a router or any other middleware.
// If any middleware added AFTER the error handler returns a response with a
// status code >= 400, the error handler will provide a message body, headers,
// etc. to match.
$server->add($server->createRouter()
->register("GET", "/", 'MySite\RootEndpoint')
/* ... register other routes ... */
);
/**
* Provides a silly response for 404 errors.
*/
class CustomErrorHandler extends \WellRESTed\ErrorHandling\ErrorHandler
{
protected function provideResponseForError(
\Psr\Http\Message\ServerRequestInterface $request,
\Psr\Http\Message\ResponseInterface $response
) {
// Set the contet-type.
$response = $response->withHeader("Content-type", "text/html");
// Check if this is a Not Found error.
if ($response->getStatusCode() === 404) {
// Set a silly message body.
$message = "<h1>It's not here!</h1><p>I must have eaten it.</p>";
} else {
// Set the message body to the status code and reaspon phrase.
$message = "<h1>" . $response->getStatusCode() . " " . $response->getReasonPhrase() . "</h1>";
}
// Return the response.
return $response->withBody(new Stream($message));
}
}
// Create a server.
$server = new \WellRESTed\Server();
// Create a catcher, providing the dispatcher used by the server.
$catcher = new \WellRESTed\ErrorHandling\Catcher($server->getDispatcher());
// Add the catcher to the server.
$server->add($catcher);
// Add middleware to the catcher. Any HttpException thrown by middleware
// contained in the catcher will be converted to a response with an
// appropriate status code.
$catcher->add($server->createRouter()
->register("GET", "/", 'MySite\RootEndpoint')
/* ... add other routes ... */
);
class CustomCatcher extends Catcher
{
/**
* @param Exception $e The exception caught by the instance
* @param ServerRequestInterface $request The request passed into the
* instance's __invoke
* @param ResponseInterface $response The response passed into the
* instance's __invoke
* @return ResponseInterface|null $response with an updated status code
*/
protected function getResponseForException(
Exception $e,
ServerRequestInterface $request,
ResponseInterface $response
) {
if ($e instanceof \Some\Custom\Exception) {
// Do some logging or something...
// Return a response.
$response = $response
->withStatus(500)
->withBody(new \WellRESTed\Message\Stream(
"That's not supposed to happen."));
return $response;
}
// Optionally, return null to re-throw exceptions you don't want the
// instance to handle.
return null;
}
}
// Create a server.
$server = new \WellRESTed\Server();
// Create a catcher, providing the dispatcher used by the server.
$catcher = new \WellRESTed\ErrorHandling\Catcher($server->getDispatcher());
// Add an error handler near the front of the server.
$server->add(new WellRESTed\ErrorHandling\TextErrorHandler());
// Add the catcher to the server AFTER the error handler. This allows the
// error handler to react to the response returned by the catcher.
$server->add($catcher);
// Add middleware to the catcher. Any HttpException thrown by middleware
// contained in the catcher will be converted to a response with an
// appropriate status code.
//
// The catcher will return that response to the error handler, which will
// return a human-readable version of that response.
$catcher->add($server->createRouter()
->register("GET", "/", 'MySite\RootEndpoint')
/* ... add other routes ... */
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.