From 53cf1ecb39eac0c450c7757b00a93c938d3e6cd1 Mon Sep 17 00:00:00 2001
From: "Kai A. Hiller" <V02460@gmail.com>
Date: Wed, 7 Aug 2019 10:37:47 -0400
Subject: [PATCH] Make wrapError easier to understand

Signed-off-by: Kai A. Hiller <V02460@gmail.com>
---
 src/bot.ts                  |  6 +++---
 src/matrixeventprocessor.ts |  4 ++--
 src/util.ts                 | 14 ++++++++++----
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/bot.ts b/src/bot.ts
index 56badb0..7e4d5b5 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 635b20c..5f7fa69 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 f8da79e..66878d6 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 {
-- 
GitLab