Skip to content

Commit 033dbe6

Browse files
committed
Only decode or return JSON or plain text
1 parent d95f9a3 commit 033dbe6

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

packages/graph-explorer/src/connector/fetchDatabaseRequest.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ describe("decodeErrorSafely", () => {
1414
expect(decoded).toBeUndefined();
1515
});
1616

17+
it("should ignore xml responses", async () => {
18+
const response = new Response("<xml></xml>", {
19+
status: 500,
20+
headers: {
21+
"Content-Type": "text/xml",
22+
},
23+
});
24+
25+
const decoded = await decodeErrorSafely(response);
26+
27+
expect(decoded).toBeUndefined();
28+
});
29+
1730
it("should return the error message inside the JSON response", async () => {
1831
const response = new Response(
1932
JSON.stringify({

packages/graph-explorer/src/connector/fetchDatabaseRequest.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,38 @@ const NeptuneErrorSchema = z.object({
2323
*/
2424
export async function decodeErrorSafely(response: Response): Promise<any> {
2525
const contentType = response.headers.get("Content-Type");
26-
const contentTypeHasValue = contentType !== null && contentType.length > 0;
27-
// Assume missing content type is JSON
28-
const isJson =
29-
!contentTypeHasValue || contentType.includes("application/json");
30-
const isHtml = contentTypeHasValue && contentType.includes("text/html");
26+
if (
27+
!contentType ||
28+
(!contentType.includes("application/json") &&
29+
!contentType.includes("text/plain"))
30+
) {
31+
return undefined;
32+
}
3133

3234
// Extract the raw text from the response
3335
const rawText = await response.text();
34-
35-
// Check for empty response
3636
if (!rawText) {
3737
return undefined;
38-
} else if (isJson) {
38+
}
39+
40+
// Parse JSON
41+
if (contentType.includes("application/json")) {
3942
try {
4043
// Try parsing the response as JSON
4144
const data = JSON.parse(rawText);
4245

4346
// Flatten the error if it contains an error object
4447
return data?.error ?? data;
4548
} catch (error) {
46-
console.error("Failed to decode the error response as JSON", {
49+
// Log the error and fallthrough to return the raw text
50+
logger.warn("Failed to decode the error response as JSON", {
4751
error,
4852
rawText,
4953
});
50-
return rawText;
5154
}
52-
} else if (isHtml) {
53-
// Ignore the content of HTML responses
54-
return undefined;
5555
}
5656

57+
// Just return the whole response as the error
5758
return { message: rawText };
5859
}
5960

0 commit comments

Comments
 (0)