Skip to content

Exceptions ​

Routing exceptions ​

Requests that reach no route handler are reported with exceptions in Solo\Application\Exception. See Routing Errors for how to answer them.

RuntimeException
└── RoutingException (abstract)
    ├── RouteNotFoundException          404
    ├── MethodNotAllowedException       405, Allow
    └── InvalidRouteParameterException  400

RoutingException ​

php
abstract class RoutingException extends RuntimeException

The base class. The HTTP status is also the exception code.

php
public function __construct(
    string $message,
    int $statusCode,
    array $headers = [],
    ?Throwable $previous = null,
)

$headers maps header names to lists of values, array<string, list<string>>.

MethodReturns
getStatusCode(): intThe HTTP status, the same as getCode()
getHeaders(): arrayHeaders the response must carry, array<string, list<string>>, such as ['Allow' => ['GET', 'HEAD']]
applyTo(ResponseInterface $response): ResponseInterfaceThe response with this status and these headers; its body and other headers stay
php
use Solo\Application\Exception\RoutingException;

try {
    return $handler->handle($request);
} catch (RoutingException $e) {
    return $e->applyTo($responseFactory->createResponse());
}

Extend it for routing failures of your own; see your own routing exceptions.

RouteNotFoundException ​

php
final class RouteNotFoundException extends RoutingException
public function __construct(string $method, string $path)

No route matches the request: status 404, no headers. Thrown by RoutingMiddleware, and by RouteDispatcher for a request without a handler. Message: No route matches GET /missing.

MethodNotAllowedException ​

php
final class MethodNotAllowedException extends RoutingException
public function __construct(string $method, string $path, array $allowedMethods)
public function getAllowedMethods(): array

The path is routed for other methods only: status 405 and an Allow header with $allowedMethods. getAllowedMethods() returns them as list<string>. An empty list gives an empty Allow header, which tells the client that the resource allows no method at the moment. Message: Method DELETE is not allowed for /users/1; allowed: GET, HEAD.

InvalidRouteParameterException ​

php
final class InvalidRouteParameterException extends RoutingException
public function __construct(string $method, string $path, string $parameter)

A route parameter decodes to a control character, such as NUL or a line break: status 400, no headers. Message: Route parameter "name" in GET /files/a%00b decodes to a control character.

Messages escape control characters in the method, so a request cannot break a log line.

Other exceptions ​

Configuration errors and invalid handlers or middleware are reported with SPL exceptions:

ExceptionThrown byWhen
RuntimeExceptionApplication constructorNo container passed and solophp/container not installed
InvalidArgumentExceptionApplication constructorA provider has no public register() method
RuntimeExceptionApplication constructorThe routes file is not found
UnexpectedValueExceptionApplication constructorThe routes file does not return a callable
InvalidArgumentExceptionApplication constructor, from Solo RouterInvalid routes, such as a duplicate name or path
LogicExceptionApplication constructor, from Solo RouterThe route cache is written and a route holds a closure or another object
RuntimeExceptionApplication constructor, from Solo RouterThe route cache file cannot be written
InvalidArgumentExceptionMiddlewarePipelineA middleware resolves to neither a MiddlewareInterface nor a callable
UnexpectedValueExceptionMiddlewarePipelineA callable middleware returns anything but a response
InvalidArgumentExceptionRouteDispatcherThe route handler cannot be called
UnexpectedValueExceptionRouteDispatcherThe route handler returns anything but a response

A service or class name the container cannot resolve throws the container's Psr\Container\NotFoundExceptionInterface.

Released under the MIT License.