Skip to content

Commit

Permalink
v10 driver - empty 200 response throws protocol error (#313)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Paterson <[email protected]>
  • Loading branch information
mwilde345 and ptpaterson authored Dec 17, 2024
1 parent 2527871 commit d004758
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions __tests__/unit/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AuthorizationError,
FetchClient,
fql,
ProtocolError,
QueryTimeoutError,
ServiceError,
ServiceInternalError,
Expand Down Expand Up @@ -123,6 +124,25 @@ describe("query", () => {
expect(actual.summary).toEqual("the summary");
});

it("Throws ProtocolError on an empty 200 response", async () => {
expect.assertions(2);
fetchMock.mockResponse("", {
status: 200,
headers: [["content-length", "0"]],
});
try {
const result = await client.query(fql`'foo'.length`);
console.log("result", result);
} catch (e) {
if (e instanceof ProtocolError) {
expect(e.message).toEqual(
"There was an issue communicating with Fauna. Response is empty. Please try again.",
);
expect(e.httpStatus).toEqual(500);
}
}
});

// it("throws an NetworkError on a timeout", async () => {
// expect.assertions(2);
// // axios mock adapater currently has a bug that cannot match
Expand Down
13 changes: 13 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,19 @@ in an environmental variable named FAUNA_SECRET or pass it to the Client\
JSON.stringify(response.headers),
);

// Receiving a 200 with no body/content indicates an issue with core router
if (
response.status === 200 &&
(response.body.length === 0 ||
response.headers["content-length"] === "0")
) {
throw new ProtocolError({
message:
"There was an issue communicating with Fauna. Response is empty. Please try again.",
httpStatus: 500,
});
}

let parsedResponse;
try {
parsedResponse = {
Expand Down

0 comments on commit d004758

Please sign in to comment.