UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

51 lines 1.78 kB
import { log } from '@juzi/wechaty-puppet'; import { GError, wrapAsyncError, } from 'gerror'; import { wechatyCaptureException } from '../raven.js'; const gErrorMixin = (mixinBase) => { log.verbose('WechatyGErrorMixin', 'gErrorMixin(%s)', mixinBase.name); class GErrorMixin extends mixinBase { constructor(...args) { super(...args); } /** * Wrap promise in sync way (catch error by emitting it) * 1. convert a async callback function to be sync function * by catcing any errors and emit them to error event * 2. wrap a Promise by catcing any errors and emit them to error event */ wrapAsync = wrapAsyncError((e) => this.emit('error', e)); /** * Wechaty internally can use `emit('error' whatever)` to emit any error * But the external call can only emit GError. * That's the reason why we need the below `emitError(e: any) */ emitError(e) { this.emit('error', e); } /** * Convert any error to GError, * and emit `error` event with GError */ emit(event, ...args) { if (event !== 'error') { return super.emit(event, ...args); } /** * Dealing with the `error` event */ const arg0 = args[0]; let gerror; if (arg0 instanceof GError) { gerror = arg0; } else { gerror = GError.from(arg0); } wechatyCaptureException(gerror); return super.emit('error', gerror); } } return GErrorMixin; }; export { gErrorMixin, }; //# sourceMappingURL=gerror-mixin.js.map