Skip to content

Commit 870ad77

Browse files
committed
Abbreviate too lengthy InconsistentResponseErrors and show what Assembly they belong to
1 parent 100f5ca commit 870ad77

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/Transloadit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ export class Transloadit {
399399
const parsedResult = zodParseWithContext(assemblyStatusSchema, rawResult)
400400
if (!parsedResult.success) {
401401
const err = new InconsistentResponseError(
402-
`The API responded with data that does not match the expected schema.\n${parsedResult.humanReadable}`
402+
`The API responded with data that does not match the expected schema while cancelling Assembly: ${assemblyId}.\n${parsedResult.humanReadable}`
403403
)
404404
// eslint-disable-next-line no-console
405405
console.error(
@@ -526,7 +526,7 @@ export class Transloadit {
526526

527527
if (!parsedResult.success) {
528528
const err = new InconsistentResponseError(
529-
`The API responded with data that does not match the expected schema.\n${parsedResult.humanReadable}`
529+
`The API responded with data that does not match the expected schema while getting Assembly: ${assemblyId}.\n${parsedResult.humanReadable}`
530530
)
531531
// eslint-disable-next-line no-console
532532
console.error(

src/alphalib/zodParseWithContext.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,25 @@ export function zodParseWithContext<T extends z.ZodType>(
161161
// <-- Add handling for invalid_type here -->
162162
else if (issue.code === 'invalid_type') {
163163
const received = issue.received === 'undefined' ? 'missing' : issue.received
164-
// Get the actual value for context
165-
const actualValue = getByPath(parentObj, nestedPath) // Get value from parent context
164+
const actualValue = getByPath(parentObj, nestedPath)
166165
const actualValueStr =
167166
typeof actualValue === 'object' && actualValue !== null
168167
? JSON.stringify(actualValue)
169168
: String(actualValue)
170-
// Simple message not relying on issue.expected
169+
170+
let expectedOutput = String(issue.expected)
171+
const MAX_EXPECTED_TO_SHOW = 3
172+
if (typeof issue.expected === 'string' && issue.expected.includes(' | ')) {
173+
const expectedValues = issue.expected.split(' | ')
174+
if (expectedValues.length > MAX_EXPECTED_TO_SHOW) {
175+
const shownValues = expectedValues.slice(0, MAX_EXPECTED_TO_SHOW).join(' | ')
176+
const remainingCount = expectedValues.length - MAX_EXPECTED_TO_SHOW
177+
expectedOutput = `${shownValues} | .. or ${remainingCount} others ..`
178+
}
179+
}
180+
171181
collectedMessages[nestedPath].push(
172-
`got invalid type: ${received} (value: \`${actualValueStr}\`, expected: ${issue.expected})`,
182+
`got invalid type: ${received} (value: \`${actualValueStr}\`, expected: ${expectedOutput})`,
173183
)
174184
}
175185
// <-- End added handling -->
@@ -202,7 +212,7 @@ export function zodParseWithContext<T extends z.ZodType>(
202212
targetMessages.push(...unrecognizedKeyMessages)
203213
} else if (literalMessages.length > 0) {
204214
const uniqueLiterals = [...new Set(literalMessages)]
205-
targetMessages.push(`should be one of: \`${uniqueLiterals.join('`, `')}\``)
215+
targetMessages.push(`should be one of: \`${uniqueLiterals.join('\`, \`')}\``)
206216
} else {
207217
// Fallback to joining the collected raw messages for this path
208218
targetMessages.push(...collectedMessages[nestedPath])

0 commit comments

Comments
 (0)