Skip to content

Instantly share code, notes, and snippets.

@ciiqr
Last active September 15, 2023 17:40
Show Gist options
  • Save ciiqr/27b46e63710d97996979b6b4bb47556f to your computer and use it in GitHub Desktop.
Save ciiqr/27b46e63710d97996979b6b4bb47556f to your computer and use it in GitHub Desktop.
zod type guard for @sendgrid/mail's ResponseError
// NOTE: sendgrid doesn't properly export its ResponseError class, so this type
// (roughly) represents that type (with the type of body fixed)
const sendGridResponseErrorSchema = z.instanceof(Error).and(z.object({
code: z.number(),
response: z.object({
headers: z.record(z.string(), z.string()),
body: z.object({
errors: z.array(z.unknown())
})
})
}));
type SendGridResponseError = z.infer<typeof sendGridResponseErrorSchema>;
function isSendGridResponseError(val: unknown): val is SendGridResponseError {
return sendGridResponseErrorSchema.safeParse(val).success;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment