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

Commit

Permalink
Make slash command errors translatable but also work in rageshakes
Browse files Browse the repository at this point in the history
See #7372 (comment)

We want the error to be translated for the user but not in our rageshake logs
  • Loading branch information
MadLittleMods committed Dec 15, 2021
1 parent 5e5c1af commit 50ecd8c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
43 changes: 24 additions & 19 deletions src/SlashCommands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { logger } from "matrix-js-sdk/src/logger";

import { MatrixClientPeg } from './MatrixClientPeg';
import dis from './dispatcher/dispatcher';
import { _t, _td } from './languageHandler';
import { _t, _td, newTranslatableError } from './languageHandler';
import Modal from './Modal';
import MultiInviter from './utils/MultiInviter';
import { linkifyAndSanitizeHtml } from './HtmlUtils';
Expand Down Expand Up @@ -297,15 +297,10 @@ export const Commands = [
return success((async () => {
const unixTimestamp = Date.parse(args);
if (!unixTimestamp) {
throw new Error(
// FIXME: Use newTranslatableError here instead
// otherwise the rageshake error messages will be
// translated too
_t(
// eslint-disable-next-line max-len
'We were unable to understand the given date (%(inputDate)s). Try using the format YYYY-MM-DD.',
{ inputDate: args },
),
throw newTranslatableError(
'We were unable to understand the given date (%(inputDate)s). ' +
'Try using the format YYYY-MM-DD.',
{ inputDate: args },
);
}

Expand Down Expand Up @@ -509,10 +504,16 @@ export const Commands = [
useDefaultIdentityServer();
return;
}
throw new Error(_t("Use an identity server to invite by email. Manage in Settings."));
throw newTranslatableError(
"Use an identity server to invite by email. Manage in Settings.",
);
});
} else {
return reject(_t("Use an identity server to invite by email. Manage in Settings."));
return reject(
newTranslatableError(
"Use an identity server to invite by email. Manage in Settings.",
),
);
}
}
const inviter = new MultiInviter(roomId);
Expand Down Expand Up @@ -941,30 +942,34 @@ export const Commands = [
return success((async () => {
const device = cli.getStoredDevice(userId, deviceId);
if (!device) {
throw new Error(_t('Unknown (user, session) pair:') + ` (${userId}, ${deviceId})`);
throw newTranslatableError(
'Unknown (user, session) pair: (%(userId)s, %(deviceId)s)',
{ userId, deviceId },
);
}
const deviceTrust = await cli.checkDeviceTrust(userId, deviceId);

if (deviceTrust.isVerified()) {
if (device.getFingerprint() === fingerprint) {
throw new Error(_t('Session already verified!'));
throw newTranslatableError('Session already verified!');
} else {
throw new Error(_t('WARNING: Session already verified, but keys do NOT MATCH!'));
throw newTranslatableError('WARNING: Session already verified, but keys do NOT MATCH!');
}
}

if (device.getFingerprint() !== fingerprint) {
const fprint = device.getFingerprint();
throw new Error(
_t('WARNING: KEY VERIFICATION FAILED! The signing key for %(userId)s and session' +
throw newTranslatableError(
'WARNING: KEY VERIFICATION FAILED! The signing key for %(userId)s and session' +
' %(deviceId)s is "%(fprint)s" which does not match the provided key ' +
'"%(fingerprint)s". This could mean your communications are being intercepted!',
{
fprint,
userId,
deviceId,
fingerprint,
}));
},
);
}

await cli.setDeviceVerified(userId, deviceId, true);
Expand Down Expand Up @@ -1083,7 +1088,7 @@ export const Commands = [
if (isPhoneNumber) {
const results = await CallHandler.instance.pstnLookup(this.state.value);
if (!results || results.length === 0 || !results[0].userid) {
throw new Error("Unable to find Matrix ID for phone number");
throw newTranslatableError("Unable to find Matrix ID for phone number");
}
userId = results[0].userid;
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/views/rooms/SendMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
let errText;
if (typeof error === 'string') {
errText = error;
} else if (error.translatedMessage) {
// Check for translatable errors (newTranslatableError)
errText = error.translatedMessage;
} else if (error.message) {
errText = error.message;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/languageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ interface ITranslatableError extends Error {
* @param {string} message Message to translate.
* @returns {Error} The constructed error.
*/
export function newTranslatableError(message: string) {
export function newTranslatableError(message: string, variables?: IVariables): ITranslatableError {
const error = new Error(message) as ITranslatableError;
error.translatedMessage = _t(message);
error.translatedMessage = _t(message, variables);
return error;
}

Expand Down

0 comments on commit 50ecd8c

Please sign in to comment.