Skip to content

Commit 850c13f

Browse files
authored
fix(middleware-sdk-s3): process errors with 200 status code (#3874)
1 parent 2156889 commit 850c13f

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

packages/middleware-sdk-s3/src/throw-200-exceptions.spec.ts

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
66
const mockNextHandler = jest.fn();
77
const mockStreamCollector = jest.fn();
88
const mockUtf8Encoder = jest.fn();
9+
const mockResponse = jest.fn();
910
const mockConfig = {
1011
streamCollector: mockStreamCollector,
1112
utf8Encoder: mockUtf8Encoder,
@@ -15,58 +16,80 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
1516
jest.clearAllMocks();
1617
});
1718

18-
it("should throw if response body is empty", async () => {
19-
expect.assertions(3);
19+
describe("tests for statusCode < 200 and >= 300", () => {
2020
mockStreamCollector.mockResolvedValue(Buffer.from(""));
2121
mockUtf8Encoder.mockReturnValue("");
22-
mockNextHandler.mockReturnValue({
23-
response: new HttpResponse({
24-
statusCode: 200,
22+
23+
it.each([199, 300])("results for statusCode %i", async (statusCode) => {
24+
mockNextHandler.mockReturnValue({
25+
response: mockResponse,
26+
statusCode,
2527
headers: {},
26-
body: "",
27-
}),
28-
});
29-
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
30-
try {
31-
await handler({
28+
body: "body",
29+
});
30+
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
31+
const result = await handler({
3232
input: {},
3333
request: new HttpRequest({
3434
hostname: "s3.us-east-1.amazonaws.com",
3535
}),
3636
});
37-
} catch (e) {
38-
expect(e).toBeDefined();
39-
expect(e.name).toEqual("InternalError");
40-
expect(e.message).toEqual("S3 aborted request");
41-
}
42-
});
37+
expect(result.response).toBe(mockResponse);
38+
});
39+
40+
it("should throw if response body is empty", async () => {
41+
expect.assertions(3);
42+
mockStreamCollector.mockResolvedValue(Buffer.from(""));
43+
mockUtf8Encoder.mockReturnValue("");
44+
mockNextHandler.mockReturnValue({
45+
response: new HttpResponse({
46+
statusCode: 200,
47+
headers: {},
48+
body: "",
49+
}),
50+
});
51+
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
52+
try {
53+
await handler({
54+
input: {},
55+
request: new HttpRequest({
56+
hostname: "s3.us-east-1.amazonaws.com",
57+
}),
58+
});
59+
} catch (e) {
60+
expect(e).toBeDefined();
61+
expect(e.name).toEqual("InternalError");
62+
expect(e.message).toEqual("S3 aborted request");
63+
}
64+
});
4365

44-
it("should throw if response body contains Error tag", async () => {
45-
const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
66+
it("should throw if response body contains Error tag", async () => {
67+
const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
4668
<Error>
4769
<Code>InternalError</Code>
4870
<Message>We encountered an internal error. Please try again.</Message>
4971
<RequestId>656c76696e6727732072657175657374</RequestId>
5072
<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
5173
</Error>`;
52-
mockStreamCollector.mockResolvedValue(Buffer.from(errorBody));
53-
mockUtf8Encoder.mockReturnValue(errorBody);
54-
mockNextHandler.mockReturnValue({
55-
response: new HttpResponse({
56-
statusCode: 200,
57-
headers: {},
58-
body: "",
59-
}),
60-
});
61-
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
62-
const { response } = await handler({
63-
input: {},
64-
request: new HttpRequest({
65-
hostname: "s3.us-east-1.amazonaws.com",
66-
}),
74+
mockStreamCollector.mockResolvedValue(Buffer.from(errorBody));
75+
mockUtf8Encoder.mockReturnValue(errorBody);
76+
mockNextHandler.mockReturnValue({
77+
response: new HttpResponse({
78+
statusCode: 200,
79+
headers: {},
80+
body: "",
81+
}),
82+
});
83+
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
84+
const { response } = await handler({
85+
input: {},
86+
request: new HttpRequest({
87+
hostname: "s3.us-east-1.amazonaws.com",
88+
}),
89+
});
90+
expect(HttpResponse.isInstance(response)).toBe(true);
91+
// @ts-ignore
92+
expect(response.statusCode).toBeGreaterThanOrEqual(400);
6793
});
68-
expect(HttpResponse.isInstance(response)).toBe(true);
69-
// @ts-ignore
70-
expect(response.statusCode).toBeGreaterThanOrEqual(400);
7194
});
7295
});

packages/middleware-sdk-s3/src/throw-200-exceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const throw200ExceptionsMiddleware =
1919
const { response } = result;
2020
if (!HttpResponse.isInstance(response)) return result;
2121
const { statusCode, body } = response;
22-
if (statusCode < 200 && statusCode >= 300) return result;
22+
if (statusCode < 200 || statusCode >= 300) return result;
2323

2424
// Throw 2XX response that's either an error or has empty body.
2525
const bodyBytes = await collectBody(body, config);

0 commit comments

Comments
 (0)