diff --git a/.changeset/chilly-chairs-jog.md b/.changeset/chilly-chairs-jog.md new file mode 100644 index 000000000..e700994dd --- /dev/null +++ b/.changeset/chilly-chairs-jog.md @@ -0,0 +1,5 @@ +--- +"inngest": patch +--- + +Fix rare body reuse when parsing failure returns from `inngest.send()` and `step.sendEvent()` diff --git a/packages/inngest/src/components/Inngest.ts b/packages/inngest/src/components/Inngest.ts index 62069c6d3..f4d0e8fbc 100644 --- a/packages/inngest/src/components/Inngest.ts +++ b/packages/inngest/src/components/Inngest.ts @@ -318,6 +318,7 @@ export class Inngest { */ private async getResponseError( response: globalThis.Response, + rawBody: unknown, foundErr = "Unknown error" ): Promise { let errorMessage = foundErr; @@ -337,7 +338,7 @@ export class Inngest { errorMessage = "Event key not found"; break; case 406: - errorMessage = `${JSON.stringify(await response.json())}`; + errorMessage = `${JSON.stringify(await rawBody)}`; break; case 409: case 412: @@ -350,7 +351,11 @@ export class Inngest { errorMessage = "Internal server error"; break; default: - errorMessage = await response.text(); + try { + errorMessage = await response.text(); + } catch (err) { + errorMessage = `${JSON.stringify(await rawBody)}`; + } break; } } @@ -558,17 +563,18 @@ export class Inngest { headers: { ...this.headers, ...headers }, }); + let rawBody: unknown; let body: SendEventResponse | undefined; try { - const rawBody: unknown = await response.json(); + rawBody = await response.json(); body = await sendEventResponseSchema.parseAsync(rawBody); } catch (err) { - throw await this.getResponseError(response); + throw await this.getResponseError(response, rawBody); } if (body.status / 100 !== 2 || body.error) { - throw await this.getResponseError(response, body.error); + throw await this.getResponseError(response, rawBody, body.error); } return await applyHookToOutput({ result: { ids: body.ids } });