diff --git a/packages/api-rest/src/apis/common/internalPost.ts b/packages/api-rest/src/apis/common/internalPost.ts index 835c3581d56..574e5eb3da8 100644 --- a/packages/api-rest/src/apis/common/internalPost.ts +++ b/packages/api-rest/src/apis/common/internalPost.ts @@ -46,7 +46,7 @@ const cancelTokenMap = new WeakMap, AbortController>(); * @param postInput.abortController The abort controller used to cancel the POST request * @returns a {@link RestApiResponse} * - * @throws an {@link Error} with `Network error` as the `message` when the external resource is unreachable due to one + * @throws an {@link AmplifyError} with `Network Error` as the `message` when the external resource is unreachable due to one * of the following reasons: * 1. no network connection * 2. CORS error diff --git a/packages/aws-amplify/package.json b/packages/aws-amplify/package.json index 8508cc83082..c23c9eae63b 100644 --- a/packages/aws-amplify/package.json +++ b/packages/aws-amplify/package.json @@ -293,7 +293,7 @@ "name": "[Analytics] record (Pinpoint)", "path": "./dist/esm/analytics/index.mjs", "import": "{ record }", - "limit": "17.18 kB" + "limit": "17.25 kB" }, { "name": "[Analytics] record (Kinesis)", @@ -317,7 +317,7 @@ "name": "[Analytics] identifyUser (Pinpoint)", "path": "./dist/esm/analytics/index.mjs", "import": "{ identifyUser }", - "limit": "15.68 kB" + "limit": "15.75 kB" }, { "name": "[Analytics] enable", @@ -353,13 +353,13 @@ "name": "[Auth] resetPassword (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ resetPassword }", - "limit": "12.53 kB" + "limit": "12.55 kB" }, { "name": "[Auth] confirmResetPassword (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ confirmResetPassword }", - "limit": "12.47 kB" + "limit": "12.50 kB" }, { "name": "[Auth] signIn (Cognito)", @@ -371,7 +371,7 @@ "name": "[Auth] resendSignUpCode (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ resendSignUpCode }", - "limit": "12.49 kB" + "limit": "12.50 kB" }, { "name": "[Auth] confirmSignUp (Cognito)", @@ -389,19 +389,19 @@ "name": "[Auth] updateMFAPreference (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ updateMFAPreference }", - "limit": "11.83 kB" + "limit": "11.85 kB" }, { "name": "[Auth] fetchMFAPreference (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ fetchMFAPreference }", - "limit": "11.86 kB" + "limit": "11.90 kB" }, { "name": "[Auth] verifyTOTPSetup (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ verifyTOTPSetup }", - "limit": "12.71 kB" + "limit": "12.75 kB" }, { "name": "[Auth] updatePassword (Cognito)", @@ -419,7 +419,7 @@ "name": "[Auth] updateUserAttributes (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ updateUserAttributes }", - "limit": "11.95 kB" + "limit": "12.00 kB" }, { "name": "[Auth] getCurrentUser (Cognito)", @@ -431,7 +431,7 @@ "name": "[Auth] confirmUserAttribute (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ confirmUserAttribute }", - "limit": "12.71 kB" + "limit": "12.75 kB" }, { "name": "[Auth] signInWithRedirect (Cognito)", @@ -443,7 +443,7 @@ "name": "[Auth] fetchUserAttributes (Cognito)", "path": "./dist/esm/auth/index.mjs", "import": "{ fetchUserAttributes }", - "limit": "11.77 kB" + "limit": "11.80 kB" }, { "name": "[Auth] Basic Auth Flow (Cognito)", @@ -479,7 +479,7 @@ "name": "[Storage] getUrl (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ getUrl }", - "limit": "15.90 kB" + "limit": "15.95 kB" }, { "name": "[Storage] list (S3)", @@ -497,7 +497,7 @@ "name": "[Storage] uploadData (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ uploadData }", - "limit": "19.90 kB" + "limit": "19.95 kB" } ] } diff --git a/packages/core/package.json b/packages/core/package.json index 4fdbe7c02df..5c34ecc05be 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -82,13 +82,13 @@ "name": "Custom clients (fetch handler)", "path": "./dist/esm/clients/handlers/fetch.mjs", "import": "{ fetchTransferHandler }", - "limit": "500 B" + "limit": "575 B" }, { "name": "Custom clients (unauthenticated handler)", "path": "./dist/esm/clients/handlers/unauthenticated.mjs", "import": "{ unauthenticatedHandler }", - "limit": "1 kB" + "limit": "1.15 kB" }, { "name": "Custom clients (request signer)", diff --git a/packages/core/src/clients/handlers/fetch.ts b/packages/core/src/clients/handlers/fetch.ts index be8e5ac9520..30a37f210e2 100644 --- a/packages/core/src/clients/handlers/fetch.ts +++ b/packages/core/src/clients/handlers/fetch.ts @@ -3,7 +3,9 @@ import { HttpRequest, HttpResponse, HttpTransferOptions } from '../types/http'; import { TransferHandler } from '../types/core'; +import { AmplifyError } from '../../errors'; import { withMemoization } from '../utils/memoization'; +import { AmplifyErrorCode } from '../../types'; const shouldSendBody = (method: string) => !['HEAD', 'GET', 'DELETE'].includes(method.toUpperCase()); @@ -28,11 +30,12 @@ export const fetchTransferHandler: TransferHandler< credentials: withCrossDomainCredentials ? 'include' : 'same-origin', }); } catch (e) { - // TODO: needs to revise error handling in v6 - // For now this is a thin wrapper over original fetch error similar to cognito-identity-js package. - // Ref: https://github.com/aws-amplify/amplify-js/blob/4fbc8c0a2be7526aab723579b4c95b552195a80b/packages/amazon-cognito-identity-js/src/Client.js#L103-L108 if (e instanceof TypeError) { - throw new Error('Network error'); + throw new AmplifyError({ + name: AmplifyErrorCode.NetworkError, + message: 'A network error has occurred.', + underlyingError: e, + }); } throw e; } diff --git a/packages/core/src/errors/errorHelpers.ts b/packages/core/src/errors/errorHelpers.ts index a343c51f3f2..cb78f5534c9 100644 --- a/packages/core/src/errors/errorHelpers.ts +++ b/packages/core/src/errors/errorHelpers.ts @@ -15,6 +15,9 @@ const amplifyErrorMap: AmplifyErrorMap = { [AmplifyErrorCode.Unknown]: { message: 'An unknown error occurred.', }, + [AmplifyErrorCode.NetworkError]: { + message: 'A network error has occurred.', + }, }; export const assert: AssertionFunction = diff --git a/packages/core/src/types/errors.ts b/packages/core/src/types/errors.ts index 19ecec76935..3401616074f 100644 --- a/packages/core/src/types/errors.ts +++ b/packages/core/src/types/errors.ts @@ -5,6 +5,7 @@ export enum AmplifyErrorCode { NoEndpointId = 'NoEndpointId', PlatformNotSupported = 'PlatformNotSupported', Unknown = 'Unknown', + NetworkError = 'NetworkError', } export interface AmplifyErrorParams { diff --git a/packages/storage/src/providers/s3/utils/client/runtime/xhrTransferHandler.ts b/packages/storage/src/providers/s3/utils/client/runtime/xhrTransferHandler.ts index 371b4d8efa6..eabb0865e04 100644 --- a/packages/storage/src/providers/s3/utils/client/runtime/xhrTransferHandler.ts +++ b/packages/storage/src/providers/s3/utils/client/runtime/xhrTransferHandler.ts @@ -12,6 +12,7 @@ import { ConsoleLogger } from '@aws-amplify/core'; import { TransferProgressEvent } from '../../../../../types/common'; import { CanceledError } from '../../../../../errors/CanceledError'; +import { StorageError } from '../../../../../errors/StorageError'; import { ABORT_ERROR_CODE, @@ -80,10 +81,10 @@ export const xhrTransferHandler: TransferHandler< } xhr.addEventListener('error', () => { - const networkError = buildHandlerError( - NETWORK_ERROR_MESSAGE, - NETWORK_ERROR_CODE, - ); + const networkError = new StorageError({ + message: NETWORK_ERROR_MESSAGE, + name: NETWORK_ERROR_CODE, + }); logger.error(NETWORK_ERROR_MESSAGE); reject(networkError); xhr = null; // clean up request