56 lines
2.0 KiB
JavaScript
56 lines
2.0 KiB
JavaScript
import { HttpResponse } from "@smithy/protocol-http";
|
|
export const deserializerMiddleware = (options, deserializer) => (next, context) => async (args) => {
|
|
const { response } = await next(args);
|
|
try {
|
|
const parsed = await deserializer(response, options);
|
|
return {
|
|
response,
|
|
output: parsed,
|
|
};
|
|
}
|
|
catch (error) {
|
|
Object.defineProperty(error, "$response", {
|
|
value: response,
|
|
});
|
|
if (!("$metadata" in error)) {
|
|
const hint = `Deserialization error: to see the raw response, inspect the hidden field {error}.$response on this object.`;
|
|
try {
|
|
error.message += "\n " + hint;
|
|
}
|
|
catch (e) {
|
|
if (!context.logger || context.logger?.constructor?.name === "NoOpLogger") {
|
|
console.warn(hint);
|
|
}
|
|
else {
|
|
context.logger?.warn?.(hint);
|
|
}
|
|
}
|
|
if (typeof error.$responseBodyText !== "undefined") {
|
|
if (error.$response) {
|
|
error.$response.body = error.$responseBodyText;
|
|
}
|
|
}
|
|
try {
|
|
if (HttpResponse.isInstance(response)) {
|
|
const { headers = {} } = response;
|
|
const headerEntries = Object.entries(headers);
|
|
error.$metadata = {
|
|
httpStatusCode: response.statusCode,
|
|
requestId: findHeader(/^x-[\w-]+-request-?id$/, headerEntries),
|
|
extendedRequestId: findHeader(/^x-[\w-]+-id-2$/, headerEntries),
|
|
cfId: findHeader(/^x-[\w-]+-cf-id$/, headerEntries),
|
|
};
|
|
}
|
|
}
|
|
catch (e) {
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
};
|
|
const findHeader = (pattern, headers) => {
|
|
return (headers.find(([k]) => {
|
|
return k.match(pattern);
|
|
}) || [void 0, void 1])[1];
|
|
};
|