Skip to content

Commit

Permalink
Fix event API errors not being parsed and returned (#535)
Browse files Browse the repository at this point in the history
## Summary
<!-- Succinctly describe your change, providing context, what you've
changed, and why. -->

When the event API responds with an error, we attempt to parse that
error and include it in the `Error` returned to the user.

The tests for this, however, mocked the incorrect response from Inngest,
resulting in the illusion of this functioning. The mock has been changed
and the fix implemented by slightly changing the parsing of the
response.

## Checklist
<!-- Tick these items off as you progress. -->
<!-- If an item isn't applicable, ideally please strikeout the item by
wrapping it in "~~"" and suffix it with "N/A My reason for skipping
this." -->
<!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" -->

- [ ] ~Added a [docs PR](https://github.com/inngest/website) that
references this PR~ N/A Bug fix
- [x] Added unit/integration tests
- [x] Added changesets if applicable
  • Loading branch information
jpwilliams committed Apr 16, 2024
1 parent a40cbce commit 09ef143
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-numbers-nail.md
@@ -0,0 +1,5 @@
---
"inngest": patch
---

Fix sending events sometimes returning generic errors when we can be more specific
60 changes: 43 additions & 17 deletions packages/inngest/src/components/Inngest.test.ts
Expand Up @@ -141,13 +141,16 @@ describe("send", () => {
error,
}: Partial<SendEventResponse> = {}) => {
return jest.fn((url: string, opts: { body: string }) => {
const json = {
status,
ids:
ids ??
(JSON.parse(opts.body) as EventPayload[]).map(() => "test-id"),
error,
};
const json = error
? {
error,
}
: {
status,
ids:
ids ??
(JSON.parse(opts.body) as EventPayload[]).map(() => "test-id"),
};

return Promise.resolve({
status,
Expand All @@ -162,7 +165,12 @@ describe("send", () => {
};

beforeAll(() => {
global.fetch = setFetch();
Object.defineProperties(global, {
fetch: {
value: setFetch(),
configurable: true,
},
});
});

beforeEach(() => {
Expand All @@ -171,7 +179,13 @@ describe("send", () => {
});

afterAll(() => {
global.fetch = originalFetch;
Object.defineProperties(global, {
fetch: {
value: originalFetch,
configurable: true,
},
});

process.env = originalProcessEnv;
});

Expand Down Expand Up @@ -480,31 +494,43 @@ describe("send", () => {
});

test("should return error from Inngest if parsed", () => {
global.fetch = setFetch({ status: 400, error: "Test Error" });
const inngest = createClient({ id: "test", eventKey: testEventKey });
const inngest = createClient({
id: "test",
eventKey: testEventKey,
fetch: setFetch({ status: 400, error: "Test Error" }),
});

return expect(inngest.send(testEvent)).rejects.toThrowError("Test Error");
});

test("should return error from Inngest if parsed even for 200", () => {
global.fetch = setFetch({ status: 200, error: "Test Error" });
const inngest = createClient({ id: "test", eventKey: testEventKey });
const inngest = createClient({
id: "test",
eventKey: testEventKey,
fetch: setFetch({ status: 200, error: "Test Error" }),
});

return expect(inngest.send(testEvent)).rejects.toThrowError("Test Error");
});

test("should return error if bad status code with no error string", () => {
global.fetch = setFetch({ status: 400 });
const inngest = createClient({ id: "test", eventKey: testEventKey });
const inngest = createClient({
id: "test",
eventKey: testEventKey,
fetch: setFetch({ status: 400 }),
});

return expect(inngest.send(testEvent)).rejects.toThrowError(
"Cannot process event payload"
);
});

test("should return unknown error from response text if very bad status code", () => {
global.fetch = setFetch({ status: 600 });
const inngest = createClient({ id: "test", eventKey: testEventKey });
const inngest = createClient({
id: "test",
eventKey: testEventKey,
fetch: setFetch({ status: 600 }),
});

return expect(inngest.send(testEvent)).rejects.toThrowError("600");
});
Expand Down
4 changes: 2 additions & 2 deletions packages/inngest/src/types.ts
Expand Up @@ -493,12 +493,12 @@ export const sendEventResponseSchema = z.object({
/**
* Event IDs
*/
ids: z.array(z.string()),
ids: z.array(z.string()).default([]),

/**
* HTTP Status Code. Will be undefined if no request was sent.
*/
status: z.number(),
status: z.number().default(0),

/**
* Error message. Will be undefined if no error occurred.
Expand Down

0 comments on commit 09ef143

Please sign in to comment.