Skip to content

(chore): Added early error message for configuration error #14222

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

Closed
wants to merge 12 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -16,12 +16,27 @@ describe('Analytics KinesisFirehose Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { KinesisFirehose: providedConfig },
});
@@ -30,6 +45,7 @@ describe('Analytics KinesisFirehose Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
bufferSize: undefined,
Original file line number Diff line number Diff line change
@@ -16,12 +16,27 @@ describe('Analytics Kinesis Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Kinesis: kinesisConfig },
});
@@ -30,6 +45,7 @@ describe('Analytics Kinesis Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
bufferSize: undefined,
Original file line number Diff line number Diff line change
@@ -18,12 +18,27 @@ describe('Analytics Personalize Provider Util: resolveConfig', () => {
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Personalize: providedConfig },
});
@@ -35,6 +50,7 @@ describe('Analytics Personalize Provider Util: resolveConfig', () => {
});

it('use default config for optional fields', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
const requiredFields = {
region: 'us-east-1',
trackingId: 'trackingId1',
Original file line number Diff line number Diff line change
@@ -16,12 +16,27 @@ describe('Analytics Pinpoint Provider Util: resolveConfig', () => {
};
// create spies
const getConfigSpy = jest.spyOn(Amplify, 'getConfig');
const assertConfiguredSpy = jest.spyOn(Amplify, 'assertConfigured');

beforeEach(() => {
getConfigSpy.mockReset();
assertConfiguredSpy.mockReset();
});

it('throws if Amplify is not configured', () => {
assertConfiguredSpy.mockImplementation(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(resolveConfig).toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

it('returns required config', () => {
assertConfiguredSpy.mockImplementation(jest.fn());
getConfigSpy.mockReturnValue({
Analytics: { Pinpoint: pinpointConfig },
});
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import {
import { DEFAULT_KINESIS_FIREHOSE_CONFIG } from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a few of these occur right before we call Amplify.getConfig(). Do we always want to assert configured with getConfig? If so, could we include this in the getConfig behavior to remove some of the amount of change happening here?

Also, there are a number of places where this isn't couple with getConfig. This seems counterintuitive, right? Are these other callsites still calling getConfig, but a few layers of abstraction removed? Is there a pattern for accessing the config different from using getConfig?

const config = Amplify.getConfig().Analytics?.KinesisFirehose;
const {
region,
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import {
import { DEFAULT_KINESIS_CONFIG } from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
const config = Amplify.getConfig().Analytics?.Kinesis;
const {
region,
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import {
} from './constants';

export const resolveConfig = () => {
Amplify.assertConfigured();
const config = Amplify.getConfig().Analytics?.Personalize;
const {
region,
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import {
* @internal
*/
export const resolveConfig = () => {
Amplify.assertConfigured();
const { appId, region, bufferSize, flushSize, flushInterval, resendLimit } =
Amplify.getConfig().Analytics?.Pinpoint ?? {};
assertValidationError(!!appId, AnalyticsValidationErrorCode.NoAppId);
Original file line number Diff line number Diff line change
@@ -26,10 +26,13 @@ const mockUnauthenticatedHandler = jest.mocked(unauthenticatedHandler);
const mockParseJsonError = jest.mocked(parseJsonError);
const mockGetRetryDecider = jest.mocked(getRetryDecider);
const mockFetchAuthSession = jest.fn();
const mockAssertConfigured = jest.fn();
const mockAmplifyInstance = {
Auth: {
fetchAuthSession: mockFetchAuthSession,
},
isConfigured: true,
assertConfigured: mockAssertConfigured,
} as any as AmplifyClassV6;

const successResponse = {
2 changes: 2 additions & 0 deletions packages/api-rest/__tests__/apis/common/publicApis.test.ts
Original file line number Diff line number Diff line change
@@ -49,11 +49,13 @@ const mockConfig = {
const mockParseJsonError = parseJsonError as jest.Mock;
const mockRestHeaders = jest.fn();
const mockGetConfig = jest.fn();
const mockAssertConfigured = jest.fn();
const mockAmplifyInstance = {
Auth: {
fetchAuthSession: mockFetchAuthSession,
},
getConfig: mockGetConfig,
assertConfigured: mockAssertConfigured,
libraryOptions: {
API: {
REST: {
1 change: 1 addition & 0 deletions packages/api-rest/src/apis/common/internalPost.ts
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@
* @param postInput.abortController The abort controller used to cancel the POST request
* @returns a {@link RestApiResponse}
*
* @throws an {@link AmplifyError} with `Network Error` as the `message` when the external resource is unreachable due to one

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

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
@@ -57,6 +57,7 @@
amplify: AmplifyClassV6,
{ url, options, abortController }: InternalPostInput,
): Promise<RestApiResponse> => {
amplify.assertConfigured();
const controller = abortController ?? new AbortController();
const responsePromise = createCancellableOperation(async () => {
const response = transferHandler(
1 change: 1 addition & 0 deletions packages/api-rest/src/apis/common/publicApis.ts
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ const publicHandler = (
method: string,
) =>
createCancellableOperation(async abortSignal => {
amplify.assertConfigured();
const { apiName, options: apiOptions = {}, path: apiPath } = options;
const url = resolveApiUrl(
amplify,
1 change: 1 addition & 0 deletions packages/api-rest/src/apis/common/transferHandler.ts
Original file line number Diff line number Diff line change
@@ -128,6 +128,7 @@ const getRetryDeciderFromStrategy = (
const resolveCredentials = async (
amplify: AmplifyClassV6,
): Promise<AWSCredentials | null> => {
amplify.assertConfigured();
try {
const { credentials } = await amplify.Auth.fetchAuthSession();
if (credentials) {
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import { authAPITestParams } from './testUtils/authApiTestParams';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('../../../src/client/utils/store');
jest.mock(
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ jest.mock('../../../src/providers/cognito/factories');

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock(
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider',
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
@@ -34,6 +34,7 @@ describe('fetchUserAttributes', () => {
const mockCreateCognitoUserPoolEndpointResolver = jest.mocked(
createCognitoUserPoolEndpointResolver,
);
const mockAssertConfigured = Amplify.assertConfigured as jest.Mock;

beforeAll(() => {
setUpGetConfig(Amplify);
@@ -43,6 +44,7 @@ describe('fetchUserAttributes', () => {
});

beforeEach(() => {
mockAssertConfigured.mockReturnValue(undefined);
mockGetUser.mockResolvedValue({
UserAttributes: [
{ Name: 'email', Value: 'XXXXXXXXXXXXX' },
@@ -60,6 +62,7 @@ describe('fetchUserAttributes', () => {
mockGetUser.mockReset();
mockFetchAuthSession.mockClear();
mockCreateGetUserClient.mockClear();
mockAssertConfigured.mockReset();
});

it('should return the current user attributes into a map format', async () => {
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Original file line number Diff line number Diff line change
@@ -13,7 +13,11 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { Auth: { getTokens: jest.fn() }, getConfig: jest.fn(() => ({})) },
Amplify: {
Auth: { getTokens: jest.fn() },
getConfig: jest.fn(() => ({})),
assertConfigured: jest.fn(),
},
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
@@ -25,12 +29,14 @@ describe('getCurrentUser', () => {
const mockedUsername = 'XXXXXXXXXXXXXX';
// assert mocks
const mockGetTokensFunction = Amplify.Auth.getTokens as jest.Mock;
const mockAssertConfigured = Amplify.assertConfigured as jest.Mock;

beforeAll(() => {
setUpGetConfig(Amplify);
});

beforeEach(() => {
mockAssertConfigured.mockReturnValue(undefined);
mockGetTokensFunction.mockResolvedValue({
accessToken: decodeJWT(mockAccessToken),
idToken: {
@@ -48,6 +54,7 @@ describe('getCurrentUser', () => {

afterEach(() => {
mockGetTokensFunction.mockReset();
mockAssertConfigured.mockReset();
});

it('should get current user', async () => {
@@ -71,4 +78,16 @@ describe('getCurrentUser', () => {
expect(error.name).toBe(USER_UNAUTHENTICATED_EXCEPTION);
}
});

it('throws if Amplify is not configured', async () => {
mockAssertConfigured.mockImplementationOnce(() => {
throw new Error(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});

expect(getCurrentUser()).rejects.toThrow(
'Amplify has not been configured. Please call Amplify.configure() before using this service.',
);
});
});
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ import { setUpGetConfig } from './testUtils/setUpGetConfig';

jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})) },
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
Loading