mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Add stack traces to promiseTimeout & delay
This commit is contained in:
@@ -45,15 +45,18 @@ export class BaileysError extends Error {
|
|||||||
status?: number
|
status?: number
|
||||||
context: any
|
context: any
|
||||||
|
|
||||||
constructor (message: string, context: any) {
|
constructor (message: string, context: any, stack?: string) {
|
||||||
super (message)
|
super (message)
|
||||||
this.name = 'BaileysError'
|
this.name = 'BaileysError'
|
||||||
this.status = context.status
|
this.status = context.status
|
||||||
this.context = context
|
this.context = context
|
||||||
|
if(stack) {
|
||||||
|
this.stack = stack
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export const TimedOutError = () => new BaileysError ('timed out', { status: 408 })
|
export const TimedOutError = (stack?: string) => new BaileysError ('timed out', { status: 408 })
|
||||||
export const CancelledError = () => new BaileysError ('cancelled', { status: 500 })
|
export const CancelledError = (stack?: string) => new BaileysError ('cancelled', { status: 500 })
|
||||||
|
|
||||||
export interface WAQuery {
|
export interface WAQuery {
|
||||||
json: any[] | WANode
|
json: any[] | WANode
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ export const unixTimestampSeconds = (date: Date = new Date()) => Math.floor(date
|
|||||||
|
|
||||||
export const delay = (ms: number) => delayCancellable (ms).delay
|
export const delay = (ms: number) => delayCancellable (ms).delay
|
||||||
export const delayCancellable = (ms: number) => {
|
export const delayCancellable = (ms: number) => {
|
||||||
|
const stack = new Error().stack
|
||||||
let timeout: NodeJS.Timeout
|
let timeout: NodeJS.Timeout
|
||||||
let reject: (error) => void
|
let reject: (error) => void
|
||||||
const delay: Promise<void> = new Promise((resolve, _reject) => {
|
const delay: Promise<void> = new Promise((resolve, _reject) => {
|
||||||
@@ -111,18 +112,18 @@ export const delayCancellable = (ms: number) => {
|
|||||||
})
|
})
|
||||||
const cancel = () => {
|
const cancel = () => {
|
||||||
clearTimeout (timeout)
|
clearTimeout (timeout)
|
||||||
reject (CancelledError())
|
reject (CancelledError(stack))
|
||||||
}
|
}
|
||||||
return { delay, cancel }
|
return { delay, cancel }
|
||||||
}
|
}
|
||||||
export async function promiseTimeout<T>(ms: number, promise: (resolve: (v?: T)=>void, reject: (error) => void) => void) {
|
export async function promiseTimeout<T>(ms: number, promise: (resolve: (v?: T)=>void, reject: (error) => void) => void) {
|
||||||
if (!ms) return new Promise (promise)
|
if (!ms) return new Promise (promise)
|
||||||
|
const stack = new Error().stack
|
||||||
// Create a promise that rejects in <ms> milliseconds
|
// Create a promise that rejects in <ms> milliseconds
|
||||||
let {delay, cancel} = delayCancellable (ms)
|
let {delay, cancel} = delayCancellable (ms)
|
||||||
const p = new Promise ((resolve, reject) => {
|
const p = new Promise ((resolve, reject) => {
|
||||||
delay
|
delay
|
||||||
.then(() => reject(TimedOutError()))
|
.then(() => reject(TimedOutError(stack)))
|
||||||
.catch (err => reject(err))
|
.catch (err => reject(err))
|
||||||
|
|
||||||
promise (resolve, reject)
|
promise (resolve, reject)
|
||||||
|
|||||||
Reference in New Issue
Block a user