-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathErrorUtils.js
135 lines (124 loc) · 3.98 KB
/
ErrorUtils.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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import CONST from '../CONST';
import DateUtils from './DateUtils';
import * as Localize from './Localize';
/**
* @param {Object} response
* @param {Number} response.jsonCode
* @param {String} response.message
* @returns {String}
*/
function getAuthenticateErrorMessage(response) {
switch (response.jsonCode) {
case CONST.JSON_CODE.UNABLE_TO_RETRY:
return 'session.offlineMessageRetry';
case 401:
return 'passwordForm.error.incorrectLoginOrPassword';
case 402:
// If too few characters are passed as the password, the WAF will pass it to the API as an empty
// string, which results in a 402 error from Auth.
if (response.message === '402 Missing partnerUserSecret') {
return 'passwordForm.error.incorrectLoginOrPassword';
}
return 'passwordForm.error.twoFactorAuthenticationEnabled';
case 403:
if (response.message === 'Invalid code') {
return 'passwordForm.error.incorrect2fa';
}
return 'passwordForm.error.invalidLoginOrPassword';
case 404:
return 'passwordForm.error.unableToResetPassword';
case 405:
return 'passwordForm.error.noAccess';
case 413:
return 'passwordForm.error.accountLocked';
default:
return 'passwordForm.error.fallback';
}
}
/**
* Method used to get an error object with microsecond as the key.
* @param {String} error - error key or message to be saved
* @return {Object}
*
*/
function getMicroSecondOnyxError(error) {
return {[DateUtils.getMicroseconds()]: error};
}
/**
* @param {Object} onyxData
* @param {Object} onyxData.errors
* @returns {String}
*/
function getLatestErrorMessage(onyxData) {
if (_.isEmpty(onyxData.errors)) {
return '';
}
return _.chain(onyxData.errors || [])
.keys()
.sortBy()
.reverse()
.map((key) => onyxData.errors[key])
.first()
.value();
}
/**
* @param {Object} onyxData
* @param {Object} onyxData.errorFields
* @param {String} fieldName
* @returns {Object}
*/
function getLatestErrorField(onyxData, fieldName) {
const errorsForField = lodashGet(onyxData, ['errorFields', fieldName], {});
if (_.isEmpty(errorsForField)) {
return {};
}
return _.chain(errorsForField)
.keys()
.sortBy()
.reverse()
.map((key) => ({[key]: errorsForField[key]}))
.first()
.value();
}
/**
* @param {Object} onyxData
* @param {Object} onyxData.errorFields
* @param {String} fieldName
* @returns {Object}
*/
function getEarliestErrorField(onyxData, fieldName) {
const errorsForField = lodashGet(onyxData, ['errorFields', fieldName], {});
if (_.isEmpty(errorsForField)) {
return {};
}
return _.chain(errorsForField)
.keys()
.sortBy()
.map((key) => ({[key]: errorsForField[key]}))
.first()
.value();
}
/**
* Method used to generate error message for given inputID
* @param {Object} errors - An object containing current errors in the form
* @param {String} inputID
* @param {String|Array} message - Message to assign to the inputID errors
*
*/
function addErrorMessage(errors, inputID, message) {
if (!message || !inputID) {
return;
}
const errorList = errors;
const translatedMessage = Localize.translateIfPhraseKey(message);
if (_.isEmpty(errorList[inputID])) {
errorList[inputID] = [translatedMessage, {isTranslated: true}];
} else if (_.isString(errorList[inputID])) {
errorList[inputID] = [`${errorList[inputID]}\n${translatedMessage}`, {isTranslated: true}];
} else {
errorList[inputID][0] = `${errorList[inputID][0]}\n${translatedMessage}`;
}
}
export {getAuthenticateErrorMessage, getMicroSecondOnyxError, getLatestErrorMessage, getLatestErrorField, getEarliestErrorField, addErrorMessage};