Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: show underlying errors in fetch / xhr #13687

Merged
Merged
2 changes: 1 addition & 1 deletion packages/api-rest/src/apis/common/internalPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* @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

Check warning on line 49 in packages/api-rest/src/apis/common/internalPost.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/api-rest

The type 'AmplifyError' is undefined
* of the following reasons:
* 1. no network connection
* 2. CORS error
Expand Down
26 changes: 13 additions & 13 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -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",
Expand Down Expand Up @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand All @@ -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)",
Expand Down Expand Up @@ -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)",
Expand All @@ -497,7 +497,7 @@
"name": "[Storage] uploadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ uploadData }",
"limit": "19.90 kB"
"limit": "19.95 kB"
}
]
}
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/clients/handlers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/errors/errorHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const amplifyErrorMap: AmplifyErrorMap<AmplifyErrorCode> = {
[AmplifyErrorCode.Unknown]: {
message: 'An unknown error occurred.',
},
[AmplifyErrorCode.NetworkError]: {
message: 'A network error has occurred.',
},
};

export const assert: AssertionFunction<AmplifyErrorCode> =
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum AmplifyErrorCode {
NoEndpointId = 'NoEndpointId',
PlatformNotSupported = 'PlatformNotSupported',
Unknown = 'Unknown',
NetworkError = 'NetworkError',
}

export interface AmplifyErrorParams<ErrorCode extends string = string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading