Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not reuse the response body when parsing errors #745

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/chilly-chairs-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"inngest": patch
---

Fix rare body reuse when parsing failure returns from `inngest.send()` and `step.sendEvent()`
16 changes: 11 additions & 5 deletions packages/inngest/src/components/Inngest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export class Inngest<TClientOpts extends ClientOptions = ClientOptions> {
*/
private async getResponseError(
response: globalThis.Response,
rawBody: unknown,
foundErr = "Unknown error"
): Promise<Error> {
let errorMessage = foundErr;
Expand All @@ -337,7 +338,7 @@ export class Inngest<TClientOpts extends ClientOptions = ClientOptions> {
errorMessage = "Event key not found";
break;
case 406:
errorMessage = `${JSON.stringify(await response.json())}`;
errorMessage = `${JSON.stringify(await rawBody)}`;
break;
case 409:
case 412:
Expand All @@ -350,7 +351,11 @@ export class Inngest<TClientOpts extends ClientOptions = ClientOptions> {
errorMessage = "Internal server error";
break;
default:
errorMessage = await response.text();
try {
errorMessage = await response.text();
} catch (err) {
errorMessage = `${JSON.stringify(await rawBody)}`;
}
break;
}
}
Expand Down Expand Up @@ -558,17 +563,18 @@ export class Inngest<TClientOpts extends ClientOptions = ClientOptions> {
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 } });
Expand Down