Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Session.call throws wrong error type for server errors #112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions source/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,29 +571,25 @@ export class Session {
if (response.exception) {
throw this.getErrorFromResponse(response);
}

return this.decode(response, {}, decodeDatesAsIso);
} catch (reason) {
logger.warn("Failed to perform request. ", reason);

if (reason instanceof Error) {
if (reason.name === "AbortError") {
throw this.getErrorFromResponse({
exception: "AbortError",
content: reason.message,
});
}

try {
return this.decode(response, {}, decodeDatesAsIso);
} catch (reason) {
logger.warn("Server reported error in unexpected format. ", reason);
throw this.getErrorFromResponse({
exception: "MalformedResponseError",
content: "Response is malformed",
});
}
} catch (reason) {
logger.warn("Failed to perform request. ", reason);
if (reason instanceof Error && reason.name === "AbortError") {
throw this.getErrorFromResponse({
exception: "AbortError",
content: reason.message,
error: reason,
});
}
throw reason;
}

throw new Error("Unknown error");
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,36 @@ describe("Session", () => {
content: "foo",
})
).toBeInstanceOf(ServerError);
expect(
session.getErrorFromResponse({
exception: "MalformedResponseError",
content: "foo",
})
).toBeInstanceOf(ServerError);
});
it("If configure_totp returns validation error, we expect it to throw ValidationError", async () => {
const secret = "";
const code = "";
server.use(
rest.post("http://ftrack.test/api", async (req, res, ctx) => {
const payload = await req.json();
if (payload[0].action === "configure_totp") {
return res.once(
ctx.json({
content: "Code must be provided to enable totp.",
exception: "ValidationError",
error_code: null,
})
);
}
})
);

await expect(() =>
session.call([{ action: "configure_totp", secret, code }], {
decodeDatesAsIso: true,
})
).rejects.toThrowError("Code must be provided to enable totp.");
});
});

Expand Down