This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
auth-errors.js
176 lines (159 loc) · 5.08 KB
/
auth-errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// provides functions to work with errors returned by the auth server.
'use strict';
define([
],
function () {
var t = function (msg) {
return msg;
};
var ERROR_TO_CODE = {
UNEXPECTED_ERROR: 999,
INVALID_TOKEN: 110,
INVALID_TIMESTAMP: 111,
INVALID_NONCE: 115,
ACCOUNT_ALREADY_EXISTS: 101,
UNKNOWN_ACCOUNT: 102,
INCORRECT_EMAIL_CASE: 120,
INCORRECT_PASSWORD: 103,
UNVERIFIED_ACCOUNT: 104,
INVALID_VERIFICATION_CODE: 105,
INVALID_JSON: 106,
INVALID_PARAMETER: 107,
MISSING_PARAMETER: 108,
INVALID_REQUEST_SIGNATURE: 109,
MISSING_CONTENT_LENGTH_HEADER: 112,
REQUEST_TOO_LARGE: 113,
THROTTLED: 114,
SERVER_BUSY: 201,
ENDPOINT_NOT_SUPPORTED: 116,
// local only error codes
SERVICE_UNAVAILABLE: 998,
USER_CANCELED_LOGIN: 1001,
SESSION_EXPIRED: 1002,
COOKIES_STILL_DISABLED: 1003,
PASSWORDS_DO_NOT_MATCH: 1004,
WORKING: 1005,
COULD_NOT_GET_PP: 1006,
COULD_NOT_GET_TOS: 1007,
PASSWORDS_MUST_BE_DIFFERENT: 1008,
PASSWORD_TOO_SHORT: 1009,
PASSWORD_REQUIRED: 1010,
EMAIL_REQUIRED: 1011,
YEAR_OF_BIRTH_REQUIRED: 1012,
UNUSABLE_IMAGE: 1013
};
var CODE_TO_MESSAGES = {
// errors returned by the auth server
999: t('Unexpected error'),
110: t('Invalid token'),
111: t('Invalid timestamp in request signature'),
115: t('Invalid nonce in request signature'),
101: t('Account already exists'),
102: t('Unknown account'),
120: t('Incorrect email case'),
103: t('Incorrect password'),
104: t('Unverified account'),
105: t('Invalid verification code'),
106: t('Invalid JSON in request body'),
107: t('Invalid parameter in request body: %(param)s'),
108: t('Missing parameter in request body: %(param)s'),
109: t('Invalid request signature'),
112: t('Missing content-length header'),
113: t('Request body too large'),
114: t('Attempt limit exceeded'),
201: t('Server busy, try again soon'),
116: t('This endpoint is no longer supported'),
// local only error messages
998: t('System unavailable, try again soon'),
1002: t('Session expired. Sign in to continue.'),
1003: t('Cookies are still disabled'),
1004: t('Passwords do not match'),
1005: t('Working…'),
1006: t('Could not get Privacy Notice'),
1007: t('Could not get Terms of Service'),
1008: t('Your new password must be different'),
1009: t('Must be at least 8 characters'),
1010: t('Valid password required'),
1011: t('Valid email required'),
1012: t('Year of birth required'),
1013: t('A usable image was not found')
};
return {
ERROR_TO_CODE: ERROR_TO_CODE,
CODE_TO_MESSAGES: CODE_TO_MESSAGES,
/**
* Convert an error, a numeric code or string type to a message
*/
toMessage: function (err) {
var code;
if (typeof err === 'number') {
code = err;
} else if (err && err.forceMessage) {
return err.forceMessage;
// error from backend
} else if (err && typeof err.errno === 'number') {
code = err.errno;
// error from backend that only has a message. Print the message.
} else if (err && err.message) {
return err.message;
// probably a string. Try to convert it.
} else if (err && err.length) {
code = this.toCode(err);
} else {
// if there is no error, no error message, and no code,
// assume no response from the backend and
// the service is unavailable.
code = this.toCode('SERVICE_UNAVAILABLE');
}
return this.CODE_TO_MESSAGES[code] || err;
},
/**
* Fetch the translation context out of the server error.
*/
toContext: function (err) {
// For data returned by backend, see
// https://github.com/mozilla/fxa-auth-server/blob/master/error.js
try {
if (this.is(err, 'INVALID_PARAMETER')) {
return {
param: err.validation.keys
};
} else if (this.is(err, 'MISSING_PARAMETER')) {
return {
param: err.param
};
}
} catch (e) {
// handle invalid/unexpected data from the backend.
if (window.console && console.error) {
console.error('Error in auth-errors.js->toContext: %s', String(e));
}
}
return {};
},
/**
* Convert an error or a text type from ERROR_TO_CODE to a numeric code
*/
toCode: function (type) {
return type.errno || this.ERROR_TO_CODE[type] || type;
},
/**
* Synthesize an error of the given type with the supplied message.
*/
toError: function (type, message) {
var err = new Error(message);
err.errno = this.toCode(type);
return err;
},
/**
* Check if an error is of the given type
*/
is: function (error, type) {
var code = this.toCode(type);
return error.errno === code;
}
};
});