From 6ae56a2ee0dc511f0285d817b1f742c7ee5fbb42 Mon Sep 17 00:00:00 2001
From: "Kai A. Hiller" <V02460@gmail.com>
Date: Tue, 6 Aug 2019 10:49:41 -0400
Subject: [PATCH] Spawn foreign bridge errors

Signed-off-by: Kai A. Hiller <V02460@gmail.com>
---
 src/bot.ts                  | 19 +++++++++++++++----
 src/matrixeventprocessor.ts |  9 +++------
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/src/bot.ts b/src/bot.ts
index 8051c0b..400bf1f 100644
--- a/src/bot.ts
+++ b/src/bot.ts
@@ -19,8 +19,15 @@ import { DiscordClientFactory } from "./clientfactory";
 import { DiscordStore } from "./store";
 import { DbEmoji } from "./db/dbdataemoji";
 import { DbEvent } from "./db/dbdataevent";
-import { MatrixUser, RemoteUser, Bridge, Entry, Intent } from "matrix-appservice-bridge";
-import { Util } from "./util";
+import {
+    Bridge,
+    Entry,
+    Intent,
+    MatrixUser,
+    RemoteUser,
+    unstable,
+} from "matrix-appservice-bridge";
+import { Util, wrap } from "./util";
 import {
     DiscordMessageProcessor,
     DiscordMessageProcessorOpts,
@@ -423,6 +430,10 @@ export class DiscordBot {
         this.unlockChannel(channel);
     }
 
+    /**
+     * Sends an event to Discord.
+     * @throws {unstable.ForeignNetworkError}
+     */
     public async send(
         embedSet: IMatrixEventProcessorResult,
         opts: Discord.MessageOptions,
@@ -447,7 +458,7 @@ export class DiscordBot {
                         "Matrix Bridge: Allow rich user messages");
                 }
             } catch (err) {
-                log.error("Unable to create \"_matrix\" webhook. ", err);
+                throw wrap(err, unstable.ForeignNetworkError, "Unable to create \"_matrix\" webhook");
             }
         }
         try {
@@ -474,7 +485,7 @@ export class DiscordBot {
             await this.StoreMessagesSent(msg, chan, event);
             this.unlockChannel(chan);
         } catch (err) {
-            log.error("Couldn't send message. ", err);
+            throw wrap(err, unstable.ForeignNetworkError, "Couldn't send message");
         }
     }
 
diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts
index 4979082..461a4f0 100644
--- a/src/matrixeventprocessor.ts
+++ b/src/matrixeventprocessor.ts
@@ -131,11 +131,7 @@ export class MatrixEventProcessor {
             if (isBotCommand(event)) {
                 await this.mxCommandHandler.Process(event, context);
             } else {
-                try {
-                    await this.ProcessMsgEvent(event, context);
-                } catch (err) {
-                    throw wrap(err, Error, "There was an error sending a matrix event");
-                }
+                await this.ProcessMsgEvent(event, context);
             }
             return;
         } else if (event.type === "m.room.encryption" && context.rooms.remote) {
@@ -175,6 +171,7 @@ export class MatrixEventProcessor {
      *
      * @param event The message event to process.
      * @param context Context of the bridge.
+     * @throws {unstable.ForeignNetworkError}
      */
     public async ProcessMsgEvent(event: IMatrixEvent, context: BridgeContext): Promise<void> {
         const room = context.rooms.remote;
@@ -199,7 +196,7 @@ export class MatrixEventProcessor {
             opts.file = file;
         }
 
-        await this.discord.send(embedSet, opts, roomLookup, event);
+        await this.discord.send(embedSet, opts, roomLookup, event); // throws
         await this.sendReadReceipt(event);
     }
 
-- 
GitLab