1
/*
2
 * Copyright 2019-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
 * the License. A copy of the License is located at
6
 *
7
 *     http://aws.amazon.com/apache2.0/
8
 *
9
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
 * and limitations under the License.
12
 */
13

14
import { AuthErrorMessages, AuthErrorTypes } from './types';
15 1
import { ConsoleLogger as Logger } from '@aws-amplify/core';
16 1
import { AuthErrorStrings } from './common/AuthErrorStrings';
17

18 1
const logger = new Logger('AuthError');
19

20 1
export class AuthError extends Error {
21
	public log: string;
22 1
	constructor(type: AuthErrorTypes) {
23 1
		const { message, log } = authErrorMessages[type];
24 1
		super(message);
25

26
		// Hack for making the custom error class work when transpiled to es5
27
		// TODO: Delete the following 2 lines after we change the build target to >= es2015
28 1
		this.constructor = AuthError;
29 1
		Object.setPrototypeOf(this, AuthError.prototype);
30

31 1
		this.name = 'AuthError';
32 1
		this.log = log || message;
33

34 1
		logger.error(this.log);
35
	}
36 1
}
37

38 1
export class NoUserPoolError extends AuthError {
39
	constructor(type: AuthErrorTypes) {
40 1
		super(type);
41

42
		// Hack for making the custom error class work when transpiled to es5
43
		// TODO: Delete the following 2 lines after we change the build target to >= es2015
44 1
		this.constructor = NoUserPoolError;
45 1
		Object.setPrototypeOf(this, NoUserPoolError.prototype);
46

47 1
		this.name = 'NoUserPoolError';
48
	}
49 1
}
50

51 1
export const authErrorMessages: AuthErrorMessages = {
52
	noConfig: {
53
		message: AuthErrorStrings.DEFAULT_MSG,
54
		log: `
55
            Error: Amplify has not been configured correctly.
56
            This error is typically caused by one of the following scenarios:
57

58
            1. Make sure you're passing the awsconfig object to Amplify.configure() in your app's entry point
59
                See https://aws-amplify.github.io/docs/js/authentication#configure-your-app for more information
60
            
61
            2. There might be multiple conflicting versions of aws-amplify or amplify packages in your node_modules.
62
                Try deleting your node_modules folder and reinstalling the dependencies with \`yarn install\`
63
        `,
64
	},
65
	missingAuthConfig: {
66
		message: AuthErrorStrings.DEFAULT_MSG,
67
		log: `
68
            Error: Amplify has not been configured correctly. 
69
            The configuration object is missing required auth properties. 
70
            Did you run \`amplify push\` after adding auth via \`amplify add auth\`?
71
            See https://aws-amplify.github.io/docs/js/authentication#amplify-project-setup for more information
72
        `,
73
	},
74
	emptyUsername: {
75
		message: AuthErrorStrings.EMPTY_USERNAME,
76
	},
77
	// TODO: should include a list of valid sign-in types
78
	invalidUsername: {
79
		message: AuthErrorStrings.INVALID_USERNAME,
80
	},
81
	emptyPassword: {
82
		message: AuthErrorStrings.EMPTY_PASSWORD,
83
	},
84
	emptyCode: {
85
		message: AuthErrorStrings.EMPTY_CODE,
86
	},
87
	signUpError: {
88
		message: AuthErrorStrings.SIGN_UP_ERROR,
89
		log: 'The first parameter should either be non-null string or object',
90
	},
91
	noMFA: {
92
		message: AuthErrorStrings.NO_MFA,
93
	},
94
	invalidMFA: {
95
		message: AuthErrorStrings.INVALID_MFA,
96
	},
97
	emptyChallengeResponse: {
98
		message: AuthErrorStrings.EMPTY_CHALLENGE,
99
	},
100
	noUserSession: {
101
		message: AuthErrorStrings.NO_USER_SESSION,
102
	},
103
	default: {
104
		message: AuthErrorStrings.DEFAULT_MSG,
105
	},
106
};

Read our documentation on viewing source code .

Loading