mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Stopped throwing literals :/
This commit is contained in:
@@ -75,7 +75,7 @@ export default class WAConnectionBase {
|
||||
*/
|
||||
loadAuthInfoFromBase64(authInfo: AuthenticationCredentialsBase64 | string) {
|
||||
if (!authInfo) {
|
||||
throw 'given authInfo is null'
|
||||
throw new Error('given authInfo is null')
|
||||
}
|
||||
if (typeof authInfo === 'string') {
|
||||
this.log(`loading authentication credentials from ${authInfo}`)
|
||||
@@ -96,7 +96,7 @@ export default class WAConnectionBase {
|
||||
*/
|
||||
loadAuthInfoFromBrowser(authInfo: AuthenticationCredentialsBrowser | string) {
|
||||
if (!authInfo) {
|
||||
throw 'given authInfo is null'
|
||||
throw new Error('given authInfo is null')
|
||||
}
|
||||
if (typeof authInfo === 'string') {
|
||||
this.log(`loading authentication credentials from ${authInfo}`)
|
||||
@@ -130,7 +130,7 @@ export default class WAConnectionBase {
|
||||
return this.registerCallback([parameters, null, null], callback)
|
||||
}
|
||||
if (!Array.isArray(parameters)) {
|
||||
throw 'parameters (' + parameters + ') must be a string or array'
|
||||
throw new Error('parameters (' + parameters + ') must be a string or array')
|
||||
}
|
||||
const func = 'function:' + parameters[0]
|
||||
const key = parameters[1] || ''
|
||||
@@ -152,7 +152,7 @@ export default class WAConnectionBase {
|
||||
return this.deregisterCallback([parameters])
|
||||
}
|
||||
if (!Array.isArray(parameters)) {
|
||||
throw 'parameters (' + parameters + ') must be a string or array'
|
||||
throw new Error('parameters (' + parameters + ') must be a string or array')
|
||||
}
|
||||
const func = 'function:' + parameters[0]
|
||||
const key = parameters[1] || ''
|
||||
@@ -250,7 +250,7 @@ export default class WAConnectionBase {
|
||||
/** Send some message to the WhatsApp servers */
|
||||
protected send(m) {
|
||||
if (!this.conn) {
|
||||
throw 'cannot send message, disconnected from WhatsApp'
|
||||
throw new Error('cannot send message, disconnected from WhatsApp')
|
||||
}
|
||||
this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages
|
||||
this.conn.send(m)
|
||||
@@ -261,7 +261,7 @@ export default class WAConnectionBase {
|
||||
*/
|
||||
async logout() {
|
||||
if (!this.conn) {
|
||||
throw "You're not even connected, you can't log out"
|
||||
throw new Error("You're not even connected, you can't log out")
|
||||
}
|
||||
await new Promise((resolve) => {
|
||||
this.conn.send('goodbye,["admin","Conn","disconnect"]', null, () => {
|
||||
|
||||
@@ -25,7 +25,7 @@ export default class WAConnectionConnector extends WAConnectionValidator {
|
||||
async connectSlim(authInfo: AuthenticationCredentialsBase64 | string = null, timeoutMs: number = null) {
|
||||
// if we're already connected, throw an error
|
||||
if (this.conn) {
|
||||
throw [1, 'already connected or connecting']
|
||||
throw new Error('already connected or connecting')
|
||||
}
|
||||
// set authentication credentials if required
|
||||
try {
|
||||
|
||||
@@ -58,7 +58,7 @@ describe('Test Connect', () => {
|
||||
assert.ok(chats)
|
||||
if (chats.length > 0) {
|
||||
assert.ok(chats[0].jid)
|
||||
assert.ok(chats[0].count)
|
||||
assert.ok(chats[0].count !== null)
|
||||
}
|
||||
assert.ok(contacts)
|
||||
if (contacts.length > 0) {
|
||||
|
||||
@@ -80,7 +80,7 @@ export function decryptWA (message: any, macKey: Buffer, encKey: Buffer, decoder
|
||||
const commaIndex = message.indexOf(',') // all whatsapp messages have a tag and a comma, followed by the actual message
|
||||
if (commaIndex < 0) {
|
||||
// if there was no comma, then this message must be not be valid
|
||||
throw [2, 'invalid message', message]
|
||||
throw Error ('invalid message: ' + message)
|
||||
}
|
||||
let data = message.slice(commaIndex+1, message.length)
|
||||
// get the message tag.
|
||||
@@ -99,7 +99,7 @@ export function decryptWA (message: any, macKey: Buffer, encKey: Buffer, decoder
|
||||
} else {
|
||||
if (!macKey || !encKey) {
|
||||
// if we recieved a message that was encrypted but we don't have the keys, then there must be an error
|
||||
throw [3, 'recieved encrypted message when auth creds not available', data]
|
||||
throw new Error ('recieved encrypted buffer when auth creds unavailable')
|
||||
}
|
||||
/*
|
||||
If the data recieved was not a JSON, then it must be an encrypted message.
|
||||
@@ -115,9 +115,8 @@ export function decryptWA (message: any, macKey: Buffer, encKey: Buffer, decoder
|
||||
const computedChecksum = hmacSign(data, macKey) // compute the sign of the message we recieved using our macKey
|
||||
|
||||
if (!checksum.equals(computedChecksum)) {
|
||||
throw [7, "checksums don't match"]
|
||||
throw new Error (`Checksums don't match:\nog: ${checksum.toString('hex')}\ncomputed: ${computedChecksum.toString('hex')}`)
|
||||
}
|
||||
|
||||
// the checksum the server sent, must match the one we computed for the message to be valid
|
||||
const decrypted = aesDecrypt(data, encKey) // decrypt using AES
|
||||
json = decoder.read(decrypted) // decode the binary message into a JSON array
|
||||
|
||||
@@ -2,6 +2,8 @@ import * as Curve from 'curve25519-js'
|
||||
import * as Utils from './Utils'
|
||||
import WAConnectionBase from './Base'
|
||||
|
||||
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() {
|
||||
@@ -37,31 +39,30 @@ export default class WAConnectionValidator extends WAConnectionBase {
|
||||
return this.generateKeysForAuth(json.ref)
|
||||
}
|
||||
default:
|
||||
throw [json.status, 'unknown error', json]
|
||||
throw StatusError (json)
|
||||
}
|
||||
})
|
||||
.then((json) => {
|
||||
if ('status' in json) {
|
||||
switch (json.status) {
|
||||
case 401: // if the phone was unpaired
|
||||
throw [json.status, 'unpaired from phone', json]
|
||||
throw StatusError (json, 'unpaired from phone')
|
||||
case 429: // request to login was denied, don't know why it happens
|
||||
throw [json.status, 'request denied, try reconnecting', json]
|
||||
case 304: // request to generate a new key for a QR code was denied
|
||||
throw [json.status, 'request for new key denied', json]
|
||||
throw StatusError (json, 'request denied, try reconnecting')
|
||||
default:
|
||||
throw [json.status, 'unknown error status', json]
|
||||
throw StatusError (json)
|
||||
}
|
||||
}
|
||||
if (json[1] && json[1].challenge) {
|
||||
// if its a challenge request (we get it when logging in)
|
||||
return this.respondToChallenge(json[1].challenge).then((json) => {
|
||||
if (json.status !== 200) {
|
||||
// throw an error if the challenge failed
|
||||
throw [json.status, 'unknown error', json]
|
||||
}
|
||||
return this.waitForMessage('s2', []) // otherwise wait for the validation message
|
||||
})
|
||||
return this.respondToChallenge(json[1].challenge)
|
||||
.then((json) => {
|
||||
if (json.status !== 200) {
|
||||
// throw an error if the challenge failed
|
||||
throw StatusError (json)
|
||||
}
|
||||
return this.waitForMessage('s2', []) // otherwise wait for the validation message
|
||||
})
|
||||
} else {
|
||||
// otherwise just chain the promise further
|
||||
return json
|
||||
@@ -107,7 +108,7 @@ export default class WAConnectionValidator extends WAConnectionBase {
|
||||
}
|
||||
const secret = Buffer.from(json.secret, 'base64')
|
||||
if (secret.length !== 144) {
|
||||
throw [4, 'incorrect secret length: ' + secret.length]
|
||||
throw new Error ('incorrect secret length received: ' + secret.length)
|
||||
}
|
||||
// generate shared key from our private key & the secret shared by the server
|
||||
const sharedKey = Curve.sharedKey(this.curveKeys.private, secret.slice(0, 32))
|
||||
@@ -140,11 +141,11 @@ export default class WAConnectionValidator extends WAConnectionBase {
|
||||
return onValidationSuccess()
|
||||
} else {
|
||||
// if the checksums didn't match
|
||||
throw [5, 'HMAC validation failed']
|
||||
throw new Error ('HMAC validation failed')
|
||||
}
|
||||
} else {
|
||||
// if we didn't get the connected field (usually we get this message when one opens WhatsApp on their phone)
|
||||
throw [6, 'json connection failed', json]
|
||||
throw new Error (`incorrect JSON: ${JSON.stringify(json)}`)
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user