fix: create custom error for display name retrieval errors

pull/3281/head
yougotwill 4 months ago
parent f9ccc317b6
commit fd24a47b91

@ -34,6 +34,7 @@ import { ContinueButton, OnboardDescription, OnboardHeading } from '../component
import { BackButtonWithinContainer } from '../components/BackButton'; import { BackButtonWithinContainer } from '../components/BackButton';
import { displayNameIsValid, sanitizeDisplayNameOrToast } from '../utils'; import { displayNameIsValid, sanitizeDisplayNameOrToast } from '../utils';
import { localize } from '../../../util/i18n/localizedString'; import { localize } from '../../../util/i18n/localizedString';
import { RetrieveDisplayNameError } from '../../../session/utils/errors';
export type AccountDetails = { export type AccountDetails = {
recoveryPassword: string; recoveryPassword: string;
@ -108,15 +109,9 @@ export const CreateAccount = () => {
); );
dispatch(setAccountCreationStep(AccountCreation.DisplayName)); dispatch(setAccountCreationStep(AccountCreation.DisplayName));
switch (err.message) { if (err instanceof RetrieveDisplayNameError) {
case 'failed to retrieve display name after setting it':
dispatch(setDisplayNameError(localize('displayNameErrorDescription').toString())); dispatch(setDisplayNameError(localize('displayNameErrorDescription').toString()));
return; return;
case 'failed to get truncated displayName after setting it':
dispatch(setDisplayNameError(localize('displayNameErrorDescription').toString()));
return;
default:
// we can't guarantee that an error has a message so we handle the final case outside of the switch
} }
// Note: we have to assume here that libsession threw an error because the name was too long since we covered the other cases. // Note: we have to assume here that libsession threw an error because the name was too long since we covered the other cases.

@ -6,7 +6,7 @@ import { InvalidWordsError, NotEnoughWordsError } from '../../../session/crypto/
import { ProfileManager } from '../../../session/profile_manager/ProfileManager'; import { ProfileManager } from '../../../session/profile_manager/ProfileManager';
import { PromiseUtils } from '../../../session/utils'; import { PromiseUtils } from '../../../session/utils';
import { TaskTimedOutError } from '../../../session/utils/Promise'; import { TaskTimedOutError } from '../../../session/utils/Promise';
import { NotFoundError } from '../../../session/utils/errors'; import { NotFoundError, RetrieveDisplayNameError } from '../../../session/utils/errors';
import { trigger } from '../../../shims/events'; import { trigger } from '../../../shims/events';
import { import {
AccountRestoration, AccountRestoration,
@ -198,15 +198,9 @@ export const RestoreAccount = () => {
); );
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName)); dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
switch (err.message) { if (err instanceof RetrieveDisplayNameError) {
case 'failed to retrieve display name after setting it':
dispatch(setDisplayNameError(localize('displayNameErrorDescription').toString())); dispatch(setDisplayNameError(localize('displayNameErrorDescription').toString()));
return; return;
case 'failed to get truncated displayName after setting it':
dispatch(setDisplayNameError(localize('displayNameErrorDescription').toString()));
return;
default:
// we can't guarantee that an error has a message so we handle the final case outside of the switch
} }
// Note: we have to assume here that libsession threw an error because the name was too long since we covered the other cases. // Note: we have to assume here that libsession threw an error because the name was too long since we covered the other cases.

@ -6,6 +6,7 @@ import { SyncUtils, UserUtils } from '../utils';
import { fromHexToArray, sanitizeSessionUsername, toHex } from '../utils/String'; import { fromHexToArray, sanitizeSessionUsername, toHex } from '../utils/String';
import { AvatarDownload } from '../utils/job_runners/jobs/AvatarDownloadJob'; import { AvatarDownload } from '../utils/job_runners/jobs/AvatarDownloadJob';
import { CONVERSATION_PRIORITIES, ConversationTypeEnum } from '../../models/types'; import { CONVERSATION_PRIORITIES, ConversationTypeEnum } from '../../models/types';
import { RetrieveDisplayNameError } from '../utils/errors';
export type Profile = { export type Profile = {
displayName: string | undefined; displayName: string | undefined;
@ -115,7 +116,7 @@ async function updateOurProfileDisplayNameOnboarding(newName: string) {
const appliedName = await UserConfigWrapperActions.getName(); const appliedName = await UserConfigWrapperActions.getName();
if (isNil(appliedName)) { if (isNil(appliedName)) {
throw new Error('failed to retrieve display name after setting it'); throw new RetrieveDisplayNameError();
} }
return appliedName; return appliedName;
@ -141,7 +142,7 @@ async function updateOurProfileDisplayName(newName: string) {
await UserConfigWrapperActions.setNameTruncated(sanitizeSessionUsername(newName).trim()); await UserConfigWrapperActions.setNameTruncated(sanitizeSessionUsername(newName).trim());
const truncatedName = await UserConfigWrapperActions.getName(); const truncatedName = await UserConfigWrapperActions.getName();
if (isNil(truncatedName)) { if (isNil(truncatedName)) {
throw new Error('failed to get truncated displayName after setting it'); throw new RetrieveDisplayNameError();
} }
await UserConfigWrapperActions.setPriority(dbPriority); await UserConfigWrapperActions.setPriority(dbPriority);
if (dbProfileUrl && !isEmpty(dbProfileKey)) { if (dbProfileUrl && !isEmpty(dbProfileKey)) {

@ -74,3 +74,11 @@ export class SnodeResponseError extends Error {
Object.setPrototypeOf(this, SnodeResponseError.prototype); Object.setPrototypeOf(this, SnodeResponseError.prototype);
} }
} }
export class RetrieveDisplayNameError extends Error {
constructor(message = 'failed to retrieve display name after setting it') {
super(message);
// restore prototype chain
Object.setPrototypeOf(this, SnodeResponseError.prototype);
}
}

Loading…
Cancel
Save