@@ -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,21 @@ 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
+ console . log ( {
2264
+ isRedirect : isRedirect ( err ) ,
2265
+ isNotFound : isNotFound ( err ) ,
2266
+ isError,
2267
+ } )
2268
+ if ( isRedirect ( err ) || isNotFound ( err ) || isError ) {
2262
2269
updateMatch ( match . id , ( prev ) => ( {
2263
2270
...prev ,
2264
2271
status : isRedirect ( err )
@@ -2272,8 +2279,8 @@ export class RouterCore<
2272
2279
loaderPromise : undefined ,
2273
2280
} ) )
2274
2281
2275
- if ( ! ( err as any ) . routeId ) {
2276
- ; ( err as any ) . routeId = match . routeId
2282
+ if ( ! err . routeId ) {
2283
+ err . routeId = match . routeId
2277
2284
}
2278
2285
2279
2286
match . beforeLoadPromise ?. resolve ( )
@@ -2293,6 +2300,11 @@ export class RouterCore<
2293
2300
match : this . getMatch ( match . id ) ! ,
2294
2301
} )
2295
2302
throw err
2303
+ } else if ( isError ) {
2304
+ this . serverSsr ?. onMatchSettled ( {
2305
+ router : this ,
2306
+ match : this . getMatch ( match . id ) ! ,
2307
+ } )
2296
2308
}
2297
2309
}
2298
2310
}
@@ -2318,13 +2330,13 @@ export class RouterCore<
2318
2330
2319
2331
err . routerCode = routerCode
2320
2332
firstBadMatchIndex = firstBadMatchIndex ?? index
2321
- handleRedirectAndNotFound ( this . getMatch ( matchId ) ! , err )
2333
+ handleRouteError ( this . getMatch ( matchId ) ! , err )
2322
2334
2323
2335
try {
2324
2336
route . options . onError ?.( err )
2325
2337
} catch ( errorHandlerErr ) {
2326
2338
err = errorHandlerErr
2327
- handleRedirectAndNotFound ( this . getMatch ( matchId ) ! , err )
2339
+ handleRouteError ( this . getMatch ( matchId ) ! , err )
2328
2340
}
2329
2341
2330
2342
updateMatch ( matchId , ( prev ) => {
@@ -2523,7 +2535,7 @@ export class RouterCore<
2523
2535
await prevLoaderPromise
2524
2536
const match = this . getMatch ( matchId ) !
2525
2537
if ( match . error ) {
2526
- handleRedirectAndNotFound ( match , match . error )
2538
+ handleRouteError ( match , match . error )
2527
2539
}
2528
2540
} else {
2529
2541
const parentMatchPromise = matchPromises [ index - 1 ] as any
@@ -2643,10 +2655,7 @@ export class RouterCore<
2643
2655
const loaderData =
2644
2656
await route . options . loader ?.( getLoaderContext ( ) )
2645
2657
2646
- handleRedirectAndNotFound (
2647
- this . getMatch ( matchId ) ! ,
2648
- loaderData ,
2649
- )
2658
+ handleRouteError ( this . getMatch ( matchId ) ! , loaderData )
2650
2659
2651
2660
// Lazy option can modify the route options,
2652
2661
// so we need to wait for it to resolve before
@@ -2675,13 +2684,13 @@ export class RouterCore<
2675
2684
2676
2685
await potentialPendingMinPromise ( )
2677
2686
2678
- handleRedirectAndNotFound ( this . getMatch ( matchId ) ! , e )
2687
+ handleRouteError ( this . getMatch ( matchId ) ! , e )
2679
2688
2680
2689
try {
2681
2690
route . options . onError ?.( e )
2682
2691
} catch ( onErrorError ) {
2683
2692
error = onErrorError
2684
- handleRedirectAndNotFound (
2693
+ handleRouteError (
2685
2694
this . getMatch ( matchId ) ! ,
2686
2695
onErrorError ,
2687
2696
)
@@ -2710,7 +2719,7 @@ export class RouterCore<
2710
2719
} ) )
2711
2720
executeHead ( )
2712
2721
} )
2713
- handleRedirectAndNotFound ( this . getMatch ( matchId ) ! , err )
2722
+ handleRouteError ( this . getMatch ( matchId ) ! , err )
2714
2723
}
2715
2724
}
2716
2725
@@ -3088,10 +3097,15 @@ export class RouterCore<
3088
3097
}
3089
3098
3090
3099
// 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
- )
3100
+ // generic Error instead of a NotFoundError when notFoundComponent doesn't exist, is this a bug?
3101
+ try {
3102
+ invariant (
3103
+ routeCursor . options . notFoundComponent ,
3104
+ 'No notFoundComponent found. Please set a notFoundComponent on your route or provide a defaultNotFoundComponent to the router.' ,
3105
+ )
3106
+ } catch ( error ) {
3107
+ ; ( error as any ) . invariantSource = 'notFound'
3108
+ }
3095
3109
3096
3110
// Find the match for this route
3097
3111
const matchForRoute = matchesByRouteId [ routeCursor . id ]
0 commit comments