github vapor/vapor 4.121.0
4.121.0 - The RouteNotFound that is used when the router finds nothing is now public

latest releases: 4.121.4, 4.121.3, 4.121.2...
4 months ago

What's Changed

The RouteNotFound that is used when the router finds nothing is now public by @fizker in #3326

When no routes match the current request, DefaultResponder throws the RouteNotFound error.

This PR makes that error public, allowing any middleware to handle that specific scenario.

Unlike the CatchAll route **, a middleware catching the RouteNotFound works across all HTTP methods automatically.

This would be used in a middleware like so:

public struct CatchAllMiddleware: AsyncMiddleware {
    public func respond(to request: Request, chainingTo next: any AsyncResponder) async throws -> Response {
        do {
            return try await next.respond(to: request)
        } catch is RouteNotFound {
            // No route was found, so this would normally result in a 404. 
            // We can now handle this like any other request.
            // Or we can make treat it like an ErrorMiddleware where we 
            // log purposeful 404s different than `RouteNotFound` 404s.
            ...
        }
    }
}
This patch was released by @0xTim

Full Changelog: 4.120.0...4.121.0

Don't miss a new vapor release

NewReleases is sending notifications on new releases.