Consolidate login errors (#10722)

This commit is contained in:
Michael Telatynski 2023-04-27 09:05:31 +01:00 committed by GitHub
parent 70326b98f2
commit 0a22ed90ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 463 additions and 146 deletions

View file

@ -15,9 +15,21 @@ limitations under the License.
*/
import React, { ReactNode } from "react";
import { MatrixError } from "matrix-js-sdk/src/http-api";
import { MatrixError, ConnectionError } from "matrix-js-sdk/src/http-api";
import { _t, _td, Tags, TranslatedString } from "../languageHandler";
import SdkConfig from "../SdkConfig";
import { ValidatedServerConfig } from "./ValidatedServerConfig";
export const resourceLimitStrings = {
"monthly_active_user": _td("This homeserver has hit its Monthly Active User limit."),
"hs_blocked": _td("This homeserver has been blocked by its administrator."),
"": _td("This homeserver has exceeded one of its resource limits."),
};
export const adminContactStrings = {
"": _td("Please <a>contact your service administrator</a> to continue using this service."),
};
/**
* Produce a translated error message for a
@ -63,14 +75,16 @@ export function messageForResourceLimitError(
export function messageForSyncError(err: Error): ReactNode {
if (err instanceof MatrixError && err.errcode === "M_RESOURCE_LIMIT_EXCEEDED") {
const limitError = messageForResourceLimitError(err.data.limit_type, err.data.admin_contact, {
"monthly_active_user": _td("This homeserver has hit its Monthly Active User limit."),
"hs_blocked": _td("This homeserver has been blocked by its administrator."),
"": _td("This homeserver has exceeded one of its resource limits."),
});
const adminContact = messageForResourceLimitError(err.data.limit_type, err.data.admin_contact, {
"": _td("Please <a>contact your service administrator</a> to continue using the service."),
});
const limitError = messageForResourceLimitError(
err.data.limit_type,
err.data.admin_contact,
resourceLimitStrings,
);
const adminContact = messageForResourceLimitError(
err.data.limit_type,
err.data.admin_contact,
adminContactStrings,
);
return (
<div>
<div>{limitError}</div>
@ -81,3 +95,109 @@ export function messageForSyncError(err: Error): ReactNode {
return <div>{_t("Unable to connect to Homeserver. Retrying…")}</div>;
}
}
export function messageForLoginError(
err: MatrixError,
serverConfig: Pick<ValidatedServerConfig, "hsName" | "hsUrl">,
): ReactNode {
if (err.errcode === "M_RESOURCE_LIMIT_EXCEEDED") {
const errorTop = messageForResourceLimitError(
err.data.limit_type,
err.data.admin_contact,
resourceLimitStrings,
);
const errorDetail = messageForResourceLimitError(
err.data.limit_type,
err.data.admin_contact,
adminContactStrings,
);
return (
<div>
<div>{errorTop}</div>
<div className="mx_Login_smallError">{errorDetail}</div>
</div>
);
} else if (err.httpStatus === 401 || err.httpStatus === 403) {
if (err.errcode === "M_USER_DEACTIVATED") {
return _t("This account has been deactivated.");
} else if (SdkConfig.get("disable_custom_urls")) {
return (
<div>
<div>{_t("Incorrect username and/or password.")}</div>
<div className="mx_Login_smallError">
{_t("Please note you are logging into the %(hs)s server, not matrix.org.", {
hs: serverConfig.hsName,
})}
</div>
</div>
);
} else {
return _t("Incorrect username and/or password.");
}
} else {
return messageForConnectionError(err, serverConfig);
}
}
export function messageForConnectionError(
err: Error,
serverConfig: Pick<ValidatedServerConfig, "hsName" | "hsUrl">,
): ReactNode {
let errorText = _t("There was a problem communicating with the homeserver, please try again later.");
if (err instanceof ConnectionError) {
if (
window.location.protocol === "https:" &&
(serverConfig.hsUrl.startsWith("http:") || !serverConfig.hsUrl.startsWith("http"))
) {
return (
<span>
{_t(
"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>.",
{},
{
a: (sub) => {
return (
<a
target="_blank"
rel="noreferrer noopener"
href="https://www.google.com/search?&q=enable%20unsafe%20scripts"
>
{sub}
</a>
);
},
},
)}
</span>
);
}
return (
<span>
{_t(
"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.",
{},
{
a: (sub) => (
<a target="_blank" rel="noreferrer noopener" href={serverConfig.hsUrl}>
{sub}
</a>
),
},
)}
</span>
);
} else if (err instanceof MatrixError) {
if (err.errcode) {
errorText += `(${err.errcode})`;
} else if (err.httpStatus) {
errorText += ` (HTTP ${err.httpStatus})`;
}
}
return errorText;
}