From c501353388eb7e0f6a30f6ee491b7ea73e688031 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Wed, 23 Jan 2019 08:52:47 -0500 Subject: [PATCH] Improve castError handling of non strings (#13315) This pull removes castError from reduxRoutine and just passes along the value thrown instead of potentially coercing to an error. - Exposes values to consuming code. - Less opinionated about what happens with the error. --- packages/redux-routine/CHANGELOG.md | 4 +++- packages/redux-routine/src/cast-error.js | 14 -------------- packages/redux-routine/src/runtime.js | 6 +----- packages/redux-routine/src/test/cast-error.js | 18 ------------------ packages/redux-routine/src/test/index.js | 2 +- 5 files changed, 5 insertions(+), 39 deletions(-) delete mode 100644 packages/redux-routine/src/cast-error.js delete mode 100644 packages/redux-routine/src/test/cast-error.js diff --git a/packages/redux-routine/CHANGELOG.md b/packages/redux-routine/CHANGELOG.md index b820720f392bc..dbddb8321016b 100644 --- a/packages/redux-routine/CHANGELOG.md +++ b/packages/redux-routine/CHANGELOG.md @@ -2,7 +2,9 @@ ### Bug Fixes -- Fix unhandled promise rejection error caused by returning null from registered generator ([#13314](https://github.com/WordPress/gutenberg/pull/13314) +- Fix unhandled promise rejection error caused by returning null from registered generator ([#13314](https://github.com/WordPress/gutenberg/pull/13314)) +- The middleware will no longer attempt to coerce an error to an instance of `Error`, and instead passes through the thrown value directly. This resolves issues where an `Error` would be thrown when the underlying values were not of type `Error` or `string` (e.g. a thrown object) and the message would end up not being useful (e.g. `[Object object]`). +([#13315](https://github.com/WordPress/gutenberg/pull/13315)) ## 3.0.3 (2018-10-19) diff --git a/packages/redux-routine/src/cast-error.js b/packages/redux-routine/src/cast-error.js deleted file mode 100644 index 9bc2e22a46d40..0000000000000 --- a/packages/redux-routine/src/cast-error.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Casts value as an error if it's not one. - * - * @param {*} error The value to cast. - * - * @return {Error} The cast error. - */ -export default function castError( error ) { - if ( ! ( error instanceof Error ) ) { - error = new Error( error ); - } - - return error; -} diff --git a/packages/redux-routine/src/runtime.js b/packages/redux-routine/src/runtime.js index 484d65ac6c035..d3de265c3f2b1 100644 --- a/packages/redux-routine/src/runtime.js +++ b/packages/redux-routine/src/runtime.js @@ -8,7 +8,6 @@ import isPromise from 'is-promise'; /** * Internal dependencies */ -import castError from './cast-error'; import { isActionOfType, isAction } from './is-action'; /** @@ -27,10 +26,7 @@ export default function createRuntime( controls = {}, dispatch ) { const routine = control( value ); if ( isPromise( routine ) ) { // Async control routine awaits resolution. - routine.then( - yieldNext, - ( error ) => yieldError( castError( error ) ), - ); + routine.then( yieldNext, yieldError ); } else { next( routine ); } diff --git a/packages/redux-routine/src/test/cast-error.js b/packages/redux-routine/src/test/cast-error.js deleted file mode 100644 index bdca1c7c202b9..0000000000000 --- a/packages/redux-routine/src/test/cast-error.js +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Internal dependencies - */ -import castError from '../cast-error'; - -describe( 'castError', () => { - it( 'should return error verbatim', () => { - const error = new Error( 'Foo' ); - - expect( castError( error ) ).toBe( error ); - } ); - - it( 'should return string as message of error', () => { - const error = 'Foo'; - - expect( castError( error ) ).toEqual( new Error( 'Foo' ) ); - } ); -} ); diff --git a/packages/redux-routine/src/test/index.js b/packages/redux-routine/src/test/index.js index 98aedf7bbef47..79a35673f70a5 100644 --- a/packages/redux-routine/src/test/index.js +++ b/packages/redux-routine/src/test/index.js @@ -61,7 +61,7 @@ describe( 'createMiddleware', () => { try { yield { type: 'WAIT_FAIL' }; } catch ( error ) { - expect( error.message ).toBe( 'Message' ); + expect( error ).toBe( 'Message' ); } }