Improve error messages in UpstashAPL (#200)

* Fix #199

Fix #199

* Throw formatted error on UpstashAPL error

* Create .changeset/big-beans-grab.md

---------

Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
Co-authored-by: Lukasz Ostrowski <lukasz@monolog.tech>
This commit is contained in:
Hazem Khaled 2023-03-09 10:59:35 +03:00 committed by GitHub
parent 172de4a91a
commit 3786c86506
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 10 deletions

View file

@ -0,0 +1,5 @@
---
"@saleor/app-sdk": patch
---
Original error messages from Upstash in UpstashAPL are now exposed in debug logs

View file

@ -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,
}),

View file

@ -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 (!response.ok || "error" in parsedResponse) {
debug(`Operation unsuccessful. Upstash API has responded with ${response.status} code`);
if ("error" in parsedResponse) {
debug("Upstash API responded with error: %s", parsedResponse.error);
throw new Error("Upstash APL was not able to perform operation");
debug("Error message: %s", parsedResponse.error);
throw new Error(
`Upstash APL was not able to perform operation. Status code: ${response.status}. Error: ${parsedResponse.error}`
);
}
debug("Register service responded successfully");
throw new Error(
`Upstash APL was not able to perform operation. Status code: ${response.status}`
);
}
debug("Upstash service responded successfully");
return parsedResponse.result;
}