diff --git a/src/bot.ts b/src/bot.ts index 56badb03b6291b712f974734495fdc4afcb541a6..7e4d5b5e5db9d2e49a77e7f1a40b009f4d16e17c 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -27,7 +27,7 @@ import { RemoteUser, Unstable, } from "matrix-appservice-bridge"; -import { Util, wrap } from "./util"; +import { Util, wrapError } from "./util"; import { DiscordMessageProcessor, DiscordMessageProcessorOpts, @@ -458,7 +458,7 @@ export class DiscordBot { "Matrix Bridge: Allow rich user messages"); } } catch (err) { - throw wrap(err, Unstable.ForeignNetworkError, "Unable to create \"_matrix\" webhook"); + throw wrapError(err, Unstable.ForeignNetworkError, "Unable to create \"_matrix\" webhook"); } } try { @@ -485,7 +485,7 @@ export class DiscordBot { await this.StoreMessagesSent(msg, chan, event); this.unlockChannel(chan); } catch (err) { - throw wrap(err, Unstable.ForeignNetworkError, "Couldn't send message"); + throw wrapError(err, Unstable.ForeignNetworkError, "Couldn't send message"); } } diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index 635b20cf55fdf6c0f50fec061b5d550135d485d8..5f7fa69bf3b8d8360bcf393140fd176482b60b3b 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -18,7 +18,7 @@ import * as Discord from "discord.js"; import { DiscordBot } from "./bot"; import { DiscordBridgeConfig } from "./config"; import * as escapeStringRegexp from "escape-string-regexp"; -import { Util, wrap } from "./util"; +import { Util, wrapError } from "./util"; import * as path from "path"; import * as mime from "mime"; import { @@ -141,7 +141,7 @@ export class MatrixEventProcessor { await this.HandleEncryptionWarning(event.room_id); return; } catch (err) { - throw wrap(err, Unstable.EventNotHandledError, `Failed to handle encrypted room, ${err}`); + throw wrapError(err, Unstable.EventNotHandledError, `Failed to handle encrypted room, ${err}`); } } else { throw new Unstable.EventUnknownError("Got non m.room.message event"); diff --git a/src/util.ts b/src/util.ts index f8da79e939dc26401ed577b91e05e06cfc8844d4..66878d6af46760dfae0e23a8e75800311e93b636 100644 --- a/src/util.ts +++ b/src/util.ts @@ -434,11 +434,17 @@ export function instanceofsome(obj: object, types: Type[]): boolean { /** * Append the old error message to the new one and keep its stack trace. - * Example: - * throw wrap(e, HighLevelError, "This error is more specific"); + * + * @example + * throw wrapError(e, HighLevelError, "This error is more specific"); + * + * @param oldError The original error to wrap. + * @param newErrorType Type of the error returned by this function. + * @returns A new error of type `newErrorType` containing the information of + * the original error (stacktrace and error message). */ -export function wrap<T extends Error>( - oldError: Type, +export function wrapError<T extends Error>( + oldError: object|Error, newErrorType: new (...args: any[]) => T, // tslint:disable-line no-any ...args: any[] // tslint:disable-line no-any trailing-comma ): T {