{"version":3,"file":"createCancellableOperation.mjs","sources":["../../../src/utils/createCancellableOperation.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { CanceledError } from '../errors';\nimport { parseRestApiServiceError } from './serviceError';\nimport { logger } from './logger';\n/**\n * @internal\n */\nexport function createCancellableOperation(handler, abortController, operationType, timeout) {\n    const abortSignal = abortController.signal;\n    let abortReason;\n    if (timeout != null) {\n        if (timeout < 0) {\n            throw new Error('Timeout must be a non-negative number');\n        }\n        setTimeout(() => {\n            abortReason = 'TimeoutError';\n            abortController.abort(abortReason);\n        }, timeout);\n    }\n    const job = async () => {\n        try {\n            const response = await handler();\n            if (response.statusCode >= 300) {\n                throw await parseRestApiServiceError(response);\n            }\n            return response;\n        }\n        catch (error) {\n            if (error.name === 'AbortError' || abortSignal?.aborted === true) {\n                // Check if timeout caused the abort\n                const isTimeout = abortReason && abortReason === 'TimeoutError';\n                if (isTimeout) {\n                    const timeoutError = new Error(`Request timeout after ${timeout}ms`);\n                    timeoutError.name = 'TimeoutError';\n                    logger.debug(timeoutError);\n                    throw timeoutError;\n                }\n                else {\n                    const message = abortReason ?? abortSignal.reason;\n                    const canceledError = new CanceledError({\n                        ...(message && { message }),\n                        underlyingError: error,\n                        recoverySuggestion: 'The API request was explicitly canceled. If this is not intended, validate if you called the `cancel()` function on the API request erroneously.',\n                    });\n                    logger.debug(canceledError);\n                    throw canceledError;\n                }\n            }\n            logger.debug(error);\n            throw error;\n        }\n    };\n    if (operationType === 'internal') {\n        return job();\n    }\n    else {\n        const cancel = (abortMessage) => {\n            if (abortSignal.aborted === true) {\n                return;\n            }\n            abortController.abort(abortMessage);\n            // If abort reason is not supported, set a scoped reasons instead. The reason property inside an\n            // AbortSignal is a readonly property and trying to set it would throw an error.\n            if (abortMessage && abortSignal.reason !== abortMessage) {\n                abortReason = abortMessage;\n            }\n        };\n        return { response: job(), cancel };\n    }\n}\n"],"names":[],"mappings":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