UNPKG

@sentry/core

Version:
127 lines (126 loc) • 4.33 kB
import { defineIntegration } from "../integration.js"; import { isError } from "../utils/is.js"; import { truncate } from "../utils/string.js"; // src/integrations/zoderrors.ts const DEFAULT_LIMIT = 10; const INTEGRATION_NAME = "ZodErrors"; function originalExceptionIsZodError(originalException) { return isError(originalException) && (originalException.name === "ZodError" || originalException.name === "$ZodError") && Array.isArray(originalException.issues); } /** * Formats child objects or arrays to a string * that is preserved when sent to Sentry. * * Without this, we end up with something like this in Sentry: * * [ * [Object], * [Object], * [Object], * [Object] * ] */ function flattenIssue(issue) { return { ...issue, path: "path" in issue && Array.isArray(issue.path) ? issue.path.join(".") : void 0, keys: "keys" in issue ? JSON.stringify(issue.keys) : void 0, unionErrors: "unionErrors" in issue ? JSON.stringify(issue.unionErrors) : void 0, ..."errors" in issue ? { errors: JSON.stringify(issue.errors) } : {} }; } /** * Takes ZodError issue path array and returns a flattened version as a string. * This makes it easier to display paths within a Sentry error message. * * Array indexes are normalized to reduce duplicate entries * * @param path ZodError issue path * @returns flattened path * * @example * flattenIssuePath([0, 'foo', 1, 'bar']) // -> '<array>.foo.<array>.bar' */ function flattenIssuePath(path) { return path.map((p) => { if (typeof p === "number") return "<array>"; else return p; }).join("."); } /** * Zod error message is a stringified version of ZodError.issues * This doesn't display well in the Sentry UI. Replace it with something shorter. */ function formatIssueMessage(zodError) { const errorKeyMap = /* @__PURE__ */ new Set(); for (const iss of zodError.issues) { const issuePath = flattenIssuePath(iss.path); if (issuePath.length > 0) errorKeyMap.add(issuePath); } const errorKeys = Array.from(errorKeyMap); if (errorKeys.length === 0) { let rootExpectedType = "variable"; if (zodError.issues.length > 0) { const iss = zodError.issues[0]; if (iss !== void 0 && "expected" in iss && typeof iss.expected === "string") rootExpectedType = iss.expected; } return `Failed to validate ${rootExpectedType}`; } return `Failed to validate keys: ${truncate(errorKeys.join(", "), 100)}`; } /** * Applies ZodError issues to an event extra and replaces the error message */ function applyZodErrorsToEvent(limit, saveZodIssuesAsAttachment = false, event, hint) { if (!event.exception?.values || !hint.originalException || !originalExceptionIsZodError(hint.originalException) || hint.originalException.issues.length === 0) return event; try { const flattenedIssues = (saveZodIssuesAsAttachment ? hint.originalException.issues : hint.originalException.issues.slice(0, limit)).map(flattenIssue); if (saveZodIssuesAsAttachment) { if (!Array.isArray(hint.attachments)) hint.attachments = []; hint.attachments.push({ filename: "zod_issues.json", data: JSON.stringify({ issues: flattenedIssues }) }); } return { ...event, exception: { ...event.exception, values: [{ ...event.exception.values[0], value: formatIssueMessage(hint.originalException) }, ...event.exception.values.slice(1)] }, extra: { ...event.extra, "zoderror.issues": flattenedIssues.slice(0, limit) } }; } catch (e) { return { ...event, extra: { ...event.extra, "zoderrors sentry integration parse error": { message: "an exception was thrown while processing ZodError within applyZodErrorsToEvent()", error: e instanceof Error ? `${e.name}: ${e.message}\n${e.stack}` : "unknown" } } }; } } const _zodErrorsIntegration = ((options = {}) => { const limit = options.limit ?? DEFAULT_LIMIT; return { name: INTEGRATION_NAME, processEvent(originalEvent, hint) { return applyZodErrorsToEvent(limit, options.saveZodIssuesAsAttachment, originalEvent, hint); } }; }); /** * Sentry integration to process Zod errors, making them easier to work with in Sentry. */ const zodErrorsIntegration = defineIntegration(_zodErrorsIntegration); export { applyZodErrorsToEvent, flattenIssue, flattenIssuePath, formatIssueMessage, zodErrorsIntegration }; //# sourceMappingURL=zoderrors.js.map