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

Commit 0a22ed9

Browse files
authored
Consolidate login errors (#10722)
1 parent 70326b9 commit 0a22ed9

File tree

7 files changed

+463
-146
lines changed

7 files changed

+463
-146
lines changed

src/Lifecycle.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import { Action } from "./dispatcher/actions";
6363
import AbstractLocalStorageSettingsHandler from "./settings/handlers/AbstractLocalStorageSettingsHandler";
6464
import { OverwriteLoginPayload } from "./dispatcher/payloads/OverwriteLoginPayload";
6565
import { SdkContextClass } from "./contexts/SDKContext";
66+
import { messageForLoginError } from "./utils/ErrorUtils";
6667

6768
const HOMESERVER_URL_KEY = "mx_hs_url";
6869
const ID_SERVER_URL_KEY = "mx_is_url";
@@ -230,17 +231,10 @@ export function attemptTokenLogin(
230231
.catch((err) => {
231232
Modal.createDialog(ErrorDialog, {
232233
title: _t("We couldn't log you in"),
233-
description:
234-
err.name === "ConnectionError"
235-
? _t(
236-
"Your homeserver was unreachable and was not able to log you in. Please try again. " +
237-
"If this continues, please contact your homeserver administrator.",
238-
)
239-
: _t(
240-
"Your homeserver rejected your log in attempt. " +
241-
"This could be due to things just taking too long. Please try again. " +
242-
"If this continues, please contact your homeserver administrator.",
243-
),
234+
description: messageForLoginError(err, {
235+
hsUrl: homeserver,
236+
hsName: homeserver,
237+
}),
244238
button: _t("Try again"),
245239
onFinished: (tryAgain) => {
246240
if (tryAgain) {

src/components/structures/auth/Login.tsx

+7-106
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ limitations under the License.
1515
*/
1616

1717
import React, { ReactNode } from "react";
18-
import { ConnectionError, MatrixError } from "matrix-js-sdk/src/http-api";
1918
import classNames from "classnames";
2019
import { logger } from "matrix-js-sdk/src/logger";
2120
import { ISSOFlow, LoginFlow, SSOAction } from "matrix-js-sdk/src/@types/auth";
2221

2322
import { _t, _td } from "../../../languageHandler";
2423
import Login from "../../../Login";
25-
import SdkConfig from "../../../SdkConfig";
26-
import { messageForResourceLimitError } from "../../../utils/ErrorUtils";
24+
import { messageForConnectionError, messageForLoginError } from "../../../utils/ErrorUtils";
2725
import AutoDiscoveryUtils from "../../../utils/AutoDiscoveryUtils";
2826
import AuthPage from "../../views/auth/AuthPage";
2927
import PlatformPeg from "../../../PlatformPeg";
@@ -212,56 +210,20 @@ export default class LoginComponent extends React.PureComponent<IProps, IState>
212210
this.props.onLoggedIn(data, password);
213211
},
214212
(error) => {
215-
if (this.unmounted) {
216-
return;
217-
}
218-
let errorText: ReactNode;
213+
if (this.unmounted) return;
219214

215+
let errorText: ReactNode;
220216
// Some error strings only apply for logging in
221-
const usingEmail = username && username.indexOf("@") > 0;
222-
if (error.httpStatus === 400 && usingEmail) {
217+
if (error.httpStatus === 400 && username && username.indexOf("@") > 0) {
223218
errorText = _t("This homeserver does not support login using email address.");
224-
} else if (error.errcode === "M_RESOURCE_LIMIT_EXCEEDED") {
225-
const errorTop = messageForResourceLimitError(error.data.limit_type, error.data.admin_contact, {
226-
"monthly_active_user": _td("This homeserver has hit its Monthly Active User limit."),
227-
"hs_blocked": _td("This homeserver has been blocked by its administrator."),
228-
"": _td("This homeserver has exceeded one of its resource limits."),
229-
});
230-
const errorDetail = messageForResourceLimitError(error.data.limit_type, error.data.admin_contact, {
231-
"": _td("Please <a>contact your service administrator</a> to continue using this service."),
232-
});
233-
errorText = (
234-
<div>
235-
<div>{errorTop}</div>
236-
<div className="mx_Login_smallError">{errorDetail}</div>
237-
</div>
238-
);
239-
} else if (error.httpStatus === 401 || error.httpStatus === 403) {
240-
if (error.errcode === "M_USER_DEACTIVATED") {
241-
errorText = _t("This account has been deactivated.");
242-
} else if (SdkConfig.get("disable_custom_urls")) {
243-
errorText = (
244-
<div>
245-
<div>{_t("Incorrect username and/or password.")}</div>
246-
<div className="mx_Login_smallError">
247-
{_t("Please note you are logging into the %(hs)s server, not matrix.org.", {
248-
hs: this.props.serverConfig.hsName,
249-
})}
250-
</div>
251-
</div>
252-
);
253-
} else {
254-
errorText = _t("Incorrect username and/or password.");
255-
}
256219
} else {
257-
// other errors, not specific to doing a password login
258-
errorText = this.errorTextFromError(error);
220+
errorText = messageForLoginError(error, this.props.serverConfig);
259221
}
260222

261223
this.setState({
262224
busy: false,
263225
busyLoggingIn: false,
264-
errorText: errorText,
226+
errorText,
265227
// 401 would be the sensible status code for 'incorrect password'
266228
// but the login API gives a 403 https://matrix.org/jira/browse/SYN-744
267229
// mentions this (although the bug is for UI auth which is not this)
@@ -425,7 +387,7 @@ export default class LoginComponent extends React.PureComponent<IProps, IState>
425387
},
426388
(err) => {
427389
this.setState({
428-
errorText: this.errorTextFromError(err),
390+
errorText: messageForConnectionError(err, this.props.serverConfig),
429391
loginIncorrect: false,
430392
canTryLogin: false,
431393
});
@@ -448,67 +410,6 @@ export default class LoginComponent extends React.PureComponent<IProps, IState>
448410
return true;
449411
};
450412

451-
private errorTextFromError(err: MatrixError): ReactNode {
452-
let errCode = err.errcode;
453-
if (!errCode && err.httpStatus) {
454-
errCode = "HTTP " + err.httpStatus;
455-
}
456-
457-
let errorText: ReactNode =
458-
_t("There was a problem communicating with the homeserver, please try again later.") +
459-
(errCode ? " (" + errCode + ")" : "");
460-
461-
if (err instanceof ConnectionError) {
462-
if (
463-
window.location.protocol === "https:" &&
464-
(this.props.serverConfig.hsUrl.startsWith("http:") || !this.props.serverConfig.hsUrl.startsWith("http"))
465-
) {
466-
errorText = (
467-
<span>
468-
{_t(
469-
"Can't connect to homeserver via HTTP when an HTTPS URL is in your browser bar. " +
470-
"Either use HTTPS or <a>enable unsafe scripts</a>.",
471-
{},
472-
{
473-
a: (sub) => {
474-
return (
475-
<a
476-
target="_blank"
477-
rel="noreferrer noopener"
478-
href="https://www.google.com/search?&q=enable%20unsafe%20scripts"
479-
>
480-
{sub}
481-
</a>
482-
);
483-
},
484-
},
485-
)}
486-
</span>
487-
);
488-
} else {
489-
errorText = (
490-
<span>
491-
{_t(
492-
"Can't connect to homeserver - please check your connectivity, ensure your " +
493-
"<a>homeserver's SSL certificate</a> is trusted, and that a browser extension " +
494-
"is not blocking requests.",
495-
{},
496-
{
497-
a: (sub) => (
498-
<a target="_blank" rel="noreferrer noopener" href={this.props.serverConfig.hsUrl}>
499-
{sub}
500-
</a>
501-
),
502-
},
503-
)}
504-
</span>
505-
);
506-
}
507-
}
508-
509-
return errorText;
510-
}
511-
512413
public renderLoginComponentForFlows(): ReactNode {
513414
if (!this.state.flows) return null;
514415

src/components/structures/auth/Registration.tsx

+8-10
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import classNames from "classnames";
2121
import { logger } from "matrix-js-sdk/src/logger";
2222
import { ISSOFlow, SSOAction } from "matrix-js-sdk/src/@types/auth";
2323

24-
import { _t, _td } from "../../../languageHandler";
25-
import { messageForResourceLimitError } from "../../../utils/ErrorUtils";
24+
import { _t } from "../../../languageHandler";
25+
import { adminContactStrings, messageForResourceLimitError, resourceLimitStrings } from "../../../utils/ErrorUtils";
2626
import AutoDiscoveryUtils from "../../../utils/AutoDiscoveryUtils";
2727
import * as Lifecycle from "../../../Lifecycle";
2828
import { IMatrixClientCreds, MatrixClientPeg } from "../../../MatrixClientPeg";
@@ -313,17 +313,15 @@ export default class Registration extends React.Component<IProps, IState> {
313313
let errorText: ReactNode = (response as Error).message || (response as Error).toString();
314314
// can we give a better error message?
315315
if (response instanceof MatrixError && response.errcode === "M_RESOURCE_LIMIT_EXCEEDED") {
316-
const errorTop = messageForResourceLimitError(response.data.limit_type, response.data.admin_contact, {
317-
"monthly_active_user": _td("This homeserver has hit its Monthly Active User limit."),
318-
"hs_blocked": _td("This homeserver has been blocked by its administrator."),
319-
"": _td("This homeserver has exceeded one of its resource limits."),
320-
});
316+
const errorTop = messageForResourceLimitError(
317+
response.data.limit_type,
318+
response.data.admin_contact,
319+
resourceLimitStrings,
320+
);
321321
const errorDetail = messageForResourceLimitError(
322322
response.data.limit_type,
323323
response.data.admin_contact,
324-
{
325-
"": _td("Please <a>contact your service administrator</a> to continue using this service."),
326-
},
324+
adminContactStrings,
327325
);
328326
errorText = (
329327
<div>

src/i18n/strings/en_EN.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@
103103
"We couldn't log you in": "We couldn't log you in",
104104
"We asked the browser to remember which homeserver you use to let you sign in, but unfortunately your browser has forgotten it. Go to the sign in page and try again.": "We asked the browser to remember which homeserver you use to let you sign in, but unfortunately your browser has forgotten it. Go to the sign in page and try again.",
105105
"Try again": "Try again",
106-
"Your homeserver was unreachable and was not able to log you in. Please try again. If this continues, please contact your homeserver administrator.": "Your homeserver was unreachable and was not able to log you in. Please try again. If this continues, please contact your homeserver administrator.",
107-
"Your homeserver rejected your log in attempt. This could be due to things just taking too long. Please try again. If this continues, please contact your homeserver administrator.": "Your homeserver rejected your log in attempt. This could be due to things just taking too long. Please try again. If this continues, please contact your homeserver administrator.",
108106
"Database unexpectedly closed": "Database unexpectedly closed",
109107
"This may be caused by having the app open in multiple tabs or due to clearing browser data.": "This may be caused by having the app open in multiple tabs or due to clearing browser data.",
110108
"Reload": "Reload",
@@ -703,8 +701,14 @@
703701
"This homeserver has hit its Monthly Active User limit.": "This homeserver has hit its Monthly Active User limit.",
704702
"This homeserver has been blocked by its administrator.": "This homeserver has been blocked by its administrator.",
705703
"This homeserver has exceeded one of its resource limits.": "This homeserver has exceeded one of its resource limits.",
706-
"Please <a>contact your service administrator</a> to continue using the service.": "Please <a>contact your service administrator</a> to continue using the service.",
704+
"Please <a>contact your service administrator</a> to continue using this service.": "Please <a>contact your service administrator</a> to continue using this service.",
707705
"Unable to connect to Homeserver. Retrying…": "Unable to connect to Homeserver. Retrying…",
706+
"This account has been deactivated.": "This account has been deactivated.",
707+
"Incorrect username and/or password.": "Incorrect username and/or password.",
708+
"Please note you are logging into the %(hs)s server, not matrix.org.": "Please note you are logging into the %(hs)s server, not matrix.org.",
709+
"There was a problem communicating with the homeserver, please try again later.": "There was a problem communicating with the homeserver, please try again later.",
710+
"Can't connect to homeserver via HTTP when an HTTPS URL is in your browser bar. Either use HTTPS or <a>enable unsafe scripts</a>.": "Can't connect to homeserver via HTTP when an HTTPS URL is in your browser bar. Either use HTTPS or <a>enable unsafe scripts</a>.",
711+
"Can't connect to homeserver - please check your connectivity, ensure your <a>homeserver's SSL certificate</a> is trusted, and that a browser extension is not blocking requests.": "Can't connect to homeserver - please check your connectivity, ensure your <a>homeserver's SSL certificate</a> is trusted, and that a browser extension is not blocking requests.",
708712
"%(items)s and %(count)s others|other": "%(items)s and %(count)s others",
709713
"%(items)s and %(count)s others|one": "%(items)s and one other",
710714
"%(items)s and %(lastItem)s": "%(items)s and %(lastItem)s",
@@ -3553,15 +3557,8 @@
35533557
"Identity server URL does not appear to be a valid identity server": "Identity server URL does not appear to be a valid identity server",
35543558
"General failure": "General failure",
35553559
"This homeserver does not support login using email address.": "This homeserver does not support login using email address.",
3556-
"Please <a>contact your service administrator</a> to continue using this service.": "Please <a>contact your service administrator</a> to continue using this service.",
3557-
"This account has been deactivated.": "This account has been deactivated.",
3558-
"Incorrect username and/or password.": "Incorrect username and/or password.",
3559-
"Please note you are logging into the %(hs)s server, not matrix.org.": "Please note you are logging into the %(hs)s server, not matrix.org.",
35603560
"Failed to perform homeserver discovery": "Failed to perform homeserver discovery",
35613561
"This homeserver doesn't offer any login flows which are supported by this client.": "This homeserver doesn't offer any login flows which are supported by this client.",
3562-
"There was a problem communicating with the homeserver, please try again later.": "There was a problem communicating with the homeserver, please try again later.",
3563-
"Can't connect to homeserver via HTTP when an HTTPS URL is in your browser bar. Either use HTTPS or <a>enable unsafe scripts</a>.": "Can't connect to homeserver via HTTP when an HTTPS URL is in your browser bar. Either use HTTPS or <a>enable unsafe scripts</a>.",
3564-
"Can't connect to homeserver - please check your connectivity, ensure your <a>homeserver's SSL certificate</a> is trusted, and that a browser extension is not blocking requests.": "Can't connect to homeserver - please check your connectivity, ensure your <a>homeserver's SSL certificate</a> is trusted, and that a browser extension is not blocking requests.",
35653562
"Syncing…": "Syncing…",
35663563
"Signing In…": "Signing In…",
35673564
"If you've joined lots of rooms, this might take a while": "If you've joined lots of rooms, this might take a while",

0 commit comments

Comments
 (0)