Skip to content

Commit

Permalink
fix: ensure API failures without messages logged correctly (#4695)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot authored Jan 4, 2024
1 parent 037de5e commit 0f8a03c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-crabs-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: ensure API failures without additional messages logged correctly
18 changes: 18 additions & 0 deletions packages/wrangler/src/__tests__/cfetch-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,22 @@ describe("throwFetchError", () => {
],
});
});

it("should include api errors without messages", async () => {
msw.use(
rest.get("*/user", (req, res, ctx) => {
return res(
ctx.json({
result: null,
success: false,
errors: [{ code: 10000, message: "error" }],
})
);
})
);
await expect(runWrangler("whoami")).rejects.toMatchObject({
text: "A request to the Cloudflare API (/user) failed.",
notes: [{ text: "error [code: 10000]" }],
});
});
});
4 changes: 2 additions & 2 deletions packages/wrangler/src/cfetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface FetchResult<ResponseType = unknown> {
success: boolean;
result: ResponseType;
errors: FetchError[];
messages: string[];
messages?: string[];
result_info?: unknown;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ function throwFetchError(
text: `A request to the Cloudflare API (${resource}) failed.`,
notes: [
...response.errors.map((err) => ({ text: renderError(err) })),
...response.messages.map((text) => ({ text })),
...(response.messages?.map((text) => ({ text })) ?? []),
],
});
// add the first error code directly to this error
Expand Down

0 comments on commit 0f8a03c

Please sign in to comment.