-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27659 from software-mansion-labs/ts-migration/Err…
…orUtils [No QA][TS migration] Migrate 'ErrorUtils.js' lib to TypeScript
- Loading branch information
Showing
3 changed files
with
116 additions
and
136 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import CONST from '../CONST'; | ||
import DateUtils from './DateUtils'; | ||
import * as Localize from './Localize'; | ||
import Response from '../types/onyx/Response'; | ||
import {ErrorFields, Errors} from '../types/onyx/OnyxCommon'; | ||
import {TranslationFlatObject} from '../languages/types'; | ||
|
||
function getAuthenticateErrorMessage(response: Response): keyof TranslationFlatObject { | ||
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 error - error key or message to be saved | ||
*/ | ||
function getMicroSecondOnyxError(error: string): Record<number, string> { | ||
return {[DateUtils.getMicroseconds()]: error}; | ||
} | ||
|
||
type OnyxDataWithErrors = { | ||
errors?: Errors; | ||
}; | ||
|
||
function getLatestErrorMessage<TOnyxData extends OnyxDataWithErrors>(onyxData: TOnyxData): string { | ||
const errors = onyxData.errors ?? {}; | ||
|
||
if (Object.keys(errors).length === 0) { | ||
return ''; | ||
} | ||
|
||
const key = Object.keys(errors).sort().reverse()[0]; | ||
|
||
return errors[key]; | ||
} | ||
|
||
type OnyxDataWithErrorFields = { | ||
errorFields?: ErrorFields; | ||
}; | ||
|
||
function getLatestErrorField<TOnyxData extends OnyxDataWithErrorFields>(onyxData: TOnyxData, fieldName: string): Record<string, string> { | ||
const errorsForField = onyxData.errorFields?.[fieldName] ?? {}; | ||
|
||
if (Object.keys(errorsForField).length === 0) { | ||
return {}; | ||
} | ||
|
||
const key = Object.keys(errorsForField).sort().reverse()[0]; | ||
|
||
return {[key]: errorsForField[key]}; | ||
} | ||
|
||
function getEarliestErrorField<TOnyxData extends OnyxDataWithErrorFields>(onyxData: TOnyxData, fieldName: string): Record<string, string> { | ||
const errorsForField = onyxData.errorFields?.[fieldName] ?? {}; | ||
|
||
if (Object.keys(errorsForField).length === 0) { | ||
return {}; | ||
} | ||
|
||
const key = Object.keys(errorsForField).sort()[0]; | ||
|
||
return {[key]: errorsForField[key]}; | ||
} | ||
|
||
type ErrorsList = Record<string, string | [string, {isTranslated: boolean}]>; | ||
|
||
/** | ||
* Method used to generate error message for given inputID | ||
* @param errorList - An object containing current errors in the form | ||
* @param message - Message to assign to the inputID errors | ||
*/ | ||
function addErrorMessage(errors: ErrorsList, inputID?: string, message?: string) { | ||
if (!message || !inputID) { | ||
return; | ||
} | ||
|
||
const errorList = errors; | ||
const error = errorList[inputID]; | ||
const translatedMessage = Localize.translateIfPhraseKey(message); | ||
|
||
if (!error) { | ||
errorList[inputID] = [translatedMessage, {isTranslated: true}]; | ||
} else if (typeof error === 'string') { | ||
errorList[inputID] = [`${error}\n${translatedMessage}`, {isTranslated: true}]; | ||
} else if (Array.isArray(error)) { | ||
error[0] = `${error[0]}\n${translatedMessage}`; | ||
} | ||
} | ||
|
||
export {getAuthenticateErrorMessage, getMicroSecondOnyxError, getLatestErrorMessage, getLatestErrorField, getEarliestErrorField, addErrorMessage}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters