mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Better error handling
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
WATag,
|
||||
MessageLogLevel,
|
||||
AuthenticationCredentialsBrowser,
|
||||
BaileysError,
|
||||
} from './Constants'
|
||||
|
||||
/** Generate a QR code from the ref & the curve public key. This is scanned by the phone */
|
||||
@@ -213,7 +214,11 @@ export default class WAConnectionBase {
|
||||
timeoutMs: number = null,
|
||||
tag: string = null,
|
||||
) {
|
||||
return Utils.errorOnNon200Status(this.query(json, binaryTags, timeoutMs, tag))
|
||||
const response = await this.query(json, binaryTags, timeoutMs, tag)
|
||||
if (response.status && Math.floor(+response.status / 100) !== 2) {
|
||||
throw new BaileysError(`Unexpected status code: ${response.status}`, {query: json})
|
||||
}
|
||||
return response
|
||||
}
|
||||
/**
|
||||
* Query something from the WhatsApp servers
|
||||
|
||||
@@ -1,6 +1,19 @@
|
||||
import { WA } from '../Binary/Constants'
|
||||
import { proto } from '../../WAMessage/WAMessage'
|
||||
|
||||
|
||||
export class BaileysError extends Error {
|
||||
status?: number
|
||||
context: any
|
||||
|
||||
constructor (message: string, context: any) {
|
||||
super (message)
|
||||
this.name = 'BaileysError'
|
||||
this.status = context.status
|
||||
this.context = context
|
||||
}
|
||||
}
|
||||
|
||||
export enum MessageLogLevel {
|
||||
none=0,
|
||||
info=1,
|
||||
|
||||
@@ -2,6 +2,7 @@ import * as Crypto from 'crypto'
|
||||
import HKDF from 'futoin-hkdf'
|
||||
import Decoder from '../Binary/Decoder'
|
||||
import {platform, release} from 'os'
|
||||
import { BaileysError } from './Constants'
|
||||
|
||||
const platformMap = {
|
||||
'aix': 'AIX',
|
||||
@@ -55,12 +56,7 @@ export const createTimeout = (timeout) => new Promise(resolve => setTimeout(reso
|
||||
export function promiseTimeout<T>(ms: number, promise: Promise<T>) {
|
||||
if (!ms) return promise
|
||||
// Create a promise that rejects in <ms> milliseconds
|
||||
const timeout = new Promise((_, reject) => {
|
||||
const id = setTimeout(() => {
|
||||
clearTimeout(id)
|
||||
reject('Timed out')
|
||||
}, ms)
|
||||
})
|
||||
const timeout = createTimeout (ms).then (() => { throw new BaileysError ('Timed out', promise) })
|
||||
return Promise.race([promise, timeout]) as Promise<T>
|
||||
}
|
||||
// whatsapp requires a message tag for every message, we just use the timestamp as one
|
||||
@@ -77,16 +73,6 @@ export function generateClientID() {
|
||||
export function generateMessageID() {
|
||||
return randomBytes(10).toString('hex').toUpperCase()
|
||||
}
|
||||
|
||||
export function errorOnNon200Status(p: Promise<any>) {
|
||||
return p.then(json => {
|
||||
if (json.status && typeof json.status === 'number' && Math.floor(json.status / 100) !== 2) {
|
||||
throw new Error(`Unexpected status code: ${json.status}`)
|
||||
}
|
||||
return json
|
||||
})
|
||||
}
|
||||
|
||||
export function decryptWA (message: any, macKey: Buffer, encKey: Buffer, decoder: Decoder, fromMe: boolean=false): [string, Object, [number, number]?] {
|
||||
let commaIndex = message.indexOf(',') // all whatsapp messages have a tag and a comma, followed by the actual message
|
||||
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import * as Curve from 'curve25519-js'
|
||||
import * as Utils from './Utils'
|
||||
import WAConnectionBase from './Base'
|
||||
import { MessageLogLevel, WAMetric, WAFlag } from './Constants'
|
||||
import { MessageLogLevel, WAMetric, WAFlag, BaileysError } from './Constants'
|
||||
import { Presence } from '../WAClient/WAClient'
|
||||
|
||||
const StatusError = (message: any, description: string='unknown error') => new Error (`unexpected status: ${message.status} on JSON: ${JSON.stringify(message)}`)
|
||||
|
||||
export default class WAConnectionValidator extends WAConnectionBase {
|
||||
/** Authenticate the connection */
|
||||
protected async authenticate() {
|
||||
@@ -40,21 +38,21 @@ export default class WAConnectionValidator extends WAConnectionBase {
|
||||
}
|
||||
return this.generateKeysForAuth(json.ref) // generate keys which will in turn be the QR
|
||||
})
|
||||
.then(json => {
|
||||
.then(async json => {
|
||||
if ('status' in json) {
|
||||
switch (json.status) {
|
||||
case 401: // if the phone was unpaired
|
||||
throw StatusError (json, 'unpaired from phone')
|
||||
throw new BaileysError ('unpaired from phone', json)
|
||||
case 429: // request to login was denied, don't know why it happens
|
||||
throw StatusError (json, 'request denied, try reconnecting')
|
||||
throw new BaileysError ('request denied, try reconnecting', json)
|
||||
default:
|
||||
throw StatusError (json)
|
||||
throw new BaileysError ('unexpected status', json)
|
||||
}
|
||||
}
|
||||
// if its a challenge request (we get it when logging in)
|
||||
if (json[1]?.challenge) {
|
||||
return this.respondToChallenge(json[1].challenge)
|
||||
.then (() => this.waitForMessage('s2', []))
|
||||
await this.respondToChallenge(json[1].challenge)
|
||||
return this.waitForMessage('s2', [])
|
||||
}
|
||||
// otherwise just chain the promise further
|
||||
return json
|
||||
@@ -147,11 +145,11 @@ export default class WAConnectionValidator extends WAConnectionBase {
|
||||
return onValidationSuccess()
|
||||
} else {
|
||||
// if the checksums didn't match
|
||||
throw new Error ('HMAC validation failed')
|
||||
throw new BaileysError ('HMAC validation failed', json)
|
||||
}
|
||||
} else {
|
||||
// if we didn't get the connected field (usually we get this message when one opens WhatsApp on their phone)
|
||||
throw new Error (`incorrect JSON: ${JSON.stringify(json)}`)
|
||||
throw new BaileysError (`invalid JSON`, json)
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user