diff --git a/.changeset/big-beans-grab.md b/.changeset/big-beans-grab.md new file mode 100644 index 0000000..dfea91f --- /dev/null +++ b/.changeset/big-beans-grab.md @@ -0,0 +1,5 @@ +--- +"@saleor/app-sdk": patch +--- + +Original error messages from Upstash in UpstashAPL are now exposed in debug logs diff --git a/src/APL/upstash-apl.test.ts b/src/APL/upstash-apl.test.ts index 9cf935e..a812839 100644 --- a/src/APL/upstash-apl.test.ts +++ b/src/APL/upstash-apl.test.ts @@ -45,6 +45,7 @@ describe("APL", () => { // @ts-ignore Ignore type of mocked response fetchMock.mockResolvedValue({ status: 200, + ok: true, json: async () => ({ result: "ok" }), }); const apl = new UpstashAPL({ @@ -69,14 +70,18 @@ describe("APL", () => { it("Raise error when register service returns non 200 response", async () => { // @ts-ignore Ignore type of mocked response - fetchMock.mockResolvedValue({ status: 500 }); + fetchMock.mockResolvedValue({ + status: 401, + ok: false, + json: async () => ({ error: "Unauthorized" }), + }); const apl = new UpstashAPL({ restURL: "https://example.com", restToken: "token", }); await expect(apl.set(stubAuthData)).rejects.toThrow( - "Upstash APL responded with the code 500" + "Upstash APL was not able to perform operation. Status code: 401. Error: Unauthorized" ); }); }); @@ -87,6 +92,7 @@ describe("APL", () => { // @ts-ignore Ignore type of mocked response fetchMock.mockResolvedValue({ status: 200, + ok: true, json: async () => ({ result: JSON.stringify(stubAuthData), }), @@ -100,6 +106,7 @@ describe("APL", () => { // @ts-ignore Ignore type of mocked response fetchMock.mockResolvedValue({ status: 200, + ok: true, json: async () => ({ result: null, }), diff --git a/src/APL/upstash-apl.ts b/src/APL/upstash-apl.ts index 4e49141..5ff7db7 100644 --- a/src/APL/upstash-apl.ts +++ b/src/APL/upstash-apl.ts @@ -72,16 +72,21 @@ export class UpstashAPL implements APL { debug("Error during sending the data:", error); throw new Error(`UpstashAPL was unable to perform a request ${error}`); } - if (response.status >= 400 || response.status < 200) { - debug("Non 200 response code. Upstash responded with %j", response); - throw new Error(`Upstash APL responded with the code ${response.status}`); - } + const parsedResponse = (await response.json()) as UpstashResponse; - if ("error" in parsedResponse) { - debug("Upstash API responded with error: %s", parsedResponse.error); - throw new Error("Upstash APL was not able to perform operation"); + if (!response.ok || "error" in parsedResponse) { + debug(`Operation unsuccessful. Upstash API has responded with ${response.status} code`); + if ("error" in parsedResponse) { + debug("Error message: %s", parsedResponse.error); + throw new Error( + `Upstash APL was not able to perform operation. Status code: ${response.status}. Error: ${parsedResponse.error}` + ); + } + throw new Error( + `Upstash APL was not able to perform operation. Status code: ${response.status}` + ); } - debug("Register service responded successfully"); + debug("Upstash service responded successfully"); return parsedResponse.result; }