forked from aws/aws-sdk-js-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default-error-handler.ts
28 lines (25 loc) · 1.06 KB
/
default-error-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { HttpResponse, ResponseMetadata } from "@aws-sdk/types";
import { decorateServiceException } from "./exceptions";
/**
* Always throws an error with the given `exceptionCtor` and other arguments.
* This is only called from an error handling code path.
*
* @internal
*/
export const throwDefaultError = ({ output, parsedBody, exceptionCtor, errorCode }: any) => {
const $metadata = deserializeMetadata(output);
const statusCode = $metadata.httpStatusCode ? $metadata.httpStatusCode + "" : undefined;
const response = new exceptionCtor({
name: parsedBody?.code || parsedBody?.Code || errorCode || statusCode || "UnknownError",
$fault: "client",
$metadata,
});
throw decorateServiceException(response, parsedBody);
};
const deserializeMetadata = (output: HttpResponse): ResponseMetadata => ({
httpStatusCode: output.statusCode,
requestId:
output.headers["x-amzn-requestid"] ?? output.headers["x-amzn-request-id"] ?? output.headers["x-amz-request-id"],
extendedRequestId: output.headers["x-amz-id-2"],
cfId: output.headers["x-amz-cf-id"],
});