Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/core/src/integrations/zoderrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface ZodIssue {
expected?: unknown;
received?: unknown;
unionErrors?: unknown[];
errors?: unknown[];
keys?: unknown[];
invalid_literal?: unknown;
}
Expand All @@ -43,7 +44,8 @@ interface ZodError extends Error {
function originalExceptionIsZodError(originalException: unknown): originalException is ZodError {
return (
isError(originalException) &&
originalException.name === 'ZodError' &&
// `zod/mini` (Zod 4) throws `$ZodError`
(originalException.name === 'ZodError' || originalException.name === '$ZodError') &&
Array.isArray((originalException as ZodError).issues)
);
}
Expand Down Expand Up @@ -75,6 +77,8 @@ export function flattenIssue(issue: ZodIssue): SingleLevelZodIssue<ZodIssue> {
path: 'path' in issue && Array.isArray(issue.path) ? issue.path.join('.') : undefined,
keys: 'keys' in issue ? JSON.stringify(issue.keys) : undefined,
unionErrors: 'unionErrors' in issue ? JSON.stringify(issue.unionErrors) : undefined,
// Zod 4 nests union failures under `errors` instead of `unionErrors`
...('errors' in issue ? { errors: JSON.stringify(issue.errors) } : {}),
};
}

Expand Down
40 changes: 40 additions & 0 deletions packages/core/test/lib/integrations/zoderrrors.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, expect, it, test } from 'vitest';
import { z } from 'zod';
import { z as z4 } from 'zod/v4';
import * as z4mini from 'zod/v4/mini';
import {
applyZodErrorsToEvent,
flattenIssue,
Expand Down Expand Up @@ -516,3 +518,41 @@ describe('formatIssueMessage()', () => {
});
});
});

describe('Zod 4 errors', () => {
function createEvent(originalException: Error): Event {
return { exception: { values: [{ type: originalException.name, value: originalException.message }] } };
}

test('processes errors thrown by `zod/mini`', () => {
const zodError = z4mini.object({ foo: z4mini.string() }).safeParse({ foo: 123 }).error;
if (zodError === undefined) {
throw new Error('zodError is undefined');
}
expect(zodError.name).toBe('$ZodError');

const processedEvent = applyZodErrorsToEvent(100, false, createEvent(zodError), { originalException: zodError });

expect(processedEvent.exception?.values?.[0]?.value).toBe('Failed to validate keys: foo');
expect(processedEvent.extra?.['zoderror.issues']).toEqual([
expect.objectContaining({ code: 'invalid_type', expected: 'string', path: 'foo' }),
]);
});

test('flattens nested union errors to a string', () => {
const zodError = z4.object({ value: z4.union([z4.string(), z4.number()]) }).safeParse({ value: true }).error;
if (zodError === undefined) {
throw new Error('zodError is undefined');
}

const processedEvent = applyZodErrorsToEvent(100, false, createEvent(zodError), { originalException: zodError });

expect(processedEvent.exception?.values?.[0]?.value).toBe('Failed to validate keys: value');
const [issue] = processedEvent.extra?.['zoderror.issues'] as Array<Record<string, unknown>>;
expect(issue).toEqual(expect.objectContaining({ code: 'invalid_union', path: 'value' }));
expect(JSON.parse(issue?.errors as string)).toEqual([
[expect.objectContaining({ code: 'invalid_type', expected: 'string' })],
[expect.objectContaining({ code: 'invalid_type', expected: 'number' })],
]);
});
});
Loading