Skip to content

Commit

Permalink
use gerror for Error (wechaty#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Dec 1, 2021
1 parent c93ce93 commit 5b8c9a8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
10 changes: 5 additions & 5 deletions examples/ding-dong-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ const puppet = new PuppetWeChat()
*
*/
puppet
.on('logout', onLogout)
.on('login', onLogin)
.on('scan', onScan)
.on('error', onError)
.on('message', onMessage)
.on('logout', onLogout)
.on('login', onLogin)
.on('scan', onScan)
.on('error', onError)
.on('message', onMessage)

/**
*
Expand Down
23 changes: 13 additions & 10 deletions src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import stealthPlugin from 'puppeteer-extra-plugin-stealth'
import { StateSwitch } from 'state-switch'
import { parseString } from 'xml2js'

import { wrapAsyncError } from 'gerror'
import {
wrapAsyncError,
GError,
} from 'gerror'
import type {
MemoryCard,
} from 'memory-card'
Expand Down Expand Up @@ -74,7 +77,7 @@ export class Bridge extends EventEmitter {
private page : undefined | puppeteer.Page
private state : StateSwitch

private wrapAsync = wrapAsyncError(e => this.emit('error', e))
private wrapAsync = wrapAsyncError(e => this.emit('error', GError.from(e)))

constructor (
public options: BridgeOptions,
Expand Down Expand Up @@ -116,7 +119,7 @@ export class Bridge extends EventEmitter {
log.error('PuppetWeChatBridge', 'start() exception %s, close page/browser exception %s', e, e2)
}

this.emit('error', e as Error)
this.emit('error', GError.from(e))
throw e
}
}
Expand Down Expand Up @@ -180,7 +183,7 @@ export class Bridge extends EventEmitter {
} catch (e) {
log.error('PuppetWeChatBridge', 'onDialog() dialog.dismiss() reject: %s', e as Error)
}
this.emit('error', new Error(`${dialog.type}(${dialog.message()})`))
this.emit('error', GError.from(`${dialog.type}(${dialog.message()})`))
}

public async onLoad (page: puppeteer.Page): Promise<void> {
Expand Down Expand Up @@ -212,7 +215,7 @@ export class Bridge extends EventEmitter {
} catch (e) {
log.error('PuppetWeChatBridge', 'onLoad() exception: %s', e as Error)
await page.close()
this.emit('error', e as Error)
this.emit('error', GError.from(e))
}
}

Expand All @@ -229,7 +232,7 @@ export class Bridge extends EventEmitter {
*/
await this.uosPatch(page)

page.on('error', e => this.emit('error', e as Error))
page.on('error', e => this.emit('error', GError.from(e)))
page.on('dialog', this.wrapAsync(this.onDialog.bind(this)))

const cookieList = (
Expand Down Expand Up @@ -815,7 +818,7 @@ export class Bridge extends EventEmitter {
})
.catch(e => {
log.error('PuppetWeChatBridge', 'ding(%s) exception: %s', data, (e as Error).message)
this.emit('error', e as Error)
this.emit('error', GError.from(e))
})
}

Expand Down Expand Up @@ -1003,7 +1006,7 @@ export class Bridge extends EventEmitter {
return hostname
} catch (e) {
log.error('PuppetWeChatBridge', 'hostname() exception: %s', e as Error)
this.emit('error', e as Error)
this.emit('error', GError.from(e))
return null
}
}
Expand All @@ -1021,7 +1024,7 @@ export class Bridge extends EventEmitter {
await this.page.setCookie(...cookieList)
} catch (e) {
log.error('PuppetWeChatBridge', 'cookies(%s) reject: %s', cookieList, e as Error)
this.emit('error', e as Error)
this.emit('error', GError.from(e))
}
// RETURN
} else {
Expand Down Expand Up @@ -1095,7 +1098,7 @@ export class Bridge extends EventEmitter {
return await this.page.evaluate(fn, ...args)
} catch (e) {
log.error('PuppetWeChatBridge', 'evaluate() exception: %s', e as Error)
this.emit('error', e as Error)
this.emit('error', GError.from(e))
return null
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
import {
normalizeScanStatus,
} from './pure-function-helpers/normalize-scan-status.js'
import { GError } from 'wechaty-puppet/helper'

export const Event = {
onDing,
Expand Down Expand Up @@ -121,7 +122,7 @@ async function onLogin (
if (ttl <= 0) {
log.verbose('PuppetWeChatEvent', 'onLogin(%s) TTL expired')
this.emit('error', {
data: 'onLogin() TTL expired.',
gerror: GError.from('onLogin() TTL expired.').toJSON(),
})
return
}
Expand Down Expand Up @@ -239,7 +240,7 @@ async function onUnload (this: PuppetWeChat): Promise<void> {
await this.init()
} catch (e) {
log.error('PuppetWeChatEvent', 'onUnload() exception: %s', e as Error)
this.emit('error', e as Error)
this.emit('error', GError.from(e))
throw e
}
*/
Expand Down
12 changes: 6 additions & 6 deletions src/puppet-wechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
Watchdog,
WatchdogFood,
} from 'watchdog'

import { GError } from 'gerror'
import * as PUPPET from 'wechaty-puppet'
import { log } from 'wechaty-puppet'
import {
Expand Down Expand Up @@ -226,7 +226,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
} catch (e) {
log.error('PuppetWeChat', 'initScanWatchdog() on(reset) recover FAIL: %s', e as Error)
this.emit('error', {
data: (e as Error).message,
gerror: GError.stringify(e),
})
}
}
Expand All @@ -247,7 +247,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
this.bridge.on('heartbeat', (data: string) => this.emit('heartbeat', { data: data + 'bridge ding' }))

this.bridge.on('error', (e: Error) => this.emit('error', {
data: (e as Error).message,
gerror: GError.stringify(e),
}))
this.bridge.on('log', Event.onLog.bind(this))
this.bridge.on('login', this.wrapAsync(Event.onLogin.bind(this)))
Expand All @@ -264,7 +264,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
log.error('PuppetWeChat', 'initBridge() this.bridge.stop() rejection: %s', e as Error)
})
this.emit('error', {
data: (e as Error).message,
gerror: GError.stringify(e),
})

throw e
Expand Down Expand Up @@ -1098,7 +1098,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
} catch (e) {
log.error('PuppetWeChat', 'hostname() exception:%s', e as Error)
this.emit('error', {
data: (e as Error).message,
gerror: GError.stringify(e),
})
throw e
}
Expand Down Expand Up @@ -1364,7 +1364,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
log.error('PuppetWeChat', 'updateMedia() body = %s', body)
log.error('PuppetWeChat', 'updateMedia() exception: %s', e as Error)
this.emit('error', {
data: (e as Error).message,
gerror: GError.stringify(e),
})
}
}
Expand Down

0 comments on commit 5b8c9a8

Please sign in to comment.