Skip to content

Commit 64ff587

Browse files
committed
feat: write error.$responseBodyText if available
1 parent 8fc4305 commit 64ff587

File tree

8 files changed

+25
-104
lines changed

8 files changed

+25
-104
lines changed

clients/client-lambda/src/models/models_0.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// smithy-typescript generated code
22
import { ExceptionOptionType as __ExceptionOptionType, SENSITIVE_STRING } from "@smithy/smithy-client";
3-
43
import { StreamingBlobTypes } from "@smithy/types";
54

65
import { LambdaServiceException as __BaseException } from "./LambdaServiceException";

clients/client-lambda/src/protocols/Aws_restJson1.ts

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// smithy-typescript generated code
2+
import { loadRestJsonErrorCode, parseJsonBody as parseBody, parseJsonErrorBody as parseErrorBody } from "@aws-sdk/core";
23
import { requestBuilder as rb } from "@smithy/core";
34
import { HttpRequest as __HttpRequest, HttpResponse as __HttpResponse } from "@smithy/protocol-http";
45
import {
@@ -4912,54 +4913,3 @@ const _xafe = "x-amz-function-error";
49124913
const _xait = "x-amz-invocation-type";
49134914
const _xalr = "x-amz-log-result";
49144915
const _xalt = "x-amz-log-type";
4915-
4916-
const parseBody = (streamBody: any, context: __SerdeContext): any =>
4917-
collectBodyString(streamBody, context).then((encoded) => {
4918-
if (encoded.length) {
4919-
return JSON.parse(encoded);
4920-
}
4921-
return {};
4922-
});
4923-
4924-
const parseErrorBody = async (errorBody: any, context: __SerdeContext) => {
4925-
const value = await parseBody(errorBody, context);
4926-
value.message = value.message ?? value.Message;
4927-
return value;
4928-
};
4929-
4930-
/**
4931-
* Load an error code for the aws.rest-json-1.1 protocol.
4932-
*/
4933-
const loadRestJsonErrorCode = (output: __HttpResponse, data: any): string | undefined => {
4934-
const findKey = (object: any, key: string) => Object.keys(object).find((k) => k.toLowerCase() === key.toLowerCase());
4935-
4936-
const sanitizeErrorCode = (rawValue: string | number): string => {
4937-
let cleanValue = rawValue;
4938-
if (typeof cleanValue === "number") {
4939-
cleanValue = cleanValue.toString();
4940-
}
4941-
if (cleanValue.indexOf(",") >= 0) {
4942-
cleanValue = cleanValue.split(",")[0];
4943-
}
4944-
if (cleanValue.indexOf(":") >= 0) {
4945-
cleanValue = cleanValue.split(":")[0];
4946-
}
4947-
if (cleanValue.indexOf("#") >= 0) {
4948-
cleanValue = cleanValue.split("#")[1];
4949-
}
4950-
return cleanValue;
4951-
};
4952-
4953-
const headerKey = findKey(output.headers, "x-amzn-errortype");
4954-
if (headerKey !== undefined) {
4955-
return sanitizeErrorCode(output.headers[headerKey]);
4956-
}
4957-
4958-
if (data.code !== undefined) {
4959-
return sanitizeErrorCode(data.code);
4960-
}
4961-
4962-
if (data["__type"] !== undefined) {
4963-
return sanitizeErrorCode(data["__type"]);
4964-
}
4965-
};

clients/client-s3/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
"@smithy/util-stream": "^2.1.3",
8080
"@smithy/util-utf8": "^2.1.1",
8181
"@smithy/util-waiter": "^2.1.3",
82-
"fast-xml-parser": "4.2.5",
8382
"tslib": "^2.5.0"
8483
},
8584
"devDependencies": {

clients/client-s3/src/models/models_0.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// smithy-typescript generated code
22
import { ExceptionOptionType as __ExceptionOptionType, SENSITIVE_STRING } from "@smithy/smithy-client";
3-
43
import { StreamingBlobTypes } from "@smithy/types";
54

65
import { S3ServiceException as __BaseException } from "./S3ServiceException";

clients/client-s3/src/models/models_1.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// smithy-typescript generated code
22
import { ExceptionOptionType as __ExceptionOptionType, SENSITIVE_STRING } from "@smithy/smithy-client";
3-
43
import { StreamingBlobTypes } from "@smithy/types";
54

65
import {
@@ -28,7 +27,6 @@ import {
2827
StorageClass,
2928
Tag,
3029
} from "./models_0";
31-
3230
import { S3ServiceException as __BaseException } from "./S3ServiceException";
3331

3432
/**

clients/client-s3/src/protocols/Aws_restXml.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// smithy-typescript generated code
2+
import { loadRestXmlErrorCode, parseXmlBody as parseBody, parseXmlErrorBody as parseErrorBody } from "@aws-sdk/core";
23
import { XmlNode as __XmlNode, XmlText as __XmlText } from "@aws-sdk/xml-builder";
34
import { requestBuilder as rb } from "@smithy/core";
45
import {
@@ -16,7 +17,6 @@ import {
1617
expectUnion as __expectUnion,
1718
extendedEncodeURIComponent as __extendedEncodeURIComponent,
1819
getArrayIfSingleItem as __getArrayIfSingleItem,
19-
getValueFromTextNode as __getValueFromTextNode,
2020
map,
2121
parseBoolean as __parseBoolean,
2222
parseRfc3339DateTimeWithOffset as __parseRfc3339DateTimeWithOffset,
@@ -33,7 +33,6 @@ import {
3333
SdkStreamSerdeContext as __SdkStreamSerdeContext,
3434
SerdeContext as __SerdeContext,
3535
} from "@smithy/types";
36-
import { XMLParser } from "fast-xml-parser";
3736

3837
import {
3938
AbortMultipartUploadCommandInput,
@@ -10349,47 +10348,3 @@ const _xatd = "x-amz-tagging-directive";
1034910348
const _xavi = "x-amz-version-id";
1035010349
const _xawrl = "x-amz-website-redirect-location";
1035110350
const _xi = "x-id";
10352-
10353-
const parseBody = (streamBody: any, context: __SerdeContext): any =>
10354-
collectBodyString(streamBody, context).then((encoded) => {
10355-
if (encoded.length) {
10356-
const parser = new XMLParser({
10357-
attributeNamePrefix: "",
10358-
htmlEntities: true,
10359-
ignoreAttributes: false,
10360-
ignoreDeclaration: true,
10361-
parseTagValue: false,
10362-
trimValues: false,
10363-
tagValueProcessor: (_: any, val: any) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
10364-
});
10365-
parser.addEntity("#xD", "\r");
10366-
parser.addEntity("#10", "\n");
10367-
const parsedObj = parser.parse(encoded);
10368-
const textNodeName = "#text";
10369-
const key = Object.keys(parsedObj)[0];
10370-
const parsedObjToReturn = parsedObj[key];
10371-
if (parsedObjToReturn[textNodeName]) {
10372-
parsedObjToReturn[key] = parsedObjToReturn[textNodeName];
10373-
delete parsedObjToReturn[textNodeName];
10374-
}
10375-
return __getValueFromTextNode(parsedObjToReturn);
10376-
}
10377-
return {};
10378-
});
10379-
10380-
const parseErrorBody = async (errorBody: any, context: __SerdeContext) => {
10381-
const value = await parseBody(errorBody, context);
10382-
if (value.Error) {
10383-
value.Error.message = value.Error.message ?? value.Error.Message;
10384-
}
10385-
return value;
10386-
};
10387-
10388-
const loadRestXmlErrorCode = (output: __HttpResponse, data: any): string | undefined => {
10389-
if (data?.Code !== undefined) {
10390-
return data.Code;
10391-
}
10392-
if (output.statusCode == 404) {
10393-
return "NotFound";
10394-
}
10395-
};

packages/core/src/protocols/json/parseJsonBody.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ import { collectBodyString } from "../common";
55
export const parseJsonBody = (streamBody: any, context: SerdeContext): any =>
66
collectBodyString(streamBody, context).then((encoded) => {
77
if (encoded.length) {
8-
return JSON.parse(encoded);
8+
try {
9+
return JSON.parse(encoded);
10+
} catch (e: any) {
11+
if (e?.name === "SyntaxError") {
12+
Object.defineProperty(e, "$responseBodyText", {
13+
value: encoded,
14+
});
15+
}
16+
throw e;
17+
}
918
}
1019
return {};
1120
});

packages/core/src/protocols/xml/parseXmlBody.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ export const parseXmlBody = (streamBody: any, context: SerdeContext): any =>
1818
});
1919
parser.addEntity("#xD", "\r");
2020
parser.addEntity("#10", "\n");
21-
const parsedObj = parser.parse(encoded);
21+
22+
let parsedObj;
23+
try {
24+
parsedObj = parser.parse(encoded);
25+
} catch (e: any) {
26+
if (e && typeof e === "object") {
27+
Object.defineProperty(e, "$responseBodyText", {
28+
value: encoded,
29+
});
30+
}
31+
throw e;
32+
}
33+
2234
const textNodeName = "#text";
2335
const key = Object.keys(parsedObj)[0];
2436
const parsedObjToReturn = parsedObj[key];

0 commit comments

Comments
 (0)