Skip to content

Commit 2465616

Browse files
committed
Do not throw when the server responds with something we do not have in the schema yet
Mentioning @mifi @remcohaszing @Acconut @tim-kos because this may be controversial, but i think when retrofitting types and schemas on a big api like ours, this is the least evil and still a good step forward.
1 parent 80e18bf commit 2465616

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/Transloadit.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
assemblyStatusSchema,
2929
assemblyIndexItemSchema,
3030
type AssemblyIndexItem,
31+
assemblyIndexSchema,
32+
AssemblyIndex,
3133
} from './alphalib/types/assemblyStatus.js'
3234
import type {
3335
BaseResponse,
@@ -398,10 +400,21 @@ export class Transloadit {
398400

399401
const parsedResult = zodParseWithContext(assemblyStatusSchema, rawResult)
400402
if (!parsedResult.success) {
401-
throw new InconsistentResponseError(
403+
const err = new InconsistentResponseError(
402404
`The API responded with data that does not match the expected schema.\n${parsedResult.humanReadable}`
403405
)
406+
// eslint-disable-next-line no-console
407+
console.error(
408+
`---\nPlease report this error to Transloadit ([email protected]). We are working on better schemas for our API and this looks like something we do not cover yet: \n\n${err}\nThank you in advance!---\n`
409+
)
410+
// @TODO, once our schemas have matured, we should throw the error here.
411+
// But as it stands, schemas are new, and we can't easily update all customer's node-sdks,
412+
// so there will be a long tail of throws if we enable this now.
413+
checkAssemblyUrls(rawResult as AssemblyStatus)
414+
return rawResult as AssemblyStatus
404415
}
416+
417+
checkAssemblyUrls(parsedResult.safe)
405418
return parsedResult.safe
406419
}
407420

@@ -471,12 +484,23 @@ export class Transloadit {
471484
)
472485
}
473486

474-
const parsedResult = zodParseWithContext(z.array(assemblyIndexItemSchema), rawResponse.items)
487+
const parsedResult = zodParseWithContext(assemblyIndexSchema, rawResponse.items)
475488

476489
if (!parsedResult.success) {
477-
throw new InconsistentResponseError(
490+
const err = new InconsistentResponseError(
478491
`API response for listAssemblies contained items that do not match the expected schema.\n${parsedResult.humanReadable}`
479492
)
493+
// eslint-disable-next-line no-console
494+
console.error(
495+
`---\nPlease report this error to Transloadit ([email protected]). We are working on better schemas for our API and this looks like something we do not cover yet: \n\n${err}\nThank you in advance!---\n`
496+
)
497+
return {
498+
// @TODO, once our schemas have matured, we should throw the error here.
499+
// But as it stands, schemas are new, and we can't easily update all customer's node-sdks,
500+
// so there will be a long tail of throws if we enable this now.
501+
items: rawResponse.items as AssemblyIndex,
502+
count: rawResponse.count,
503+
}
480504
}
481505

482506
return {
@@ -503,9 +527,18 @@ export class Transloadit {
503527
const parsedResult = zodParseWithContext(assemblyStatusSchema, rawResult)
504528

505529
if (!parsedResult.success) {
506-
throw new InconsistentResponseError(
530+
const err = new InconsistentResponseError(
507531
`The API responded with data that does not match the expected schema.\n${parsedResult.humanReadable}`
508532
)
533+
// eslint-disable-next-line no-console
534+
console.error(
535+
`---\nPlease report this error to Transloadit ([email protected]). We are working on better schemas for our API and this looks like something we do not cover yet: \n\n${err}\nThank you in advance!---\n`
536+
)
537+
// @TODO, once our schemas have matured, we should throw the error here.
538+
// But as it stands, schemas are new, and we can't easily update all customer's node-sdks,
539+
// so there will be a long tail of throws if we enable this now.
540+
checkAssemblyUrls(rawResult as AssemblyStatus)
541+
return rawResult as AssemblyStatus
509542
}
510543

511544
checkAssemblyUrls(parsedResult.safe)

src/alphalib/types/assemblyStatus.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ const assemblyStatusBaseSchema = z.object({
381381
.optional(),
382382
is_infinite: z.boolean().optional(),
383383
has_dupe_jobs: z.boolean().optional(),
384-
execution_start: z.string().optional(),
385-
execution_duration: z.number().optional(),
384+
execution_start: z.string().nullable().optional(),
385+
execution_duration: z.number().nullable().optional(),
386386
queue_duration: z.number().optional(),
387387
jobs_queue_duration: z.number().optional(),
388388
notify_start: z.string().nullable().optional(),
@@ -606,3 +606,6 @@ export const assemblyIndexItemSchema = z
606606
.strict()
607607

608608
export type AssemblyIndexItem = z.infer<typeof assemblyIndexItemSchema>
609+
610+
export const assemblyIndexSchema = z.array(assemblyIndexItemSchema)
611+
export type AssemblyIndex = z.infer<typeof assemblyIndexSchema>

0 commit comments

Comments
 (0)