Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA][TS migration] Migrate 'ErrorUtils.js' lib to TypeScript #27659

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 0 additions & 135 deletions src/libs/ErrorUtils.js

This file was deleted.

113 changes: 113 additions & 0 deletions src/libs/ErrorUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import CONST from '../CONST';
import DateUtils from './DateUtils';
import * as Localize from './Localize';
import Response from '../types/onyx/Response';
import {ErrorFields} from '../types/onyx/OnyxCommon';

function getAuthenticateErrorMessage(response: Response): string {
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
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: Record<string, string>;
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
};

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;
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
};

function getLatestErrorField<TOnyxData extends OnyxDataWithErrorFields>(onyxData: TOnyxData, fieldName: string): Record<string, string> {
const errorsForField = onyxData.errorFields[fieldName] ?? {};
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved

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] ?? {};
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved

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;
Loading