This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 833
Properly translate errors in AddThreepid.ts
so they show up translated to the user but not in our logs
#10432
Merged
andybalaam
merged 36 commits into
develop
from
madlittlemods/9597-properly-translated-errors-in-AddThreepid
Apr 14, 2023
Merged
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 777a4fc
Use translated message
MadLittleMods 167150d
Avoid returning undefined ever
MadLittleMods 4492ecc
More usage
MadLittleMods 8c24afb
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods e57a11c
Introduce UserFriendlyError
MadLittleMods ebbb455
Use UserFriendlyError
MadLittleMods b3d37fb
Add more usage instead of normal error
MadLittleMods cf0ba20
Use types and translatedMessage
MadLittleMods e557019
Fix lints
MadLittleMods e6f053a
Update i18n although it's wrong
MadLittleMods 8152c6b
Use unknown for easier creation from try/catch
MadLittleMods d88d5f2
Use types
MadLittleMods 6e21c16
Use error types
MadLittleMods 078e24b
Use types
MadLittleMods 924416c
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods 1cd8ec0
Update i18n strings
MadLittleMods 306a1dc
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods ac02179
Remove generic re-label of HTTPError
MadLittleMods 8675464
Make error message extraction generic
MadLittleMods 529212c
Update i18n strings
MadLittleMods edac565
Add tests for email addresses
MadLittleMods 477db79
More consistent error logging to actually see error in logs
MadLittleMods 629f429
Consistent error handling
MadLittleMods c2080ce
Any is okay because we have a fallback
MadLittleMods 8ffeb6f
Check error type
MadLittleMods 1b54ba6
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods 96ba896
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods f8164d3
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods 016341a
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods 5d74ef9
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods ac67f1f
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods 251df4d
Use dedicated mockResolvedValue function
MadLittleMods ae8bdaf
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods 817190c
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods ed00c59
Merge branch 'develop' into madlittlemods/9597-properly-translated-er…
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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"; | ||
|
@@ -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) { | ||
err.message = err.message + ` (Status ${err.httpStatus})`; | ||
} | ||
throw err; | ||
}, | ||
); | ||
public async addEmailAddress(emailAddress: string): Promise<IRequestTokenResponse> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are just getting some |
||
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; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
|
@@ -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 []; | ||
|
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
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, see #10432 (comment)