Skip to content

Commit

Permalink
fix: include messages from API in errors (#4593)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Jan 2, 2024
1 parent e1d5040 commit c370026
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-frogs-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: include messages from API in errors
43 changes: 43 additions & 0 deletions packages/wrangler/src/__tests__/cfetch-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { rest } from "msw";
import { hasMorePages } from "../cfetch";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { createFetchResult, msw } from "./helpers/msw";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";

/**
hasMorePages is a function that returns a boolean based on the result_info object returned from the cloudflare v4 API - if the current page is less than the total number of pages, it returns true, otherwise false.
Expand Down Expand Up @@ -36,3 +41,41 @@ describe("hasMorePages", () => {
).toBe(false);
});
});

describe("throwFetchError", () => {
mockAccountId();
mockApiToken();
runInTempDir();

it("should include api errors and messages in error", async () => {
msw.use(
rest.get("*/user", (req, res, ctx) => {
return res(
ctx.json(
createFetchResult(
null,
false,
[
{ code: 10001, message: "error one" },
{ code: 10002, message: "error two" },
],
["message one", "message two"]
)
)
);
})
);
await expect(runWrangler("whoami")).rejects.toMatchObject({
text: "A request to the Cloudflare API (/user) failed.",
notes: [
{ text: "error one [code: 10001]" },
{ text: "error two [code: 10002]" },
{ text: "message one" },
{ text: "message two" },
{
text: "\nIf you think this is a bug, please open an issue at: https://github.com/cloudflare/workers-sdk/issues/new/choose",
},
],
});
});
});
7 changes: 4 additions & 3 deletions packages/wrangler/src/cfetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ function throwFetchError(
): never {
const error = new ParseError({
text: `A request to the Cloudflare API (${resource}) failed.`,
notes: response.errors.map((err) => ({
text: renderError(err),
})),
notes: [
...response.errors.map((err) => ({ text: renderError(err) })),
...response.messages.map((text) => ({ text })),
],
});
// add the first error code directly to this error
// so consumers can use it for specific behaviour
Expand Down

0 comments on commit c370026

Please sign in to comment.