Skip to content

Commit f2eac66

Browse files
committed
Minor cleanup
1 parent 263b3b1 commit f2eac66

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

packages/react-router/lib/router/router.ts

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5421,18 +5421,17 @@ function runServerMiddlewarePipeline(
54215421
return runMiddlewarePipeline(
54225422
args,
54235423
handler,
5424-
// Upgrade returned data() values to real Responses
5425-
(result) =>
5426-
isDataWithResponseInit(result)
5427-
? dataWithResponseInitToResponse(result)
5428-
: result,
5424+
processResult,
54295425
isResponse,
5430-
async (error, routeId, nextResult) => {
5431-
// Convert thrown data() values to ErrorResponses
5432-
let response = await errorHandler(error, routeId, nextResult);
5433-
return response;
5434-
},
5426+
errorHandler,
54355427
);
5428+
5429+
// Upgrade returned data() values to real Responses
5430+
function processResult(result: any) {
5431+
return isDataWithResponseInit(result)
5432+
? dataWithResponseInitToResponse(result)
5433+
: result;
5434+
}
54365435
}
54375436

54385437
function runClientMiddlewarePipeline(
@@ -5447,35 +5446,42 @@ function runClientMiddlewarePipeline(
54475446
handler,
54485447
(r) => r, // No post-processing needed on the client
54495448
isDataStrategyResults,
5450-
(error, routeId, nextResult) => {
5451-
if (nextResult) {
5452-
return Promise.resolve(
5453-
Object.assign(nextResult.value, {
5454-
[routeId]: { type: "error", result: error },
5455-
}),
5456-
);
5457-
} else {
5458-
// We never even got to the handlers, so we might not have data for new routes.
5459-
// Find the boundary at or above the source of the middleware error or the
5460-
// highest route that needs to load - we can't render any UI below that since
5461-
// we won't have valid loader data.
5462-
let { matches } = args;
5463-
let maxBoundaryIdx = Math.min(
5464-
// Throwing route
5465-
matches.findIndex((m) => m.route.id === routeId) || 0,
5466-
// or the shallowest route that needs to load data
5467-
matches.findIndex((m) => m.unstable_shouldCallHandler()) || 0,
5468-
);
5469-
let boundaryRouteId = findNearestBoundary(
5470-
matches,
5471-
matches[maxBoundaryIdx].route.id,
5472-
).route.id;
5473-
return Promise.resolve({
5474-
[boundaryRouteId]: { type: "error", result: error },
5475-
});
5476-
}
5477-
},
5449+
errorHandler,
54785450
);
5451+
5452+
// Handle error bubbling on the client
5453+
function errorHandler(
5454+
error: unknown,
5455+
routeId: string,
5456+
nextResult: { value: Record<string, DataStrategyResult> } | undefined,
5457+
): Promise<Record<string, DataStrategyResult>> {
5458+
if (nextResult) {
5459+
return Promise.resolve(
5460+
Object.assign(nextResult.value, {
5461+
[routeId]: { type: "error", result: error },
5462+
}),
5463+
);
5464+
} else {
5465+
// We never even got to the handlers, so we might not have data for new routes.
5466+
// Find the boundary at or above the source of the middleware error or the
5467+
// highest route that needs to load - we can't render any UI below that since
5468+
// we won't have valid loader data.
5469+
let { matches } = args;
5470+
let maxBoundaryIdx = Math.min(
5471+
// Throwing route
5472+
matches.findIndex((m) => m.route.id === routeId) || 0,
5473+
// or the shallowest route that needs to load data
5474+
matches.findIndex((m) => m.unstable_shouldCallHandler()) || 0,
5475+
);
5476+
let boundaryRouteId = findNearestBoundary(
5477+
matches,
5478+
matches[maxBoundaryIdx].route.id,
5479+
).route.id;
5480+
return Promise.resolve({
5481+
[boundaryRouteId]: { type: "error", result: error },
5482+
});
5483+
}
5484+
}
54795485
}
54805486

54815487
async function runMiddlewarePipeline<Result>(
@@ -5579,19 +5585,9 @@ async function callRouteMiddleware<Result>(
55795585
};
55805586

55815587
try {
5582-
let value = await middleware(
5583-
{
5584-
request: args.request,
5585-
params: args.params,
5586-
context: args.context,
5587-
},
5588-
next,
5589-
);
5590-
5591-
let result =
5592-
typeof value === "undefined" ? undefined : processResult(value);
5588+
let value = await middleware(args, next);
5589+
let result = value != null ? processResult(value) : undefined;
55935590

5594-
// Handle calling next() if needed and returning the proper result
55955591
if (isResult(result)) {
55965592
// Use short circuit values of the proper type without having called next()
55975593
return result;

0 commit comments

Comments
 (0)