1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | import { AuthErrorMessages, AuthErrorTypes } from './types';
|
15 | import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
16 | import { AuthErrorStrings } from './common/AuthErrorStrings';
|
17 |
|
18 | const logger = new Logger('AuthError');
|
19 |
|
20 | export class AuthError extends Error {
|
21 | public log: string;
|
22 | constructor(type: AuthErrorTypes) {
|
23 | const { message, log } = authErrorMessages[type];
|
24 | super(message);
|
25 |
|
26 |
|
27 |
|
28 | this.constructor = AuthError;
|
29 | Object.setPrototypeOf(this, AuthError.prototype);
|
30 |
|
31 | this.name = 'AuthError';
|
32 | this.log = log || message;
|
33 |
|
34 | logger.error(this.log);
|
35 | }
|
36 | }
|
37 |
|
38 | export class NoUserPoolError extends AuthError {
|
39 | constructor(type: AuthErrorTypes) {
|
40 | super(type);
|
41 |
|
42 |
|
43 |
|
44 | this.constructor = NoUserPoolError;
|
45 | Object.setPrototypeOf(this, NoUserPoolError.prototype);
|
46 |
|
47 | this.name = 'NoUserPoolError';
|
48 | }
|
49 | }
|
50 |
|
51 | 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 amplify packages in your node_modules.
|
62 | Refer to our docs site for help upgrading Amplify packages (https://docs.amplify.aws/lib/troubleshooting/upgrading/q/platform/js)
|
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 | This error is typically caused by one of the following scenarios:
|
71 |
|
72 | 1. Did you run \`amplify push\` after adding auth via \`amplify add auth\`?
|
73 | See https://aws-amplify.github.io/docs/js/authentication#amplify-project-setup for more information
|
74 |
|
75 | 2. This could also be caused by multiple conflicting versions of amplify packages, see (https://docs.amplify.aws/lib/troubleshooting/upgrading/q/platform/js) for help upgrading Amplify packages.
|
76 | `,
|
77 | },
|
78 | emptyUsername: {
|
79 | message: AuthErrorStrings.EMPTY_USERNAME,
|
80 | },
|
81 |
|
82 | invalidUsername: {
|
83 | message: AuthErrorStrings.INVALID_USERNAME,
|
84 | },
|
85 | emptyPassword: {
|
86 | message: AuthErrorStrings.EMPTY_PASSWORD,
|
87 | },
|
88 | emptyCode: {
|
89 | message: AuthErrorStrings.EMPTY_CODE,
|
90 | },
|
91 | signUpError: {
|
92 | message: AuthErrorStrings.SIGN_UP_ERROR,
|
93 | log: 'The first parameter should either be non-null string or object',
|
94 | },
|
95 | noMFA: {
|
96 | message: AuthErrorStrings.NO_MFA,
|
97 | },
|
98 | invalidMFA: {
|
99 | message: AuthErrorStrings.INVALID_MFA,
|
100 | },
|
101 | emptyChallengeResponse: {
|
102 | message: AuthErrorStrings.EMPTY_CHALLENGE,
|
103 | },
|
104 | noUserSession: {
|
105 | message: AuthErrorStrings.NO_USER_SESSION,
|
106 | },
|
107 | deviceConfig: {
|
108 | message: AuthErrorStrings.DEVICE_CONFIG,
|
109 | },
|
110 | networkError: {
|
111 | message: AuthErrorStrings.NETWORK_ERROR,
|
112 | },
|
113 | default: {
|
114 | message: AuthErrorStrings.DEFAULT_MSG,
|
115 | },
|
116 | };
|