Browse Source

fix handling errors of failed delivery

Mike L 3 years ago
parent
commit
2a167db3f4
7 changed files with 15 additions and 19 deletions
  1. 1 1
      dist/koishi.d.ts
  2. 4 4
      dist/koishi.js
  3. 0 0
      dist/koishi.js.map
  4. 3 5
      dist/twitter.js
  5. 0 0
      dist/twitter.js.map
  6. 3 4
      src/koishi.ts
  7. 4 5
      src/twitter.ts

+ 1 - 1
dist/koishi.d.ts

@@ -31,7 +31,7 @@ export default class {
     private getChat;
     private sendToGroup;
     private sendToUser;
-    sendTo: (subscriber: IChat, messageChain: string) => Promise<void>;
+    sendTo: (subscriber: IChat, messageChain: string, noErrors?: boolean) => Promise<void>;
     private initBot;
     private listen;
     connect: () => Promise<void>;

+ 4 - 4
dist/koishi.js

@@ -90,7 +90,7 @@ class default_1 {
         this.sendToUser = (userID, message) => new Promise((resolve, reject) => {
             this.enqueue('private', userID, () => this.bot.sendPrivateMessage(userID, message).then(resolve).catch(reject));
         });
-        this.sendTo = (subscriber, messageChain) => Promise.all((splitted => [splitted.message, ...splitted.attachments])(exports.Message.separateAttachment(messageChain)).map(msg => {
+        this.sendTo = (subscriber, messageChain, noErrors = false) => Promise.all((splitted => [splitted.message, ...splitted.attachments])(exports.Message.separateAttachment(messageChain)).map(msg => {
             switch (subscriber.chatType) {
                 case 'group':
                     return this.sendToGroup(subscriber.chatID.toString(), msg);
@@ -106,9 +106,9 @@ class default_1 {
             logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response: ${response}`);
         })
             .catch(reason => {
-            reason = exports.Message.ellipseBase64(reason);
-            logger.error(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`);
-            throw Error(reason);
+            logger.error(exports.Message.ellipseBase64(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`));
+            if (!noErrors)
+                throw reason instanceof Error ? reason : Error(reason);
         });
         this.initBot = () => {
             this.app = new koishi_1.App({

File diff suppressed because it is too large
+ 0 - 0
dist/koishi.js.map


+ 3 - 5
dist/twitter.js

@@ -14,7 +14,6 @@ const fs = require("fs");
 const path = require("path");
 const Twitter = require("twitter");
 const loggers_1 = require("./loggers");
-const koishi_1 = require("./koishi");
 const utils_1 = require("./utils");
 const webshot_1 = require("./webshot");
 class ScreenNameNormalizer {
@@ -142,15 +141,14 @@ class default_1 {
         };
         this.sendTweets = (source, ...to) => (msg, text, author) => {
             to.forEach(subscriber => {
-                logger.info(`pushing data${source ? ` of ${koishi_1.Message.ellipseBase64(source)}` : ''} to ${JSON.stringify(subscriber)}`);
+                logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
                 retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
                     if (count <= maxTrials) {
                         logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
                     }
                     else {
-                        logger.warn(`${count - 1} consecutive failures while sending` +
-                            'message chain, trying plain text instead...');
-                        terminate(this.bot.sendTo(subscriber, author + text));
+                        logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
+                        terminate(this.bot.sendTo(subscriber, author + text, true));
                     }
                 });
             });

File diff suppressed because it is too large
+ 0 - 0
dist/twitter.js.map


+ 3 - 4
src/koishi.ts

@@ -104,7 +104,7 @@ export default class {
     this.enqueue('private', userID, () => this.bot.sendPrivateMessage(userID, message).then(resolve).catch(reject));
   });
 
-  public sendTo = (subscriber: IChat, messageChain: string) => Promise.all(
+  public sendTo = (subscriber: IChat, messageChain: string, noErrors = false) => Promise.all(
     (splitted => [splitted.message, ...splitted.attachments])(
       Message.separateAttachment(messageChain)
     ).map(msg => {
@@ -122,9 +122,8 @@ export default class {
       logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response: ${response}`);
     })
     .catch(reason => {
-      reason = Message.ellipseBase64(reason);
-      logger.error(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`);
-      throw Error(reason);
+      logger.error(Message.ellipseBase64(`error pushing data to ${JSON.stringify(subscriber.chatID)}, reason: ${reason}`));
+      if (!noErrors) throw reason instanceof Error ? reason : Error(reason);
     });
 
   private initBot = () => {

+ 4 - 5
src/twitter.ts

@@ -4,7 +4,7 @@ import * as Twitter from 'twitter';
 import TwitterTypes from 'twitter-d';
 
 import { getLogger } from './loggers';
-import QQBot, { Message } from './koishi';
+import QQBot from './koishi';
 import { chainPromises, BigNumOps } from './utils';
 import Webshot from './webshot';
 
@@ -276,16 +276,15 @@ export default class {
 
   private sendTweets = (source?: string, ...to: IChat[]) => (msg: string, text: string, author: string) => {
     to.forEach(subscriber => {
-      logger.info(`pushing data${source ? ` of ${Message.ellipseBase64(source)}` : ''} to ${JSON.stringify(subscriber)}`);
+      logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
       retryOnError(
         () => this.bot.sendTo(subscriber, msg),
         (_, count, terminate: (doNothing: Promise<void>) => void) => {
           if (count <= maxTrials) {
             logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
           } else {
-            logger.warn(`${count - 1} consecutive failures while sending` +
-            'message chain, trying plain text instead...');
-            terminate(this.bot.sendTo(subscriber, author + text));
+            logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
+            terminate(this.bot.sendTo(subscriber, author + text, true));
           }
         });
     });

Some files were not shown because too many files changed in this diff