这是indexloc提供的服务,不要输入任何密码
Skip to content

The RouteNotFound that is used when the router finds nothing is now public #3326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fizker
Copy link
Contributor

@fizker fizker commented Apr 20, 2025

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.
            ...
        }
    }
}

@fizker fizker requested review from 0xTim and gwynne as code owners April 20, 2025 09:33
Copy link

codecov bot commented Apr 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.34%. Comparing base (1cbae2c) to head (714b751).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3326      +/-   ##
==========================================
- Coverage   77.77%   77.34%   -0.43%     
==========================================
  Files         226      226              
  Lines        9132     9102      -30     
==========================================
- Hits         7102     7040      -62     
- Misses       2030     2062      +32     
Files with missing lines Coverage Δ
Sources/Vapor/Responder/DefaultResponder.swift 98.97% <100.00%> (ø)

... and 12 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant