UNPKG

4.06 kBPlain TextView Raw
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
14import { AuthErrorMessages, AuthErrorTypes } from './types';
15import { ConsoleLogger as Logger } from '@aws-amplify/core';
16import { AuthErrorStrings } from './common/AuthErrorStrings';
17
18const logger = new Logger('AuthError');
19
20export class AuthError extends Error {
21 public log: string;
22 constructor(type: AuthErrorTypes) {
23 const { message, log } = authErrorMessages[type];
24 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 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
38export class NoUserPoolError extends AuthError {
39 constructor(type: AuthErrorTypes) {
40 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 this.constructor = NoUserPoolError;
45 Object.setPrototypeOf(this, NoUserPoolError.prototype);
46
47 this.name = 'NoUserPoolError';
48 }
49}
50
51export 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 // TODO: should include a list of valid sign-in types
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};