@aws-amplify/core
Version:
Core category of aws-amplify
52 lines (50 loc) • 1.73 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseJsonBody = exports.parseJsonError = void 0;
const responseInfo_1 = require("./responseInfo");
/**
* Utility functions for serializing and deserializing of JSON protocol in general(including: REST-JSON, JSON-RPC, etc.)
*/
/**
* Error parser for AWS JSON protocol.
*/
const parseJsonError = async (response) => {
if (!response || response.statusCode < 300) {
return;
}
const body = await (0, exports.parseJsonBody)(response);
const sanitizeErrorCode = (rawValue) => {
const [cleanValue] = rawValue.toString().split(/[,:]+/);
if (cleanValue.includes('#')) {
return cleanValue.split('#')[1];
}
return cleanValue;
};
const code = sanitizeErrorCode(response.headers['x-amzn-errortype'] ??
body.code ??
body.__type ??
'UnknownError');
const message = body.message ?? body.Message ?? 'Unknown error';
const error = new Error(message);
return Object.assign(error, {
name: code,
$metadata: (0, responseInfo_1.parseMetadata)(response),
});
};
exports.parseJsonError = parseJsonError;
/**
* Parse JSON response body to JavaScript object.
*/
const parseJsonBody = async (response) => {
if (!response.body) {
throw new Error('Missing response payload');
}
const output = await response.body.json();
return Object.assign(output, {
$metadata: (0, responseInfo_1.parseMetadata)(response),
});
};
exports.parseJsonBody = parseJsonBody;
//# sourceMappingURL=json.js.map
;