@@ -24,7 +24,7 @@ import {
24
24
trimPathLeft ,
25
25
trimPathRight ,
26
26
} from './path'
27
- import { isNotFound } from './not-found'
27
+ import { isNotFound , isVariantNotFoundError } from './not-found'
28
28
import { setupScrollRestoration } from './scroll-restoration'
29
29
import { defaultParseSearch , defaultStringifySearch } from './searchParams'
30
30
import { rootRouteId } from './root'
@@ -2251,14 +2251,16 @@ export class RouterCore<
2251
2251
return ! ! ( allPreload && ! this . state . matches . find ( ( d ) => d . id === matchId ) )
2252
2252
}
2253
2253
2254
- const handleRedirectAndNotFound = ( match : AnyRouteMatch , err : any ) => {
2254
+ const handleRouteError = ( match : AnyRouteMatch , err : any ) => {
2255
2255
if ( isResolvedRedirect ( err ) ) {
2256
2256
if ( ! err . reloadDocument ) {
2257
2257
throw err
2258
2258
}
2259
2259
}
2260
2260
2261
- if ( isRedirect ( err ) || isNotFound ( err ) ) {
2261
+ const isError =
2262
+ err && err instanceof Error && ! isVariantNotFoundError ( err )
2263
+ if ( isRedirect ( err ) || isNotFound ( err ) || isError ) {
2262
2264
updateMatch ( match . id , ( prev ) => ( {
2263
2265
...prev ,
2264
2266
status : isRedirect ( err )
@@ -2272,8 +2274,8 @@ export class RouterCore<
2272
2274
loaderPromise : undefined ,
2273
2275
} ) )
2274
2276
2275
- if ( ! ( err as any ) . routeId ) {
2276
- ; ( err as any ) . routeId = match . routeId
2277
+ if ( ! err . routeId ) {
2278
+ err . routeId = match . routeId
2277
2279
}
2278
2280
2279
2281
match . beforeLoadPromise ?. resolve ( )
@@ -2293,6 +2295,11 @@ export class RouterCore<
2293
2295
match : this . getMatch ( match . id ) ! ,
2294
2296
} )
2295
2297
throw err
2298
+ } else if ( isError ) {
2299
+ this . serverSsr ?. onMatchSettled ( {
2300
+ router : this ,
2301
+ match : this . getMatch ( match . id ) ! ,
2302
+ } )
2296
2303
}
2297
2304
}
2298
2305
}
@@ -2318,13 +2325,13 @@ export class RouterCore<
2318
2325
2319
2326
err . routerCode = routerCode
2320
2327
firstBadMatchIndex = firstBadMatchIndex ?? index
2321
- handleRedirectAndNotFound ( this . getMatch ( matchId ) ! , err )
2328
+ handleRouteError ( this . getMatch ( matchId ) ! , err )
2322
2329
2323
2330
try {
2324
2331
route . options . onError ?.( err )
2325
2332
} catch ( errorHandlerErr ) {
2326
2333
err = errorHandlerErr
2327
- handleRedirectAndNotFound ( this . getMatch ( matchId ) ! , err )
2334
+ handleRouteError ( this . getMatch ( matchId ) ! , err )
2328
2335
}
2329
2336
2330
2337
updateMatch ( matchId , ( prev ) => {
@@ -2523,7 +2530,7 @@ export class RouterCore<
2523
2530
await prevLoaderPromise
2524
2531
const match = this . getMatch ( matchId ) !
2525
2532
if ( match . error ) {
2526
- handleRedirectAndNotFound ( match , match . error )
2533
+ handleRouteError ( match , match . error )
2527
2534
}
2528
2535
} else {
2529
2536
const parentMatchPromise = matchPromises [ index - 1 ] as any
@@ -2643,10 +2650,7 @@ export class RouterCore<
2643
2650
const loaderData =
2644
2651
await route . options . loader ?.( getLoaderContext ( ) )
2645
2652
2646
- handleRedirectAndNotFound (
2647
- this . getMatch ( matchId ) ! ,
2648
- loaderData ,
2649
- )
2653
+ handleRouteError ( this . getMatch ( matchId ) ! , loaderData )
2650
2654
2651
2655
// Lazy option can modify the route options,
2652
2656
// so we need to wait for it to resolve before
@@ -2675,13 +2679,13 @@ export class RouterCore<
2675
2679
2676
2680
await potentialPendingMinPromise ( )
2677
2681
2678
- handleRedirectAndNotFound ( this . getMatch ( matchId ) ! , e )
2682
+ handleRouteError ( this . getMatch ( matchId ) ! , e )
2679
2683
2680
2684
try {
2681
2685
route . options . onError ?.( e )
2682
2686
} catch ( onErrorError ) {
2683
2687
error = onErrorError
2684
- handleRedirectAndNotFound (
2688
+ handleRouteError (
2685
2689
this . getMatch ( matchId ) ! ,
2686
2690
onErrorError ,
2687
2691
)
@@ -2710,7 +2714,7 @@ export class RouterCore<
2710
2714
} ) )
2711
2715
executeHead ( )
2712
2716
} )
2713
- handleRedirectAndNotFound ( this . getMatch ( matchId ) ! , err )
2717
+ handleRouteError ( this . getMatch ( matchId ) ! , err )
2714
2718
}
2715
2719
}
2716
2720
@@ -3088,10 +3092,15 @@ export class RouterCore<
3088
3092
}
3089
3093
3090
3094
// Ensure we have a notFoundComponent
3091
- invariant (
3092
- routeCursor . options . notFoundComponent ,
3093
- 'No notFoundComponent found. Please set a notFoundComponent on your route or provide a defaultNotFoundComponent to the router.' ,
3094
- )
3095
+ // generic Error instead of a NotFoundError when notFoundComponent doesn't exist, is this a bug?
3096
+ try {
3097
+ invariant (
3098
+ routeCursor . options . notFoundComponent ,
3099
+ 'No notFoundComponent found. Please set a notFoundComponent on your route or provide a defaultNotFoundComponent to the router.' ,
3100
+ )
3101
+ } catch ( error ) {
3102
+ ; ( error as any ) . invariantSource = 'notFound'
3103
+ }
3095
3104
3096
3105
// Find the match for this route
3097
3106
const matchForRoute = matchesByRouteId [ routeCursor . id ]
0 commit comments