Skip to content

Commit

Permalink
Merge pull request #27659 from software-mansion-labs/ts-migration/Err…
Browse files Browse the repository at this point in the history
…orUtils

[No QA][TS migration] Migrate 'ErrorUtils.js' lib to TypeScript
  • Loading branch information
robertjchen authored Sep 27, 2023
2 parents 340a909 + 982672c commit 6263b7b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 136 deletions.
135 changes: 0 additions & 135 deletions src/libs/ErrorUtils.js

This file was deleted.

114 changes: 114 additions & 0 deletions src/libs/ErrorUtils.ts
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};
3 changes: 2 additions & 1 deletion src/types/onyx/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {OnyxUpdate} from 'react-native-onyx';
type Response = {
previousUpdateID?: number | string;
lastUpdateID?: number | string;
jsonCode?: number;
jsonCode?: number | string;
onyxData?: OnyxUpdate[];
requestID?: string;
message?: string;
};

export default Response;

0 comments on commit 6263b7b

Please sign in to comment.