Skip to content
Open
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
13 changes: 9 additions & 4 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,17 @@ export default function createClient(clientOptions) {
return { data: await getResponseData(), response };
}

// handle errors
let error = await response.text();
// handle errors (use text() when no content-length to safely handle empty bodies from proxies)
const raw = await response.text();
if (!raw) {
// empty error body - return undefined to be consistent with status 204 handling
return { error: undefined, response };
}
let error = raw;
try {
error = JSON.parse(error); // attempt to parse as JSON
error = JSON.parse(raw); // attempt to parse as JSON
} catch {
// noop
// noop - keep as raw text
}
return { error, response };
}
Expand Down
46 changes: 46 additions & 0 deletions packages/openapi-fetch/test/http-methods/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,50 @@ describe("DELETE", () => {
// assert error is empty
expect(error).toBeUndefined();
});

test("handles error response with empty body when Content-Length header is stripped by proxy", async () => {
// Simulate proxy stripping Content-Length header from an empty error response
const client = createObservedClient<paths>(
{},
async () => new Response(null, { status: 500 }), // No Content-Length header
);
const { data, error, response } = await client.DELETE("/tags/{name}", {
params: {
path: { name: "Tag" },
},
});

// assert data is undefined for error response
expect(data).toBeUndefined();

// assert error is undefined for empty body (consistent with 204 and Content-Length: 0 handling)
expect(error).toBeUndefined();

// assert response status is preserved
expect(response.status).toBe(500);
expect(response.ok).toBe(false);
});

test("handles success response with empty body when Content-Length header is stripped by proxy", async () => {
// Simulate proxy stripping Content-Length header from an empty success response
const client = createObservedClient<paths>(
{},
async () => new Response(null, { status: 200 }), // No Content-Length header
);
const { data, error, response } = await client.DELETE("/tags/{name}", {
params: {
path: { name: "Tag" },
},
});

// assert data is undefined for empty body
expect(data).toBeUndefined();

// assert error is undefined for success response
expect(error).toBeUndefined();

// assert response status is preserved
expect(response.status).toBe(200);
expect(response.ok).toBe(true);
});
});