From ff4cbc018f1d62ff024f3fadcc6ff46c1e6b6939 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 1 Jul 2021 15:11:27 -0600 Subject: [PATCH] Update matrix-bot-sdk and use request cleaning function --- package.json | 2 +- src/Mjolnir.ts | 31 ++++++++++++------- .../AddRemoveProtectedRoomsCommand.ts | 6 ++-- src/commands/CommandHandler.ts | 6 ++-- src/commands/ProtectionsCommands.ts | 6 ++-- src/commands/SetPowerLevelCommand.ts | 6 ++-- src/commands/UnbanBanCommand.ts | 6 ++-- src/models/BanList.ts | 6 ++-- src/queues/AutomaticRedactionQueue.ts | 6 ++-- src/utils.ts | 5 +-- yarn.lock | 8 ++--- 11 files changed, 49 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index a06f8263..79bb2e78 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,6 @@ "config": "^3.3.6", "escape-html": "^1.0.3", "js-yaml": "^4.1.0", - "matrix-bot-sdk": "^0.5.18" + "matrix-bot-sdk": "^0.5.19" } } diff --git a/src/Mjolnir.ts b/src/Mjolnir.ts index 71ea53d7..5f0766bc 100644 --- a/src/Mjolnir.ts +++ b/src/Mjolnir.ts @@ -1,5 +1,5 @@ /* -Copyright 2019, 2020 The Matrix.org Foundation C.I.C. +Copyright 2019-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,7 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { CreateEvent, LogLevel, LogService, MatrixClient, MatrixGlob, Permalinks, UserID } from "matrix-bot-sdk"; +import { + CreateEvent, + extractRequestError, + LogLevel, + LogService, + MatrixClient, + MatrixGlob, + Permalinks, + UserID +} from "matrix-bot-sdk"; import BanList, { ALL_RULE_TYPES } from "./models/BanList"; import { applyServerAcls } from "./actions/ApplyAcl"; import { RoomUpdateError } from "./models/RoomUpdateError"; @@ -152,7 +161,7 @@ export class Mjolnir { } } } catch (e) { - LogService.warn("Mjolnir", e); + LogService.warn("Mjolnir", extractRequestError(e)); } await this.buildWatchedBanLists(); this.applyUnprotectedRooms(); @@ -175,7 +184,7 @@ export class Mjolnir { }).catch(async err => { try { LogService.error("Mjolnir", "Error during startup:"); - LogService.error("Mjolnir", err); + LogService.error("Mjolnir", extractRequestError(err)); await logMessage(LogLevel.ERROR, "Mjolnir@startup", "Startup failed due to error - see console"); } catch (e) { // If we failed to handle the error, just crash @@ -196,7 +205,7 @@ export class Mjolnir { try { additionalProtectedRooms = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE); } catch (e) { - LogService.warn("Mjolnir", e); + LogService.warn("Mjolnir", extractRequestError(e)); } if (!additionalProtectedRooms || !additionalProtectedRooms['rooms']) additionalProtectedRooms = {rooms: []}; additionalProtectedRooms.rooms.push(roomId); @@ -214,7 +223,7 @@ export class Mjolnir { try { additionalProtectedRooms = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE); } catch (e) { - LogService.warn("Mjolnir", e); + LogService.warn("Mjolnir", extractRequestError(e)); } if (!additionalProtectedRooms || !additionalProtectedRooms['rooms']) additionalProtectedRooms = {rooms: []}; additionalProtectedRooms.rooms = additionalProtectedRooms.rooms.filter(r => r !== roomId); @@ -250,7 +259,7 @@ export class Mjolnir { } } } catch (e) { - LogService.warn("Mjolnir", e); + LogService.warn("Mjolnir", extractRequestError(e)); } return enabled; @@ -261,7 +270,7 @@ export class Mjolnir { try { this.enableProtection(protection, false); } catch (e) { - LogService.warn("Mjolnir", e); + LogService.warn("Mjolnir", extractRequestError(e)); } } } @@ -464,7 +473,7 @@ export class Mjolnir { // Otherwise OK } catch (e) { - LogService.error("Mjolnir", e); + LogService.error("Mjolnir", extractRequestError(e)); errors.push({ roomId, errorMessage: e.message || (e.body ? e.body.error : ''), @@ -559,7 +568,7 @@ export class Mjolnir { const eventPermalink = Permalinks.forEvent(roomId, event['event_id']); LogService.error("Mjolnir", "Error handling protection: " + protection.name); LogService.error("Mjolnir", "Failed event: " + eventPermalink); - LogService.error("Mjolnir", e); + LogService.error("Mjolnir", extractRequestError(e)); await this.client.sendNotice(config.managementRoom, "There was an error processing an event through a protection - see log for details. Event: " + eventPermalink); } } @@ -631,7 +640,7 @@ export class Mjolnir { return response['admin']; } catch (e) { LogService.error("Mjolnir", "Error determining if Mjolnir is a server admin:"); - LogService.error("Mjolnir", e); + LogService.error("Mjolnir", extractRequestError(e)); return false; // assume not } } diff --git a/src/commands/AddRemoveProtectedRoomsCommand.ts b/src/commands/AddRemoveProtectedRoomsCommand.ts index 51aa42ee..d569e842 100644 --- a/src/commands/AddRemoveProtectedRoomsCommand.ts +++ b/src/commands/AddRemoveProtectedRoomsCommand.ts @@ -1,5 +1,5 @@ /* -Copyright 2020 The Matrix.org Foundation C.I.C. +Copyright 2020-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ limitations under the License. */ import { Mjolnir } from "../Mjolnir"; -import { LogLevel, LogService } from "matrix-bot-sdk"; +import { extractRequestError, LogLevel, LogService } from "matrix-bot-sdk"; import { logMessage } from "../LogProxy"; // !mjolnir rooms add @@ -32,7 +32,7 @@ export async function execRemoveProtectedRoom(roomId: string, event: any, mjolni try { await mjolnir.client.leaveRoom(protectedRoomId); } catch (e) { - LogService.warn("AddRemoveProtectedRoomsCommand", e); + LogService.warn("AddRemoveProtectedRoomsCommand", extractRequestError(e)); await logMessage(LogLevel.WARN, "AddRemoveProtectedRoomsCommand", `Failed to leave ${protectedRoomId} - the room is no longer being protected, but the bot could not leave`, protectedRoomId); } await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index 22edf38d..8a1c7f9b 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -1,5 +1,5 @@ /* -Copyright 2019, 2020 The Matrix.org Foundation C.I.C. +Copyright 2019-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ import { Mjolnir } from "../Mjolnir"; import { execStatusCommand } from "./StatusCommand"; import { execBanCommand, execUnbanCommand } from "./UnbanBanCommand"; import { execDumpRulesCommand } from "./DumpRulesCommand"; -import { LogService, RichReply } from "matrix-bot-sdk"; +import { extractRequestError, LogService, RichReply } from "matrix-bot-sdk"; import * as htmlEscape from "escape-html"; import { execSyncCommand } from "./SyncCommand"; import { execPermissionCheckCommand } from "./PermissionCheckCommand"; @@ -141,7 +141,7 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir return await mjolnir.client.sendMessage(roomId, reply); } } catch (e) { - LogService.error("CommandHandler", e); + LogService.error("CommandHandler", extractRequestError(e)); const text = "There was an error processing your command - see console/log for details"; const reply = RichReply.createFor(roomId, event, text, text); reply["msgtype"] = "m.notice"; diff --git a/src/commands/ProtectionsCommands.ts b/src/commands/ProtectionsCommands.ts index 1f71324a..15f2f583 100644 --- a/src/commands/ProtectionsCommands.ts +++ b/src/commands/ProtectionsCommands.ts @@ -1,5 +1,5 @@ /* -Copyright 2019 The Matrix.org Foundation C.I.C. +Copyright 2019-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ limitations under the License. */ import { Mjolnir } from "../Mjolnir"; -import { LogService, RichReply } from "matrix-bot-sdk"; +import { extractRequestError, LogService, RichReply } from "matrix-bot-sdk"; import { PROTECTIONS } from "../protections/protections"; // !mjolnir enable @@ -24,7 +24,7 @@ export async function execEnableProtection(roomId: string, event: any, mjolnir: await mjolnir.enableProtection(parts[2]); await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); } catch (e) { - LogService.error("ProtectionsCommands", e); + LogService.error("ProtectionsCommands", extractRequestError(e)); const message = `Error enabling protection '${parts[0]}' - check the name and try again.`; const reply = RichReply.createFor(roomId, event, message, message); diff --git a/src/commands/SetPowerLevelCommand.ts b/src/commands/SetPowerLevelCommand.ts index 0d984184..074ec0d4 100644 --- a/src/commands/SetPowerLevelCommand.ts +++ b/src/commands/SetPowerLevelCommand.ts @@ -1,5 +1,5 @@ /* -Copyright 2020 The Matrix.org Foundation C.I.C. +Copyright 2020-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ limitations under the License. */ import { Mjolnir } from "../Mjolnir"; -import { LogLevel, LogService } from "matrix-bot-sdk"; +import { extractRequestError, LogLevel, LogService } from "matrix-bot-sdk"; import { logMessage } from "../LogProxy"; // !mjolnir powerlevel [room] @@ -32,7 +32,7 @@ export async function execSetPowerLevelCommand(roomId: string, event: any, mjoln } catch (e) { const message = e.message || (e.body ? e.body.error : ''); await logMessage(LogLevel.ERROR, "SetPowerLevelCommand", `Failed to set power level of ${victim} to ${level} in ${targetRoomId}: ${message}`, targetRoomId); - LogService.error("SetPowerLevelCommand", e); + LogService.error("SetPowerLevelCommand", extractRequestError(e)); } } diff --git a/src/commands/UnbanBanCommand.ts b/src/commands/UnbanBanCommand.ts index d28145d5..3caa40bc 100644 --- a/src/commands/UnbanBanCommand.ts +++ b/src/commands/UnbanBanCommand.ts @@ -1,5 +1,5 @@ /* -Copyright 2019, 2020 The Matrix.org Foundation C.I.C. +Copyright 2019-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ limitations under the License. import { Mjolnir } from "../Mjolnir"; import BanList, { RULE_ROOM, RULE_SERVER, RULE_USER, USER_RULE_TYPES } from "../models/BanList"; -import { LogLevel, LogService, MatrixGlob, RichReply } from "matrix-bot-sdk"; +import { extractRequestError, LogLevel, LogService, MatrixGlob, RichReply } from "matrix-bot-sdk"; import { RECOMMENDATION_BAN, recommendationToStable } from "../models/ListRule"; import config from "../config"; import { logMessage } from "../LogProxy"; @@ -37,7 +37,7 @@ export async function parseArguments(roomId: string, event: any, mjolnir: Mjolni defaultShortcode = data['shortcode']; } catch (e) { LogService.warn("UnbanBanCommand", "Non-fatal error getting default ban list"); - LogService.warn("UnbanBanCommand", e); + LogService.warn("UnbanBanCommand", extractRequestError(e)); // Assume no default. } diff --git a/src/models/BanList.ts b/src/models/BanList.ts index 71c6f463..877dd772 100644 --- a/src/models/BanList.ts +++ b/src/models/BanList.ts @@ -1,5 +1,5 @@ /* -Copyright 2019 The Matrix.org Foundation C.I.C. +Copyright 2019-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { LogService, MatrixClient } from "matrix-bot-sdk"; +import { extractRequestError, LogService, MatrixClient } from "matrix-bot-sdk"; import { ListRule } from "./ListRule"; export const RULE_USER = "m.room.rule.user"; @@ -50,7 +50,7 @@ export default class BanList { const currentShortcode = this.shortcode; this.shortcode = newShortcode; this.client.sendStateEvent(this.roomId, SHORTCODE_EVENT_TYPE, '', {shortcode: this.shortcode}).catch(err => { - LogService.error("BanList", err); + LogService.error("BanList", extractRequestError(err)); if (this.shortcode === newShortcode) this.shortcode = currentShortcode; }); } diff --git a/src/queues/AutomaticRedactionQueue.ts b/src/queues/AutomaticRedactionQueue.ts index b2fec7dc..a2a4efb2 100644 --- a/src/queues/AutomaticRedactionQueue.ts +++ b/src/queues/AutomaticRedactionQueue.ts @@ -1,5 +1,5 @@ /* -Copyright 2019 The Matrix.org Foundation C.I.C. +Copyright 2019-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { LogLevel, LogService, MatrixClient, Permalinks } from "matrix-bot-sdk"; +import { extractRequestError, LogLevel, LogService, MatrixClient, Permalinks } from "matrix-bot-sdk"; import { logMessage } from "../LogProxy"; import config from "../config"; @@ -44,7 +44,7 @@ export class AutomaticRedactionQueue { } } catch (e) { logMessage(LogLevel.WARN, "AutomaticRedactionQueue", `Unable to redact message: ${permalink}`); - LogService.warn("AutomaticRedactionQueue", e); + LogService.warn("AutomaticRedactionQueue", extractRequestError(e)); } } } diff --git a/src/utils.ts b/src/utils.ts index b0aa0b2e..1d78d7d1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ /* -Copyright 2019, 2020 The Matrix.org Foundation C.I.C. +Copyright 2019-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ limitations under the License. */ import { + extractRequestError, LogLevel, LogService, MatrixClient, @@ -194,7 +195,7 @@ export async function replaceRoomIdsWithPills(client: MatrixClient, text: string } catch (e) { // This is a recursive call, so tell the function not to try and call us await logMessage(LogLevel.WARN, "utils", `Failed to resolve room alias for ${roomId} - see console for details`, null, true); - LogService.warn("utils", e); + LogService.warn("utils", extractRequestError(e)); } const regexRoomId = new RegExp(escapeRegex(roomId), "g"); content.body = content.body.replace(regexRoomId, alias); diff --git a/yarn.lock b/yarn.lock index 5115fac3..44d1e9a2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1261,10 +1261,10 @@ make-error@^1.1.1: resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.5.tgz#efe4e81f6db28cadd605c70f29c831b58ef776c8" integrity sha512-c3sIjNUow0+8swNwVpqoH4YCShKNFkMaw6oH1mNS2haDZQqkeZFlHS3dhoeEbKKmJB4vXpJucU6oH75aDYeE9g== -matrix-bot-sdk@^0.5.18: - version "0.5.18" - resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.5.18.tgz#c4e4e657fd632604380fce7453370d659a6f1d88" - integrity sha512-NntJoGmZdGlBsJ+HuEi1zB0lcRd8BCiPH6hExeuRNKtMOVHhJIKUgd5IY2cTy5YxHGk+kbBbkpzooFvRxVkOrg== +matrix-bot-sdk@^0.5.19: + version "0.5.19" + resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.5.19.tgz#6ce13359ab53ea0af9dc3ebcbe288c5f6d9c02c6" + integrity sha512-RIPyvQPkOVp2yTKeDgp5rcn6z/DiKdHb6E8c69K+utai8ypRGtfDRj0PGqP+1XzqC9Wb1OFrESCUB5t0ffdC9g== dependencies: "@types/express" "^4.17.7" chalk "^4.1.0"