-
Notifications
You must be signed in to change notification settings - Fork 4
Error Handler
← Container Component | Middleware Component →
Error handlers are used for showing a beautiful page on captured errors during the runtime of an application. An error handler may show a stack trace how the process arrived in the said error and may show possible fixes and suggestions on how to handle it.
Caution
Errors are a good way to display what is wrong in an application. However, it can be very dangerous to show a very detailed error message into production as it might get used as an entry point by hackers on how to gain access to the application. With this, showing errors must be disabled in a production environment.
Whoops is one of the third-party error handlers that is supported by Slytherin. To integrate it to the Slytherin core, kindly use the WhoopsErrorHandler
provided by Slytherin then set the newly created instance into a Container:
use Rougin\Slytherin\Container\Container;
use Rougin\Slytherin\Debug\ErrorHandlerInterface;
use Rougin\Slytherin\Debug\WhoopsErrorHandler;
$container = new Container;
$class = ErrorHandlerInterface::class;
$whoops = new WhoopsErrorHandler(new Whoops\Run);
$container->set($class, $whoops);
The ErrorHandlerIntegration
is a built-in integration by Slytherin for its ErrorHandler
component to remove the need of manually creating an instance for the said component:
use Rougin\Slytherin\Application;
use Rougin\Slytherin\Configuration;
use Rougin\Slytherin\Debug\ErrorHandlerIntegration;
$config = new Configuration;
$config->set('app.environment', 'development');
$app = new Application(null, $config);
$app->integrate(new ErrorHandlerIntegration);
echo $app->run();
Note
The respective instance that will be created for the said integration is only limited for the third-party packages that are currently supported by Slytherin. The built-in implementation will be used if no third-party packages are installed for the said component.
Slytherin has built-in support for error handlers if using the ErrorHandlerIntegration
. Besides the default PHP implementation of handling errors (the ErrorHandler
class), the following third-party packages below can be integrated seamlessly to the Slytherin core once their respective packages were installed:
-
WhoopsErrorHandler
- uses the Whoops package by Filipe Dobreira (filp/whoops
)
If not using the supported packages above, a list of packages that implements debugging functionalities are also listed at the awesome-php repository. Once selected a specified package, it must be implemented in ErrorHandlerInterface
:
namespace Rougin\Slytherin\Debug;
interface ErrorHandlerInterface
{
/**
* Registers the instance as an error handler.
*
* @return void
*/
public function display();
}