Skip to content

Commit dc489e6

Browse files
committed
remove version checks in utils
1 parent dcc15b1 commit dc489e6

File tree

2 files changed

+29
-72
lines changed

2 files changed

+29
-72
lines changed

packages/remix/src/utils/errors.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import type { AppData, DataFunctionArgs, EntryContext, HandleDocumentRequestFunction } from '@remix-run/node';
1+
import type {
2+
ActionFunctionArgs,
3+
EntryContext,
4+
HandleDocumentRequestFunction,
5+
LoaderFunctionArgs,
6+
} from '@remix-run/node';
27
import {
38
addExceptionMechanism,
49
captureException,
510
getClient,
611
handleCallbackErrors,
7-
isPrimitive,
812
logger,
913
objectify,
1014
winterCGRequestToRequestData,
@@ -22,19 +26,13 @@ import type { DataFunction, RemixRequest } from './vendor/types';
2226
* @param err The error to capture.
2327
* @param name The name of the origin function.
2428
* @param request The request object.
25-
* @param isRemixV2 Whether the error is from Remix v2 or not. Default is `true`.
2629
*
2730
* @returns A promise that resolves when the exception is captured.
2831
*/
29-
export async function captureRemixServerException(
30-
err: unknown,
31-
name: string,
32-
request: Request,
33-
isRemixV2: boolean = true,
34-
): Promise<void> {
32+
export async function captureRemixServerException(err: unknown, name: string, request: Request): Promise<void> {
3533
// Skip capturing if the thrown error is not a 5xx response
3634
// https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders
37-
if (isRemixV2 && isRouteErrorResponse(err) && err.status < 500) {
35+
if (isRouteErrorResponse(err) && err.status < 500) {
3836
return;
3937
}
4038

@@ -82,7 +80,6 @@ export async function captureRemixServerException(
8280
*
8381
* @param origDocumentRequestFunction The original `HandleDocumentRequestFunction`.
8482
* @param requestContext The request context.
85-
* @param isRemixV2 Whether the Remix version is v2 or not.
8683
*
8784
* @returns The wrapped `HandleDocumentRequestFunction`.
8885
*/
@@ -96,7 +93,6 @@ export function errorHandleDocumentRequestFunction(
9693
context: EntryContext;
9794
loadContext?: Record<string, unknown>;
9895
},
99-
isRemixV2: boolean,
10096
): HandleDocumentRequestFunction {
10197
const { request, responseStatusCode, responseHeaders, context, loadContext } = requestContext;
10298

@@ -105,14 +101,6 @@ export function errorHandleDocumentRequestFunction(
105101
return origDocumentRequestFunction.call(this, request, responseStatusCode, responseHeaders, context, loadContext);
106102
},
107103
err => {
108-
// This exists to capture the server-side rendering errors on Remix v1
109-
// On Remix v2, we capture SSR errors at `handleError`
110-
// We also skip primitives here, as we can't dedupe them, and also we don't expect any primitive SSR errors.
111-
if (!isRemixV2 && !isPrimitive(err)) {
112-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
113-
captureRemixServerException(err, 'documentRequest', request, isRemixV2);
114-
}
115-
116104
throw err;
117105
},
118106
);
@@ -125,7 +113,6 @@ export function errorHandleDocumentRequestFunction(
125113
* @param origFn The original `DataFunction`.
126114
* @param name The name of the function.
127115
* @param args The arguments of the function.
128-
* @param isRemixV2 Whether the Remix version is v2 or not.
129116
* @param span The span to store the form data keys.
130117
*
131118
* @returns The wrapped `DataFunction`.
@@ -134,10 +121,9 @@ export async function errorHandleDataFunction(
134121
this: unknown,
135122
origFn: DataFunction,
136123
name: string,
137-
args: DataFunctionArgs,
138-
isRemixV2: boolean,
124+
args: ActionFunctionArgs | LoaderFunctionArgs,
139125
span?: Span,
140-
): Promise<Response | AppData> {
126+
): Promise<Response> {
141127
return handleCallbackErrors(
142128
async () => {
143129
if (name === 'action' && span) {
@@ -151,12 +137,11 @@ export async function errorHandleDataFunction(
151137
return origFn.call(this, args);
152138
},
153139
err => {
154-
// On Remix v2, we capture all unexpected errors (except the `Route Error Response`s / Thrown Responses) in `handleError` function.
140+
// We capture all unexpected errors (except the `Route Error Response`s / Thrown Responses) in `handleError` function.
155141
// This is both for consistency and also avoid duplicates such as primitives like `string` or `number` being captured twice.
156-
// Remix v1 does not have a `handleError` function, so we capture all errors here.
157-
if (isRemixV2 ? isResponse(err) : true) {
142+
if (isResponse(err)) {
158143
// eslint-disable-next-line @typescript-eslint/no-floating-promises
159-
captureRemixServerException(err, name, args.request, true);
144+
captureRemixServerException(err, name, args.request);
160145
}
161146

162147
throw err;

packages/remix/src/utils/instrumentServer.ts

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
} from '@sentry/core';
2323
import { DEBUG_BUILD } from './debug-build';
2424
import { captureRemixServerException, errorHandleDataFunction, errorHandleDocumentRequestFunction } from './errors';
25-
import { getFutureFlagsServer, getRemixVersionFromBuild } from './futureFlags';
2625
import type { RemixOptions } from './remixOptions';
2726
import { createRoutes, getTransactionName } from './utils';
2827
import { extractData, isDeferredData, isResponse, isRouteErrorResponse, json } from './vendor/response';
@@ -33,7 +32,6 @@ import type {
3332
DataFunction,
3433
DataFunctionArgs,
3534
EntryContext,
36-
FutureConfig,
3735
HandleDocumentRequestFunction,
3836
RemixRequest,
3937
RequestHandler,
@@ -42,8 +40,6 @@ import type {
4240
ServerRouteManifest,
4341
} from './vendor/types';
4442

45-
let FUTURE_FLAGS: FutureConfig | undefined;
46-
4743
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
4844
function isRedirectResponse(response: Response): boolean {
4945
return redirectStatusCodes.has(response.status);
@@ -67,7 +63,6 @@ export function sentryHandleError(err: unknown, { request }: DataFunctionArgs):
6763
// We don't want to capture them twice.
6864
// This function is only for capturing unhandled server-side exceptions.
6965
// https://remix.run/docs/en/main/file-conventions/entry.server#thrown-responses
70-
// https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders
7166
if (isResponse(err) || isRouteErrorResponse(err)) {
7267
return;
7368
}
@@ -99,7 +94,7 @@ export function wrapHandleErrorWithSentry(
9994
};
10095
}
10196

102-
function makeWrappedDocumentRequestFunction(autoInstrumentRemix?: boolean, remixVersion?: number) {
97+
function makeWrappedDocumentRequestFunction(autoInstrumentRemix?: boolean) {
10398
return function (origDocumentRequestFunction: HandleDocumentRequestFunction): HandleDocumentRequestFunction {
10499
return async function (
105100
this: unknown,
@@ -117,8 +112,6 @@ function makeWrappedDocumentRequestFunction(autoInstrumentRemix?: boolean, remix
117112
loadContext,
118113
};
119114

120-
const isRemixV2 = FUTURE_FLAGS?.v2_errorBoundary || remixVersion === 2;
121-
122115
if (!autoInstrumentRemix) {
123116
const activeSpan = getActiveSpan();
124117
const rootSpan = activeSpan && getRootSpan(activeSpan);
@@ -139,21 +132,11 @@ function makeWrappedDocumentRequestFunction(autoInstrumentRemix?: boolean, remix
139132
},
140133
},
141134
() => {
142-
return errorHandleDocumentRequestFunction.call(
143-
this,
144-
origDocumentRequestFunction,
145-
documentRequestContext,
146-
isRemixV2,
147-
);
135+
return errorHandleDocumentRequestFunction.call(this, origDocumentRequestFunction, documentRequestContext);
148136
},
149137
);
150138
} else {
151-
return errorHandleDocumentRequestFunction.call(
152-
this,
153-
origDocumentRequestFunction,
154-
documentRequestContext,
155-
isRemixV2,
156-
);
139+
return errorHandleDocumentRequestFunction.call(this, origDocumentRequestFunction, documentRequestContext);
157140
}
158141
};
159142
};
@@ -163,12 +146,9 @@ function makeWrappedDataFunction(
163146
origFn: DataFunction,
164147
id: string,
165148
name: 'action' | 'loader',
166-
remixVersion: number,
167149
autoInstrumentRemix?: boolean,
168150
): DataFunction {
169151
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
170-
const isRemixV2 = FUTURE_FLAGS?.v2_errorBoundary || remixVersion === 2;
171-
172152
if (!autoInstrumentRemix) {
173153
return startSpan(
174154
{
@@ -180,25 +160,25 @@ function makeWrappedDataFunction(
180160
},
181161
},
182162
(span: Span) => {
183-
return errorHandleDataFunction.call(this, origFn, name, args, isRemixV2, span);
163+
return errorHandleDataFunction.call(this, origFn, name, args, span);
184164
},
185165
);
186166
} else {
187-
return errorHandleDataFunction.call(this, origFn, name, args, isRemixV2);
167+
return errorHandleDataFunction.call(this, origFn, name, args);
188168
}
189169
};
190170
}
191171

192172
const makeWrappedAction =
193-
(id: string, remixVersion: number, autoInstrumentRemix?: boolean) =>
173+
(id: string, autoInstrumentRemix?: boolean) =>
194174
(origAction: DataFunction): DataFunction => {
195-
return makeWrappedDataFunction(origAction, id, 'action', remixVersion, autoInstrumentRemix);
175+
return makeWrappedDataFunction(origAction, id, 'action', autoInstrumentRemix);
196176
};
197177

198178
const makeWrappedLoader =
199-
(id: string, remixVersion: number, autoInstrumentRemix?: boolean) =>
179+
(id: string, autoInstrumentRemix?: boolean) =>
200180
(origLoader: DataFunction): DataFunction => {
201-
return makeWrappedDataFunction(origLoader, id, 'loader', remixVersion, autoInstrumentRemix);
181+
return makeWrappedDataFunction(origLoader, id, 'loader', autoInstrumentRemix);
202182
};
203183

204184
function getTraceAndBaggage(): {
@@ -217,7 +197,7 @@ function getTraceAndBaggage(): {
217197
return {};
218198
}
219199

220-
function makeWrappedRootLoader(remixVersion: number) {
200+
function makeWrappedRootLoader() {
221201
return function (origLoader: DataFunction): DataFunction {
222202
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
223203
const res = await origLoader.call(this, args);
@@ -226,7 +206,6 @@ function makeWrappedRootLoader(remixVersion: number) {
226206
if (isDeferredData(res)) {
227207
res.data['sentryTrace'] = traceAndBaggage.sentryTrace;
228208
res.data['sentryBaggage'] = traceAndBaggage.sentryBaggage;
229-
res.data['remixVersion'] = remixVersion;
230209

231210
return res;
232211
}
@@ -243,7 +222,7 @@ function makeWrappedRootLoader(remixVersion: number) {
243222

244223
if (typeof data === 'object') {
245224
return json(
246-
{ ...data, ...traceAndBaggage, remixVersion },
225+
{ ...data, ...traceAndBaggage },
247226
{
248227
headers: res.headers,
249228
statusText: res.statusText,
@@ -257,7 +236,7 @@ function makeWrappedRootLoader(remixVersion: number) {
257236
}
258237
}
259238

260-
return { ...res, ...traceAndBaggage, remixVersion };
239+
return { ...res, ...traceAndBaggage };
261240
};
262241
};
263242
}
@@ -351,7 +330,6 @@ function wrapRequestHandler(
351330

352331
function instrumentBuildCallback(build: ServerBuild, autoInstrumentRemix: boolean): ServerBuild {
353332
const routes: ServerRouteManifest = {};
354-
const remixVersion = getRemixVersionFromBuild(build);
355333
const wrappedEntry = { ...build.entry, module: { ...build.entry.module } };
356334

357335
// Not keeping boolean flags like it's done for `requestHandler` functions,
@@ -360,20 +338,20 @@ function instrumentBuildCallback(build: ServerBuild, autoInstrumentRemix: boolea
360338
// We should be able to wrap them, as they may not be wrapped before.
361339
const defaultExport = wrappedEntry.module.default as undefined | WrappedFunction;
362340
if (defaultExport && !defaultExport.__sentry_original__) {
363-
fill(wrappedEntry.module, 'default', makeWrappedDocumentRequestFunction(autoInstrumentRemix, remixVersion));
341+
fill(wrappedEntry.module, 'default', makeWrappedDocumentRequestFunction(autoInstrumentRemix));
364342
}
365343

366344
for (const [id, route] of Object.entries(build.routes)) {
367345
const wrappedRoute = { ...route, module: { ...route.module } };
368346

369347
const routeAction = wrappedRoute.module.action as undefined | WrappedFunction;
370348
if (routeAction && !routeAction.__sentry_original__) {
371-
fill(wrappedRoute.module, 'action', makeWrappedAction(id, remixVersion, autoInstrumentRemix));
349+
fill(wrappedRoute.module, 'action', makeWrappedAction(id, autoInstrumentRemix));
372350
}
373351

374352
const routeLoader = wrappedRoute.module.loader as undefined | WrappedFunction;
375353
if (routeLoader && !routeLoader.__sentry_original__) {
376-
fill(wrappedRoute.module, 'loader', makeWrappedLoader(id, remixVersion, autoInstrumentRemix));
354+
fill(wrappedRoute.module, 'loader', makeWrappedLoader(id, autoInstrumentRemix));
377355
}
378356

379357
// Entry module should have a loader function to provide `sentry-trace` and `baggage`
@@ -384,7 +362,7 @@ function instrumentBuildCallback(build: ServerBuild, autoInstrumentRemix: boolea
384362
}
385363

386364
// We want to wrap the root loader regardless of whether it's already wrapped before.
387-
fill(wrappedRoute.module, 'loader', makeWrappedRootLoader(remixVersion));
365+
fill(wrappedRoute.module, 'loader', makeWrappedRootLoader());
388366
}
389367

390368
routes[id] = wrappedRoute;
@@ -409,19 +387,13 @@ export function instrumentBuild(
409387

410388
if (resolvedBuild instanceof Promise) {
411389
return resolvedBuild.then(build => {
412-
FUTURE_FLAGS = getFutureFlagsServer(build);
413-
414390
return instrumentBuildCallback(build, autoInstrumentRemix);
415391
});
416392
} else {
417-
FUTURE_FLAGS = getFutureFlagsServer(resolvedBuild);
418-
419393
return instrumentBuildCallback(resolvedBuild, autoInstrumentRemix);
420394
}
421395
};
422396
} else {
423-
FUTURE_FLAGS = getFutureFlagsServer(build);
424-
425397
return instrumentBuildCallback(build, autoInstrumentRemix);
426398
}
427399
}

0 commit comments

Comments
 (0)