-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client-s3): throw errors with 200 status code
- Loading branch information
1 parent
1f46eac
commit 8e81e65
Showing
10 changed files
with
263 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./validate-bucket-name"; | ||
export * from "./use-regional-endpoint"; | ||
export * from "./throw-200-exceptions"; |
72 changes: 72 additions & 0 deletions
72
packages/middleware-sdk-s3/src/throw-200-exceptions.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { HttpRequest, HttpResponse } from "@aws-sdk/protocol-http"; | ||
|
||
import { throw200ExceptionsMiddleware } from "./throw-200-exceptions"; | ||
|
||
describe("throw200ExceptionsMiddlewareOptions", () => { | ||
const mockNextHandler = jest.fn(); | ||
const mockStreamCollector = jest.fn(); | ||
const mockUtf8Encoder = jest.fn(); | ||
const mockConfig = { | ||
streamCollector: mockStreamCollector, | ||
utf8Encoder: mockUtf8Encoder, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should throw if response body is empty", async () => { | ||
expect.assertions(3); | ||
mockStreamCollector.mockResolvedValue(Buffer.from("")); | ||
mockUtf8Encoder.mockReturnValue(""); | ||
mockNextHandler.mockReturnValue({ | ||
response: new HttpResponse({ | ||
statusCode: 200, | ||
headers: {}, | ||
body: "", | ||
}), | ||
}); | ||
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any); | ||
try { | ||
await handler({ | ||
input: {}, | ||
request: new HttpRequest({ | ||
hostname: "s3.us-east-1.amazonaws.com", | ||
}), | ||
}); | ||
} catch (e) { | ||
expect(e).toBeDefined(); | ||
expect(e.name).toEqual("InternalError"); | ||
expect(e.message).toEqual("S3 aborted request"); | ||
} | ||
}); | ||
|
||
it("should throw if response body contains Error tag", async () => { | ||
const errorBody = `<?xml version="1.0" encoding="UTF-8"?> | ||
<Error> | ||
<Code>InternalError</Code> | ||
<Message>We encountered an internal error. Please try again.</Message> | ||
<RequestId>656c76696e6727732072657175657374</RequestId> | ||
<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId> | ||
</Error>`; | ||
mockStreamCollector.mockResolvedValue(Buffer.from(errorBody)); | ||
mockUtf8Encoder.mockReturnValue(errorBody); | ||
mockNextHandler.mockReturnValue({ | ||
response: new HttpResponse({ | ||
statusCode: 200, | ||
headers: {}, | ||
body: "", | ||
}), | ||
}); | ||
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any); | ||
const { response } = await handler({ | ||
input: {}, | ||
request: new HttpRequest({ | ||
hostname: "s3.us-east-1.amazonaws.com", | ||
}), | ||
}); | ||
expect(HttpResponse.isInstance(response)).toBe(true); | ||
// @ts-ignore | ||
expect(response.statusCode).toBeGreaterThanOrEqual(400); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { HttpResponse } from "@aws-sdk/protocol-http"; | ||
import { DeserializeMiddleware, Encoder, Pluggable, RelativeMiddlewareOptions, StreamCollector } from "@aws-sdk/types"; | ||
|
||
type PreviouslyResolved = { | ||
streamCollector: StreamCollector; | ||
utf8Encoder: Encoder; | ||
}; | ||
|
||
/** | ||
* In case of an internal error/terminated connection, S3 operations may return 200 errors. CopyObject, UploadPartCopy, | ||
* CompleteMultipartUpload may return empty payload or payload with only xml Preamble. | ||
* @internal | ||
*/ | ||
export const throw200ExceptionsMiddleware = (config: PreviouslyResolved): DeserializeMiddleware<any, any> => ( | ||
next | ||
) => async (args) => { | ||
const result = await next(args); | ||
const { response } = result; | ||
if (!HttpResponse.isInstance(response)) return result; | ||
const { statusCode, body } = response; | ||
if (statusCode < 200 && statusCode >= 300) return result; | ||
|
||
// Throw 2XX response that's either an error or has empty body. | ||
const bodyBytes = await collectBody(body, config); | ||
const bodyString = await collectBodyString(bodyBytes, config); | ||
if (bodyBytes.length === 0) { | ||
const err = new Error("S3 aborted request"); | ||
err.name = "InternalError"; | ||
throw err; | ||
} | ||
if (bodyString && bodyString.match("<Error>")) { | ||
// Set the error code to 4XX so that error deserializer can parse them | ||
response.statusCode = 400; | ||
} | ||
|
||
// Body stream is consumed and paused at this point. So replace the response.body to the collected bytes. | ||
// So that the deserializer can consume the body as normal. | ||
response.body = bodyBytes; | ||
return result; | ||
}; | ||
|
||
// Collect low-level response body stream to Uint8Array. | ||
const collectBody = (streamBody: any = new Uint8Array(), context: PreviouslyResolved): Promise<Uint8Array> => { | ||
if (streamBody instanceof Uint8Array) { | ||
return Promise.resolve(streamBody); | ||
} | ||
return context.streamCollector(streamBody) || Promise.resolve(new Uint8Array()); | ||
}; | ||
|
||
// Encode Uint8Array data into string with utf-8. | ||
const collectBodyString = (streamBody: any, context: PreviouslyResolved): Promise<string> => | ||
collectBody(streamBody, context).then((body) => context.utf8Encoder(body)); | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const throw200ExceptionsMiddlewareOptions: RelativeMiddlewareOptions = { | ||
relation: "after", | ||
toMiddleware: "deserializerMiddleware", | ||
tags: ["THROW_200_EXCEPTIONS", "S3"], | ||
name: "throw200ExceptionsMiddleware", | ||
}; | ||
|
||
/** | ||
* | ||
* @internal | ||
*/ | ||
export const getThrow200ExceptionsPlugin = (config: PreviouslyResolved): Pluggable<any, any> => ({ | ||
applyToStack: (clientStack) => { | ||
clientStack.addRelativeTo(throw200ExceptionsMiddleware(config), throw200ExceptionsMiddlewareOptions); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters