Skip to content

Commit

Permalink
throw internal error on 200 with no content
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilde345 committed Dec 13, 2024
1 parent 2527871 commit 789a921
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __tests__/unit/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ describe("query", () => {
expect(actual.summary).toEqual("the summary");
});

it("Throws ServiceError 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 ServiceError) {
expect(e.message).toEqual(
"There was an issue communicating with Fauna. 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
19 changes: 19 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
NetworkError,
ProtocolError,
ServiceError,
ServiceInternalError,
ThrottlingError,
getServiceError,
} from "./errors";
Expand Down Expand Up @@ -639,6 +640,24 @@ 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 ServiceInternalError(
{
error: {
code: "internal_error",
message:
"There was an issue communicating with Fauna. Please try again.",
},
},
500,
);
}

let parsedResponse;
try {
parsedResponse = {
Expand Down

0 comments on commit 789a921

Please sign in to comment.