Files
outline/server/logging/sentry.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

132 lines
3.4 KiB
TypeScript

import cluster from "node:cluster";
import type * as SentryNode from "@sentry/node";
import env from "@server/env";
import type { AppContext } from "@server/types";
// Sentry is only loaded in worker processes, as it is comparatively expensive
// and the cluster primary only supervises the forked workers.
const Sentry =
env.SENTRY_DSN && cluster.isWorker ? loadSentry(env.SENTRY_DSN) : undefined;
// oxlint-disable-next-line @typescript-eslint/no-explicit-any
export function requestErrorHandler(error: any, ctx: AppContext) {
// we don't need to report every time a request stops to the bug tracker
if (error.code === "EPIPE" || error.code === "ECONNRESET") {
return;
}
if (Sentry) {
Sentry.withScope(function (scope) {
const requestId = ctx.headers["x-request-id"];
if (requestId) {
scope.setTag("request_id", requestId as string);
}
const authType = ctx.auth?.type ?? undefined;
if (authType) {
scope.setTag("auth_type", authType);
}
const teamId = ctx.auth?.user?.teamId ?? undefined;
if (teamId) {
scope.setTag("team_id", teamId);
}
const userId = ctx.auth?.user?.id ?? undefined;
if (userId) {
scope.setUser({
id: userId,
});
}
scope.setSDKProcessingMetadata({ request: ctx.req });
Sentry.captureException(error);
});
} else if (env.ENVIRONMENT !== "test") {
// oxlint-disable-next-line no-console
console.error(error);
}
}
export default Sentry;
function loadSentry(dsn: string): typeof SentryNode {
// oxlint-disable-next-line typescript/no-require-imports -- kept lazy so Sentry stays off the startup path
const sentry = require("@sentry/node") as typeof SentryNode;
sentry.init({
dsn,
environment: env.ENVIRONMENT,
release: env.RELEASE,
maxBreadcrumbs: 0,
integrations: [
// Request bodies can contain document content and credentials.
sentry.httpIntegration({ maxIncomingRequestBodySize: "none" }),
],
ignoreErrors: [
// These errors are expected in normal running of the application and
// don't need to be reported.
// Validation
"BadRequestError",
"SequelizeValidationError",
"SequelizeEmptyResultError",
"ValidationError",
"ForbiddenError",
// Authentication
"UnauthorizedError",
"TeamDomainRequiredError",
"AuthRedirectError",
"UserSuspendedError",
"TooManyRequestsError",
// Client disconnects
"Premature close",
],
beforeSend(event) {
scrubRequest(event);
try {
switch (event.level) {
case "warning":
// Sample warnings to reduce noise
if (Math.random() < 0.1) {
return null;
}
break;
}
return event;
} catch (_) {
return event;
}
},
});
return sentry;
}
const SensitiveHeaderPattern =
/auth|cookie|token|secret|key|session|signature/i;
/**
* Removes the request body, cookies, and sensitive headers from an event.
*
* @param event the event to scrub in place.
*/
function scrubRequest(event: SentryNode.ErrorEvent) {
const { request } = event;
if (!request) {
return;
}
delete request.data;
delete request.cookies;
if (request.headers) {
request.headers = Object.fromEntries(
Object.entries(request.headers).filter(
([name]) => !SensitiveHeaderPattern.test(name)
)
);
}
}