Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Properly translate errors in AddThreepid.ts so they show up translated to the user but not in our logs #10432

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
72c08a1
Properly translate errors in AddThreepid.ts
MadLittleMods Mar 22, 2023
777a4fc
Use translated message
MadLittleMods Mar 22, 2023
167150d
Avoid returning undefined ever
MadLittleMods Mar 22, 2023
4492ecc
More usage
MadLittleMods Mar 22, 2023
8c24afb
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Mar 23, 2023
e57a11c
Introduce UserFriendlyError
MadLittleMods Mar 24, 2023
ebbb455
Use UserFriendlyError
MadLittleMods Mar 24, 2023
b3d37fb
Add more usage instead of normal error
MadLittleMods Mar 24, 2023
cf0ba20
Use types and translatedMessage
MadLittleMods Mar 24, 2023
e557019
Fix lints
MadLittleMods Mar 24, 2023
e6f053a
Update i18n although it's wrong
MadLittleMods Mar 24, 2023
8152c6b
Use unknown for easier creation from try/catch
MadLittleMods Mar 24, 2023
d88d5f2
Use types
MadLittleMods Mar 24, 2023
6e21c16
Use error types
MadLittleMods Mar 24, 2023
078e24b
Use types
MadLittleMods Mar 24, 2023
924416c
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Mar 31, 2023
1cd8ec0
Update i18n strings
MadLittleMods Mar 31, 2023
306a1dc
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 3, 2023
ac02179
Remove generic re-label of HTTPError
MadLittleMods Apr 3, 2023
8675464
Make error message extraction generic
MadLittleMods Apr 3, 2023
529212c
Update i18n strings
MadLittleMods Apr 3, 2023
edac565
Add tests for email addresses
MadLittleMods Apr 4, 2023
477db79
More consistent error logging to actually see error in logs
MadLittleMods Apr 4, 2023
629f429
Consistent error handling
MadLittleMods Apr 4, 2023
c2080ce
Any is okay because we have a fallback
MadLittleMods Apr 4, 2023
8ffeb6f
Check error type
MadLittleMods Apr 4, 2023
1b54ba6
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 5, 2023
96ba896
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 5, 2023
f8164d3
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 6, 2023
016341a
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 7, 2023
5d74ef9
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 10, 2023
ac67f1f
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 11, 2023
251df4d
Use dedicated mockResolvedValue function
MadLittleMods Apr 11, 2023
ae8bdaf
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 12, 2023
817190c
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 13, 2023
ed00c59
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods Apr 14, 2023
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
158 changes: 91 additions & 67 deletions src/AddThreepid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { IAuthData, IRequestMsisdnTokenResponse, IRequestTokenResponse } from "m

