diff --git a/package.json b/package.json index 4197c47f..2df13f93 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "brolog": "^1.13.6", "clone-class": "^1.0.2", "file-box": "^1.2.3", - "gerror": "^0.2.1", + "gerror": "^1.0.2", "memory-card": "^0.12.2", "state-switch": "^1.1.14", "typed-emitter": "^1.4.0", diff --git a/src/helpers/wrap-async-error.spec.ts b/src/helpers/wrap-async-error.spec.ts deleted file mode 100755 index 2e8cf219..00000000 --- a/src/helpers/wrap-async-error.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env -S node --no-warnings --loader ts-node/esm - -import { - test, - sinon, -} from 'tstest' - -import { - wrapAsyncError, -} from './wrap-async-error.js' - -test('wrapAsyncError() smoke testing', async t => { - const spy = sinon.spy() - - const wrapAsync = wrapAsyncError(spy) - - const DATA = 'test' - const promise = Promise.resolve(DATA) - const wrappedPromise = wrapAsync(promise) - t.equal(await wrappedPromise, undefined, 'should resolve Promise to void') - - const rejection = Promise.reject(new Error('test')) - const wrappedRejection = wrapAsync(rejection) - t.equal(wrappedRejection, undefined, 'should be void and not to reject') - - t.equal(spy.callCount, 0, 'should have no error before sleep') - await new Promise(resolve => setImmediate(resolve)) // wait async event loop task to be executed - t.equal(spy.callCount, 1, 'should emit error when promise reject with error') -}) diff --git a/src/helpers/wrap-async-error.ts b/src/helpers/wrap-async-error.ts deleted file mode 100644 index 95d300f8..00000000 --- a/src/helpers/wrap-async-error.ts +++ /dev/null @@ -1,50 +0,0 @@ -/** - * 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 - */ -interface WrapAsync { - - (promise: Promise): void - - Promise> ( - asyncFunction: T, - ): (...args: Parameters) => void - -} - -type OnErrorCallback = (error: any) => void - -const wrapAsyncError: (onError: OnErrorCallback) => WrapAsync = onError => Promise> ( - asyncStaff: T | Promise, -) => { - /** - * 1. Promise - */ - if (asyncStaff instanceof Promise) { - asyncStaff - .then(_ => _) - .catch(e => onError(e)) - /** - * Huan(202110) FIXME: - * The same code works inside the Wechaty/Puppet class - * but here we have to use `as any` to bypass the type check - */ - return undefined as any - } - - /** - * 2. Function - */ - return function (this: any, ...args: Parameters): void { - asyncStaff.apply(this, args).catch(onError) - } -} - -export type { - WrapAsync, -} -export { - wrapAsyncError, -} diff --git a/src/mods/helpers.ts b/src/mods/helpers.ts index 62697a7c..34c233c6 100644 --- a/src/mods/helpers.ts +++ b/src/mods/helpers.ts @@ -9,14 +9,12 @@ import { } from 'state-switch' import { GError, + wrapAsyncError, } from 'gerror' import type { WrapAsync, -} from '../helpers/wrap-async-error.js' -import { - wrapAsyncError, -} from '../helpers/wrap-async-error.js' +} from 'gerror' export type { WrapAsync, diff --git a/src/puppet/puppet-skelton.ts b/src/puppet/puppet-skelton.ts index bfeeed0f..4f932ce6 100644 --- a/src/puppet/puppet-skelton.ts +++ b/src/puppet/puppet-skelton.ts @@ -26,14 +26,13 @@ import { * @see https://github.com/wechaty/puppet/issues/165 */ -import { GError } from 'gerror' - -import type { - WrapAsync, -} from '../helpers/wrap-async-error.js' import { + GError, wrapAsyncError, -} from '../helpers/wrap-async-error.js' +} from 'gerror' +import type { + WrapAsync, +} from 'gerror' import type { EventErrorPayload } from '../schemas/event.js'