mini\Dispatcher\HttpDispatcher::registerExceptionConverter()
Method
public
Signature
public function registerExceptionConverter(Closure $converter): void
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$converter |
Closure |
required | Documentation missing |
Returns
void
Documentation
Register an exception converter
Exception converters transform exceptions to HTTP responses. They are separate from the main converter registry to keep concerns separated.
Examples:
// Handle 404 errors
$dispatcher->registerExceptionConverter(function(NotFoundException $e): ResponseInterface {
return new Response(404, ['Content-Type' => 'text/html'], render('404'));
});
// Handle validation errors
$dispatcher->registerExceptionConverter(function(ValidationException $e): ResponseInterface {
$json = json_encode(['errors' => $e->errors]);
return new Response(400, ['Content-Type' => 'application/json'], $json);
});
// Generic error handler
$dispatcher->registerExceptionConverter(function(\Throwable $e): ResponseInterface {
$statusCode = 500;
$message = Mini::$mini->debug ? $e->getMessage() : 'Internal Server Error';
return new Response($statusCode, ['Content-Type' => 'text/html'], render('error', compact('message')));
});