@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
68 lines (67 loc) • 2.55 kB
JavaScript
import { getClient, withIsolationScope } from "./currentScopes.js";
import { captureException } from "./exports.js";
import { startSpanManual } from "./tracing/trace.js";
import { normalize } from "./utils/normalize.js";
import { setNormalizationDepthOverrideHint } from "./utils/normalizationHints.js";
import { RPC_METHOD, RPC_SYSTEM_NAME, SENTRY_OP, SENTRY_ORIGIN, SENTRY_SEGMENT_NAME_SOURCE, TRPC_PROCEDURE_PATH, TRPC_PROCEDURE_TYPE } from "@sentry/conventions/attributes";
import { RPC } from "@sentry/conventions/op";
// src/trpc.ts
const trpcCaptureContext = { mechanism: {
handled: false,
type: "auto.rpc.trpc.middleware"
} };
function captureIfError(nextResult) {
if (typeof nextResult === "object" && nextResult !== null && "ok" in nextResult && !nextResult.ok && "error" in nextResult) captureException(nextResult.error, trpcCaptureContext);
}
/**
* Sentry tRPC middleware that captures errors and creates spans for tRPC procedures.
*/
function trpcMiddleware(options = {}) {
return async function(opts) {
const { path, type, next, rawInput, getRawInput } = opts;
const client = getClient();
const clientOptions = client?.getOptions();
const dataCollection = client?.getDataCollectionOptions();
const trpcContext = {
procedure_path: path,
procedure_type: type
};
setNormalizationDepthOverrideHint(trpcContext, 1 + (clientOptions?.normalizeDepth ?? 5));
if (options.attachRpcInput !== void 0 ? options.attachRpcInput : dataCollection?.httpBodies.includes("incomingRequest")) {
if (rawInput !== void 0) trpcContext.input = normalize(rawInput);
if (getRawInput !== void 0 && typeof getRawInput === "function") try {
const rawRes = await getRawInput();
trpcContext.input = normalize(rawRes);
} catch {}
}
return withIsolationScope((scope) => {
scope.setContext("trpc", trpcContext);
return startSpanManual({
name: `trpc/${path}`,
attributes: {
[SENTRY_OP]: RPC,
[SENTRY_SEGMENT_NAME_SOURCE]: "route",
[SENTRY_ORIGIN]: "auto.rpc.trpc",
[RPC_SYSTEM_NAME]: "trpc",
[RPC_METHOD]: String(path),
[TRPC_PROCEDURE_PATH]: String(path),
[TRPC_PROCEDURE_TYPE]: String(type)
},
forceTransaction: !!options.forceTransaction
}, async (span) => {
try {
const nextResult = await next();
captureIfError(nextResult);
span.end();
return nextResult;
} catch (e) {
captureException(e, trpcCaptureContext);
span.end();
throw e;
}
});
});
};
}
export { trpcMiddleware };
//# sourceMappingURL=trpc.js.map