import { MatrixClientPeg } from "./MatrixClientPeg";
import Modal from "./Modal";
import { _t } from "./languageHandler";
import { _t, newTranslatableError } from "./languageHandler";
import IdentityAuthClient from "./IdentityAuthClient";
import { SSOAuthEntry } from "./components/views/auth/InteractiveAuthEntryComponents";
import InteractiveAuthDialog from "./components/views/dialogs/InteractiveAuthDialog";
Expand Down Expand Up @@ -67,23 +67,23 @@ export default class AddThreepid {
* @param {string} emailAddress The email address to add
* @return {Promise} Resolves when the email has been sent. Then call checkEmailLinkClicked().
*/
public addEmailAddress(emailAddress: string): Promise<IRequestTokenResponse> {
return MatrixClientPeg.get()
.requestAdd3pidEmailToken(emailAddress, this.clientSecret, 1)
.then(
(res) => {
this.sessionId = res.sid;
return res;
},
function (err) {
if (err.errcode === "M_THREEPID_IN_USE") {
err.message = _t("This email address is already in use");
} else if (err.httpStatus) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit of logging seems to get lost, is that intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, see #10432 (comment)

I don't think we need to re-label a HTTPError as they already have a good message that looks like Server returned ${response.status} error: ${body} and the main part of the error isn't translated regardless (only the word "Status" 😄)

Probably best to just remove in favor of thinking about a translations in general from the matrix-js-sdk, see matrix-org/matrix-js-sdk#1309

err.message = err.message + ` (Status ${err.httpStatus})`;
}
throw err;
},
);
public async addEmailAddress(emailAddress: string): Promise<IRequestTokenResponse> {
Copy link
Contributor Author

@MadLittleMods MadLittleMods Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are just getting some async/await treatment with some UserFriendlyError

try {
const res = await MatrixClientPeg.get().requestAdd3pidEmailToken(emailAddress, this.clientSecret, 1);
this.sessionId = res.sid;
return res;
} catch (err) {
if (err.errcode === "M_THREEPID_IN_USE") {
throw newTranslatableError("This email address is already in use");
} else if (err.httpStatus) {
throw newTranslatableError("%(errorMessage)s (Status %(httpStatus)s)", {
errorMessage: err.message,
httpStatus: err.httpStatus,
});
}
// Otherwise, just blurt out the same error
throw err;
}
}

/**
Expand All @@ -98,22 +98,28 @@ export default class AddThreepid {
// For separate bind, request a token directly from the IS.
const authClient = new IdentityAuthClient();
const identityAccessToken = (await authClient.getAccessToken()) ?? undefined;
return MatrixClientPeg.get()
.requestEmailToken(emailAddress, this.clientSecret, 1, undefined, identityAccessToken)
.then(
(res) => {
this.sessionId = res.sid;
return res;
},
function (err) {
if (err.errcode === "M_THREEPID_IN_USE") {
err.message = _t("This email address is already in use");
} else if (err.httpStatus) {
err.message = err.message + ` (Status ${err.httpStatus})`;
}
throw err;
},
try {
const res = await MatrixClientPeg.get().requestEmailToken(
emailAddress,
this.clientSecret,
1,
undefined,
identityAccessToken,
);
this.sessionId = res.sid;
return res;
} catch (err) {
if (err.errcode === "M_THREEPID_IN_USE") {
throw newTranslatableError("This email address is already in use");
} else if (err.httpStatus) {
throw newTranslatableError("%(errorMessage)s (Status %(httpStatus)s)", {
errorMessage: err.message,
httpStatus: err.httpStatus,
});
}
// Otherwise, just blurt out the same error
throw err;
}
} else {
// For tangled bind, request a token via the HS.
return this.addEmailAddress(emailAddress);
Expand All @@ -127,24 +133,29 @@ export default class AddThreepid {
* @param {string} phoneNumber The national or international formatted phone number to add
* @return {Promise} Resolves when the text message has been sent. Then call haveMsisdnToken().
*/
public addMsisdn(phoneCountry: string, phoneNumber: string): Promise<IRequestMsisdnTokenResponse> {
return MatrixClientPeg.get()
.requestAdd3pidMsisdnToken(phoneCountry, phoneNumber, this.clientSecret, 1)
.then(
(res) => {
this.sessionId = res.sid;
this.submitUrl = res.submit_url;
return res;
},
function (err) {
if (err.errcode === "M_THREEPID_IN_USE") {
err.message = _t("This phone number is already in use");
} else if (err.httpStatus) {
err.message = err.message + ` (Status ${err.httpStatus})`;
}
throw err;
},
public async addMsisdn(phoneCountry: string, phoneNumber: string): Promise<IRequestMsisdnTokenResponse> {
try {
const res = await MatrixClientPeg.get().requestAdd3pidMsisdnToken(
phoneCountry,
phoneNumber,
this.clientSecret,
1,
);
this.sessionId = res.sid;
this.submitUrl = res.submit_url;
return res;
} catch (err) {
if (err.errcode === "M_THREEPID_IN_USE") {
throw newTranslatableError("This phone number is already in use");
} else if (err.httpStatus) {
throw newTranslatableError("%(errorMessage)s (Status %(httpStatus)s)", {
errorMessage: err.message,
httpStatus: err.httpStatus,
});
}
// Otherwise, just blurt out the same error
throw err;
}
}

/**
Expand All @@ -160,22 +171,29 @@ export default class AddThreepid {
// For separate bind, request a token directly from the IS.
const authClient = new IdentityAuthClient();
const identityAccessToken = (await authClient.getAccessToken()) ?? undefined;
return MatrixClientPeg.get()
.requestMsisdnToken(phoneCountry, phoneNumber, this.clientSecret, 1, undefined, identityAccessToken)
.then(
(res) => {
this.sessionId = res.sid;
return res;
},
function (err) {
if (err.errcode === "M_THREEPID_IN_USE") {
err.message = _t("This phone number is already in use");
} else if (err.httpStatus) {
err.message = err.message + ` (Status ${err.httpStatus})`;
}
throw err;
},
try {
const res = await MatrixClientPeg.get().requestMsisdnToken(
phoneCountry,
phoneNumber,
this.clientSecret,
1,
undefined,
identityAccessToken,
);
this.sessionId = res.sid;
return res;
} catch (err) {
if (err.errcode === "M_THREEPID_IN_USE") {
throw newTranslatableError("This phone number is already in use");
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
} else if (err.httpStatus) {
throw newTranslatableError("%(errorMessage)s (Status %(httpStatus)s)", {
errorMessage: err.message,
httpStatus: err.httpStatus,
});
}
// Otherwise, just blurt out the same error
throw err;
}
} else {
// For tangled bind, request a token via the HS.
return this.addMsisdn(phoneCountry, phoneNumber);
Expand Down Expand Up @@ -257,10 +275,16 @@ export default class AddThreepid {
}
} catch (err) {
if (err.httpStatus === 401) {
err.message = _t("Failed to verify email address: make sure you clicked the link in the email");
throw newTranslatableError(
"Failed to verify email address: make sure you clicked the link in the email",
);
} else if (err.httpStatus) {
err.message += ` (Status ${err.httpStatus})`;
throw newTranslatableError("%(errorMessage)s (Status %(httpStatus)s)", {
errorMessage: err.message,
httpStatus: err.httpStatus,
});
}
// Otherwise, just blurt out the same error
throw err;
}
return [];
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/dialogs/SetEmailDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class SetEmailDialog extends React.Component<IProps, IState> {
logger.error("Unable to add email address " + emailAddress + " " + err);
Modal.createDialog(ErrorDialog, {
title: _t("Unable to add email address"),
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
},
);
Expand Down Expand Up @@ -131,7 +131,7 @@ export default class SetEmailDialog extends React.Component<IProps, IState> {
logger.error("Unable to verify email address: " + err);
Modal.createDialog(ErrorDialog, {
title: _t("Unable to verify email address."),
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/settings/account/EmailAddresses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export default class EmailAddresses extends React.Component<IProps, IState> {
this.setState({ verifying: false, continueDisabled: false, addTask: null });
Modal.createDialog(ErrorDialog, {
title: _t("Unable to add email address"),
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
});
};
Expand Down Expand Up @@ -230,7 +230,7 @@ export default class EmailAddresses extends React.Component<IProps, IState> {
logger.error("Unable to verify email address: ", err);
Modal.createDialog(ErrorDialog, {
title: _t("Unable to verify email address."),
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/settings/account/PhoneNumbers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default class PhoneNumbers extends React.Component<IProps, IState> {
this.setState({ verifying: false, continueDisabled: false, addTask: null });
Modal.createDialog(ErrorDialog, {
title: _t("Error"),
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
});
};
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/settings/discovery/EmailAddresses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class EmailAddress extends React.Component<IEmailAddressProps, IEmailAddr
});
Modal.createDialog(ErrorDialog, {
title: errorTitle,
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ export class EmailAddress extends React.Component<IEmailAddressProps, IEmailAddr
});
Modal.createDialog(ErrorDialog, {
title: errorTitle,
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ export class EmailAddress extends React.Component<IEmailAddressProps, IEmailAddr
logger.error("Unable to verify email address: " + err);
Modal.createDialog(ErrorDialog, {
title: _t("Unable to verify email address."),
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/settings/discovery/PhoneNumbers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class PhoneNumber extends React.Component<IPhoneNumberProps, IPhoneNumber
});
Modal.createDialog(ErrorDialog, {
title: errorTitle,
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ export class PhoneNumber extends React.Component<IPhoneNumberProps, IPhoneNumber
});
Modal.createDialog(ErrorDialog, {
title: errorTitle,
description: err && err.message ? err.message : _t("Operation failed"),
description: err?.translatedMessage || err?.message || _t("Operation failed"),
});
}
}
Expand Down