aboutsummaryrefslogtreecommitdiffstats
path: root/packages/dev-utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/dev-utils')
-rw-r--r--packages/dev-utils/CHANGELOG.json8
-rw-r--r--packages/dev-utils/src/callback_error_reporter.ts66
-rw-r--r--packages/dev-utils/src/index.ts1
3 files changed, 75 insertions, 0 deletions
diff --git a/packages/dev-utils/CHANGELOG.json b/packages/dev-utils/CHANGELOG.json
index 9da323f48..97ba89e3e 100644
--- a/packages/dev-utils/CHANGELOG.json
+++ b/packages/dev-utils/CHANGELOG.json
@@ -1,5 +1,13 @@
[
{
+ "version": "0.4.2",
+ "changes": [
+ {
+ "note": "Move callbackErrorReporter over from 0x.js"
+ }
+ ]
+ },
+ {
"timestamp": 1525477860,
"version": "0.4.1",
"changes": [
diff --git a/packages/dev-utils/src/callback_error_reporter.ts b/packages/dev-utils/src/callback_error_reporter.ts
new file mode 100644
index 000000000..874343ccc
--- /dev/null
+++ b/packages/dev-utils/src/callback_error_reporter.ts
@@ -0,0 +1,66 @@
+import * as chai from 'chai';
+import * as _ from 'lodash';
+
+import { DoneCallback } from '@0xproject/types';
+
+const expect = chai.expect;
+
+export const callbackErrorReporter = {
+ reportNoErrorCallbackErrors(done: DoneCallback, expectToBeCalledOnce = true) {
+ return <T>(f?: (value: T) => void) => {
+ const wrapped = (value: T) => {
+ if (_.isUndefined(f)) {
+ done();
+ return;
+ }
+ try {
+ f(value);
+ if (expectToBeCalledOnce) {
+ done();
+ }
+ } catch (err) {
+ done(err);
+ }
+ };
+ return wrapped;
+ };
+ },
+ reportNodeCallbackErrors(done: DoneCallback, expectToBeCalledOnce = true) {
+ return <T>(f?: (value: T) => void) => {
+ const wrapped = (error: Error | null, value: T | undefined) => {
+ if (!_.isNull(error)) {
+ done(error);
+ } else {
+ if (_.isUndefined(f)) {
+ done();
+ return;
+ }
+ try {
+ f(value as T);
+ if (expectToBeCalledOnce) {
+ done();
+ }
+ } catch (err) {
+ done(err);
+ }
+ }
+ };
+ return wrapped;
+ };
+ },
+ assertNodeCallbackError(done: DoneCallback, errMsg: string) {
+ const wrapped = <T>(error: Error | null, value: T | undefined) => {
+ if (_.isNull(error)) {
+ done(new Error('Expected callback to receive an error'));
+ } else {
+ try {
+ expect(error.message).to.be.equal(errMsg);
+ done();
+ } catch (err) {
+ done(err);
+ }
+ }
+ };
+ return wrapped;
+ },
+};
diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts
index 2ff2a2238..9124f3e28 100644
--- a/packages/dev-utils/src/index.ts
+++ b/packages/dev-utils/src/index.ts
@@ -3,3 +3,4 @@ export { web3Factory } from './web3_factory';
export { constants as devConstants } from './constants';
export { coverage } from './coverage';
export { env, EnvVars } from './env';
+export { callbackErrorReporter } from './callback_error_reporter';