{"version":3,"file":"serviceError.mjs","sources":["../../../src/utils/serviceError.ts"],"sourcesContent":["import { parseJsonError as parseAwsJsonError, } from '@aws-amplify/core/internals/aws-client-utils';\nimport { RestApiError } from '../errors';\n/**\n * Parses both AWS and non-AWS error responses coming from the users' backend code.\n * * AWS errors generated by the AWS services(e.g. API Gateway, Bedrock). They can be Signature errors,\n *   ClockSkew errors, etc. These responses will be parsed to errors with proper name and message from the AWS\n *   services.\n * * non-AWS errors thrown by the user code. They can contain any headers or body. Users need to access the\n *   error.response to get the headers and body and parse them accordingly. The JS error name and message will\n *   be `UnknownError` and `Unknown error` respectively.\n */\nexport const parseRestApiServiceError = async (response) => {\n    if (!response) {\n        // Response is not considered an error.\n        return;\n    }\n    const parsedAwsError = await parseAwsJsonError(stubErrorResponse(response));\n    if (!parsedAwsError) {\n        // Response is not considered an error.\n    }\n    else {\n        const bodyText = await response.body?.text();\n        return buildRestApiError(parsedAwsError, {\n            statusCode: response.statusCode,\n            headers: response.headers,\n            body: bodyText,\n        });\n    }\n};\n/**\n * The response object needs to be stub here because the parseAwsJsonError assumes the response body to be valid JSON.\n * Although this is true for AWS services, it is not true for responses from user's code. Once the response body is\n * unwrapped as JSON(and fail), it cannot be read as text again. Therefore, we need to stub the response body here to\n * make sure we can read the error response body as a JSON, and may fall back to read as text if it is not a valid JSON.\n */\nconst stubErrorResponse = (response) => {\n    let bodyTextPromise;\n    const bodyProxy = new Proxy(response.body, {\n        get(target, prop, receiver) {\n            if (prop === 'json') {\n                // For potential AWS errors, error parser will try to parse the body as JSON first.\n                return async () => {\n                    if (!bodyTextPromise) {\n                        bodyTextPromise = target.text();\n                    }\n                    try {\n                        return JSON.parse(await bodyTextPromise);\n                    }\n                    catch (error) {\n                        // If response body is not a valid JSON, we stub it to be an empty object and eventually parsed\n                        // as an unknown error\n                        return {};\n                    }\n                };\n            }\n            else if (prop === 'text') {\n                // For non-AWS errors, users can access the body as a string as a fallback.\n                return async () => {\n                    if (!bodyTextPromise) {\n                        bodyTextPromise = target.text();\n                    }\n                    return bodyTextPromise;\n                };\n            }\n            else {\n                return Reflect.get(target, prop, receiver);\n            }\n        },\n    });\n    const responseProxy = new Proxy(response, {\n        get(target, prop, receiver) {\n            if (prop === 'body') {\n                return bodyProxy;\n            }\n            else {\n                return Reflect.get(target, prop, receiver);\n            }\n        },\n    });\n    return responseProxy;\n};\n/**\n * Utility to create a new RestApiError from a service error.\n */\nconst buildRestApiError = (error, response) => {\n    const restApiError = new RestApiError({\n        name: error?.name,\n        message: error.message,\n        underlyingError: error,\n        response,\n    });\n    // $metadata is only required for backwards compatibility.\n    return Object.assign(restApiError, { $metadata: error.$metadata });\n};\n"],"names":["parseAwsJsonError"],"mappings":";;;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,wBAAwB,GAAG,OAAO,QAAQ,KAAK;AAC5D,IAAI,IAAI,CAAC,QAAQ,EAAE;AACnB;AACA,QAAQ;AACR,IAAI;AACJ,IAAI,MAAM,cAAc,GAAG,MAAMA,cAAiB,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;AAC/E,IAAI,IAAI,CAAC,cAAc,EAAE;AAGzB,SAAS;AACT,QAAQ,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE;AACpD,QAAQ,OAAO,iBAAiB,CAAC,cAAc,EAAE;AACjD,YAAY,UAAU,EAAE,QAAQ,CAAC,UAAU;AAC3C,YAAY,OAAO,EAAE,QAAQ,CAAC,OAAO;AACrC,YAAY,IAAI,EAAE,QAAQ;AAC1B,SAAS,CAAC;AACV,IAAI;AACJ;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,iBAAiB,GAAG,CAAC,QAAQ,KAAK;AACxC,IAAI,IAAI,eAAe;AACvB,IAAI,MAAM,SAAS,GAAG,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE;AAC/C,QAAQ,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,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