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
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
10 changes: 6 additions & 4 deletions packages/core/src/clients/handlers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { HttpRequest, HttpResponse, HttpTransferOptions } from '../types/http';
import { TransferHandler } from '../types/core';
import { ApiError } from '../../errors';
import { withMemoization } from '../utils/memoization';

const shouldSendBody = (method: string) =>
Expand All @@ -28,11 +29,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 ApiError({
joon-won marked this conversation as resolved.
Show resolved Hide resolved
message: 'Network Error',
name: 'NETWORK_ERROR',
joon-won marked this conversation as resolved.
Show resolved Hide resolved
underlyingError: e,
});
}
throw e;
}
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