Skip to main content

Error handling

Catching exceptions​

Every exception the package throws implements Dirthara\Routing\Exception\RoutingException, so one catch covers them all. Each also extends the SPL exception that describes the failure: an invalid route definition or URL parameter throws an InvalidArgumentException, and a path the router cannot route throws a RuntimeException.

use Dirthara\Routing\Exception\RoutingException;
use Dirthara\Routing\Exception\RouteNotFoundException;

try {
$match = $router->match($method, $path);
} catch (RouteNotFoundException $exception) {
// no route matches the path
} catch (RoutingException $exception) {
// anything else this package raised
}
ExceptionExtendsThrown for
RouteNotFoundExceptionRuntimeExceptionA path no route matches, and a name or action no route has.
MethodNotAllowedExceptionRuntimeExceptionA path that has routes, but none for the request method.
InvalidRouteExceptionInvalidArgumentExceptionInvalid paths, names, and constraints, and a name or action that more than one route has.
InvalidUrlParameterExceptionInvalidArgumentExceptionMissing, unknown, or non-matching parameters when generating a URL.

All exception classes are final; catch them by class or by RoutingException.

Context​

Every exception carries a context array with the values that describe the failure, such as the method and path that did not match, or the name, path, and parameter of a URL that could not be generated. Add your own with addContext(), which merges into what is there and returns the exception:

try {
$match = $router->match($method, $path);
} catch (RoutingException $exception) {
throw $exception->addContext(['host' => $request->getUri()->getHost()]);
}

Control characters in a path, name, or pattern are escaped in the message, so a request path cannot forge extra lines in a log. The context keeps the original values.

The package never logs. When generating a URL, it never includes a parameter's value in a message or in context.

caution

A path that does not match is different: RouteNotFoundException and MethodNotAllowedException include the request path in their message and in context, and that path contains every parameter value in it. If your paths can carry tokens, such as a password reset link, redact the path before you log these exceptions.