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:
parent
172de4a91a
commit
3786c86506
3 changed files with 27 additions and 10 deletions
5
.changeset/big-beans-grab.md
Normal file
5
.changeset/big-beans-grab.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"@saleor/app-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Original error messages from Upstash in UpstashAPL are now exposed in debug logs
|
|
@ -45,6 +45,7 @@ describe("APL", () => {
|
||||||
// @ts-ignore Ignore type of mocked response
|
// @ts-ignore Ignore type of mocked response
|
||||||
fetchMock.mockResolvedValue({
|
fetchMock.mockResolvedValue({
|
||||||
status: 200,
|
status: 200,
|
||||||
|
ok: true,
|
||||||
json: async () => ({ result: "ok" }),
|
json: async () => ({ result: "ok" }),
|
||||||
});
|
});
|
||||||
const apl = new UpstashAPL({
|
const apl = new UpstashAPL({
|
||||||
|
@ -69,14 +70,18 @@ describe("APL", () => {
|
||||||
|
|
||||||
it("Raise error when register service returns non 200 response", async () => {
|
it("Raise error when register service returns non 200 response", async () => {
|
||||||
// @ts-ignore Ignore type of mocked response
|
// @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({
|
const apl = new UpstashAPL({
|
||||||
restURL: "https://example.com",
|
restURL: "https://example.com",
|
||||||
restToken: "token",
|
restToken: "token",
|
||||||
});
|
});
|
||||||
await expect(apl.set(stubAuthData)).rejects.toThrow(
|
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
|
// @ts-ignore Ignore type of mocked response
|
||||||
fetchMock.mockResolvedValue({
|
fetchMock.mockResolvedValue({
|
||||||
status: 200,
|
status: 200,
|
||||||
|
ok: true,
|
||||||
json: async () => ({
|
json: async () => ({
|
||||||
result: JSON.stringify(stubAuthData),
|
result: JSON.stringify(stubAuthData),
|
||||||
}),
|
}),
|
||||||
|
@ -100,6 +106,7 @@ describe("APL", () => {
|
||||||
// @ts-ignore Ignore type of mocked response
|
// @ts-ignore Ignore type of mocked response
|
||||||
fetchMock.mockResolvedValue({
|
fetchMock.mockResolvedValue({
|
||||||
status: 200,
|
status: 200,
|
||||||
|
ok: true,
|
||||||
json: async () => ({
|
json: async () => ({
|
||||||
result: null,
|
result: null,
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -72,16 +72,21 @@ export class UpstashAPL implements APL {
|
||||||
debug("Error during sending the data:", error);
|
debug("Error during sending the data:", error);
|
||||||
throw new Error(`UpstashAPL was unable to perform a request ${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;
|
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) {
|
if ("error" in parsedResponse) {
|
||||||
debug("Upstash API responded with error: %s", parsedResponse.error);
|
debug("Error message: %s", parsedResponse.error);
|
||||||
throw new Error("Upstash APL was not able to perform operation");
|
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;
|
return parsedResponse.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue