|  | 
|  | 1 | +import type { IntegrationFn } from '@sentry/types'; | 
|  | 2 | +import type { Event, EventHint } from '@sentry/types'; | 
|  | 3 | +import { isError, truncate } from '@sentry/utils'; | 
|  | 4 | +import { defineIntegration } from '../integration'; | 
|  | 5 | + | 
|  | 6 | +interface ZodErrorsOptions { | 
|  | 7 | +  key?: string; | 
|  | 8 | +  limit?: number; | 
|  | 9 | +} | 
|  | 10 | + | 
|  | 11 | +const DEFAULT_LIMIT = 10; | 
|  | 12 | +const INTEGRATION_NAME = 'ZodErrors'; | 
|  | 13 | + | 
|  | 14 | +// Simplified ZodIssue type definition | 
|  | 15 | +interface ZodIssue { | 
|  | 16 | +  path: (string | number)[]; | 
|  | 17 | +  message?: string; | 
|  | 18 | +  expected?: string | number; | 
|  | 19 | +  received?: string | number; | 
|  | 20 | +  unionErrors?: unknown[]; | 
|  | 21 | +  keys?: unknown[]; | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +interface ZodError extends Error { | 
|  | 25 | +  issues: ZodIssue[]; | 
|  | 26 | + | 
|  | 27 | +  get errors(): ZodError['issues']; | 
|  | 28 | +} | 
|  | 29 | + | 
|  | 30 | +function originalExceptionIsZodError(originalException: unknown): originalException is ZodError { | 
|  | 31 | +  return ( | 
|  | 32 | +    isError(originalException) && | 
|  | 33 | +    originalException.name === 'ZodError' && | 
|  | 34 | +    Array.isArray((originalException as ZodError).errors) | 
|  | 35 | +  ); | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | +type SingleLevelZodIssue<T extends ZodIssue> = { | 
|  | 39 | +  [P in keyof T]: T[P] extends string | number | undefined | 
|  | 40 | +    ? T[P] | 
|  | 41 | +    : T[P] extends unknown[] | 
|  | 42 | +      ? string | undefined | 
|  | 43 | +      : unknown; | 
|  | 44 | +}; | 
|  | 45 | + | 
|  | 46 | +/** | 
|  | 47 | + * Formats child objects or arrays to a string | 
|  | 48 | + * That is preserved when sent to Sentry | 
|  | 49 | + */ | 
|  | 50 | +function formatIssueTitle(issue: ZodIssue): SingleLevelZodIssue<ZodIssue> { | 
|  | 51 | +  return { | 
|  | 52 | +    ...issue, | 
|  | 53 | +    path: 'path' in issue && Array.isArray(issue.path) ? issue.path.join('.') : undefined, | 
|  | 54 | +    keys: 'keys' in issue ? JSON.stringify(issue.keys) : undefined, | 
|  | 55 | +    unionErrors: 'unionErrors' in issue ? JSON.stringify(issue.unionErrors) : undefined, | 
|  | 56 | +  }; | 
|  | 57 | +} | 
|  | 58 | + | 
|  | 59 | +/** | 
|  | 60 | + * Zod error message is a stringified version of ZodError.issues | 
|  | 61 | + * This doesn't display well in the Sentry UI. Replace it with something shorter. | 
|  | 62 | + */ | 
|  | 63 | +function formatIssueMessage(zodError: ZodError): string { | 
|  | 64 | +  const errorKeyMap = new Set<string | number | symbol>(); | 
|  | 65 | +  for (const iss of zodError.issues) { | 
|  | 66 | +    if (iss.path) errorKeyMap.add(iss.path[0]); | 
|  | 67 | +  } | 
|  | 68 | +  const errorKeys = Array.from(errorKeyMap); | 
|  | 69 | + | 
|  | 70 | +  return `Failed to validate keys: ${truncate(errorKeys.join(', '), 100)}`; | 
|  | 71 | +} | 
|  | 72 | + | 
|  | 73 | +/** | 
|  | 74 | + * Applies ZodError issues to an event extras and replaces the error message | 
|  | 75 | + */ | 
|  | 76 | +export function applyZodErrorsToEvent(limit: number, event: Event, hint?: EventHint): Event { | 
|  | 77 | +  if ( | 
|  | 78 | +    !event.exception || | 
|  | 79 | +    !event.exception.values || | 
|  | 80 | +    !hint || | 
|  | 81 | +    !hint.originalException || | 
|  | 82 | +    !originalExceptionIsZodError(hint.originalException) || | 
|  | 83 | +    hint.originalException.issues.length === 0 | 
|  | 84 | +  ) { | 
|  | 85 | +    return event; | 
|  | 86 | +  } | 
|  | 87 | + | 
|  | 88 | +  return { | 
|  | 89 | +    ...event, | 
|  | 90 | +    exception: { | 
|  | 91 | +      ...event.exception, | 
|  | 92 | +      values: [ | 
|  | 93 | +        { | 
|  | 94 | +          ...event.exception.values[0], | 
|  | 95 | +          value: formatIssueMessage(hint.originalException), | 
|  | 96 | +        }, | 
|  | 97 | +        ...event.exception.values.slice(1), | 
|  | 98 | +      ], | 
|  | 99 | +    }, | 
|  | 100 | +    extra: { | 
|  | 101 | +      ...event.extra, | 
|  | 102 | +      'zoderror.issues': hint.originalException.errors.slice(0, limit).map(formatIssueTitle), | 
|  | 103 | +    }, | 
|  | 104 | +  }; | 
|  | 105 | +} | 
|  | 106 | + | 
|  | 107 | +const _zodErrorsIntegration = ((options: ZodErrorsOptions = {}) => { | 
|  | 108 | +  const limit = options.limit || DEFAULT_LIMIT; | 
|  | 109 | + | 
|  | 110 | +  return { | 
|  | 111 | +    name: INTEGRATION_NAME, | 
|  | 112 | +    processEvent(originalEvent, hint) { | 
|  | 113 | +      const processedEvent = applyZodErrorsToEvent(limit, originalEvent, hint); | 
|  | 114 | +      return processedEvent; | 
|  | 115 | +    }, | 
|  | 116 | +  }; | 
|  | 117 | +}) satisfies IntegrationFn; | 
|  | 118 | + | 
|  | 119 | +export const zodErrorsIntegration = defineIntegration(_zodErrorsIntegration); | 
0 commit comments