Browse Source

Add error logging for connection failure

painor 3 năm trước cách đây
mục cha
commit
2dccf47ba2
3 tập tin đã thay đổi với 11 bổ sung4 xóa
  1. 2 2
      examples/mainjs.js
  2. 3 2
      gramjs/extensions/BinaryReader.ts
  3. 6 0
      gramjs/network/MTProtoSender.ts

+ 2 - 2
examples/mainjs.js

@@ -1,5 +1,5 @@
-const { TelegramClient } = require("telegram/dist");
-const { StringSession } = require("telegram/dist/sessions");
+const { TelegramClient } = require("telegram");
+const { StringSession } = require("telegram/sessions");
 
 (async () => {
   console.log("Loading interactive example...");

+ 3 - 2
gramjs/extensions/BinaryReader.ts

@@ -84,14 +84,15 @@ export class BinaryReader {
     /**
      * Read the given amount of bytes, or -1 to read all remaining.
      * @param length {number}
+     * @param checkLength {boolean} whether to check if the length overflows or not.
      */
-    read(length = -1) {
+    read(length = -1, checkLength = true) {
         if (length === -1) {
             length = this.stream.length - this.offset;
         }
         const result = this.stream.slice(this.offset, this.offset + length);
         this.offset += length;
-        if (result.length !== length) {
+        if (checkLength && result.length !== length) {
             throw Error(
                 `No more data left to read (need ${length}, got ${result.length}: ${result}); last read ${this._last}`
             );

+ 6 - 0
gramjs/network/MTProtoSender.ts

@@ -226,6 +226,9 @@ export class MTProtoSender {
                 this._log.error(
                     "WebSocket connection failed attempt : " + (attempt + 1)
                 );
+                if (this._log.canSend("error")) {
+                    console.error(e);
+                }
                 await sleep(this._delay);
             }
         }
@@ -886,6 +889,9 @@ export class MTProtoSender {
                 this._log.error(
                     "WebSocket connection failed attempt : " + (attempt + 1)
                 );
+                if (this._log.canSend("error")) {
+                    console.error(e);
+                }
                 await sleep(this._delay);
             }
         }