Navigation Menu

Skip to content

Commit

Permalink
fix error swallowing for cmd.run when effect has an error
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesjtong authored and bdwain committed Aug 26, 2018
1 parent b767df7 commit a0e7721
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/cmd.js
Expand Up @@ -32,14 +32,15 @@ function handleRunCmd(cmd, dispatch, getState){
let result = cmd.func(...getMappedCmdArgs(cmd.args, dispatch, getState))

if (isPromiseLike(result) && !cmd.forceSync){
return result.then(onSuccess, onFail).then(action => {
return action ? [action] : [];
return result.then( onSuccess, (error) => {
console.error(error);
return onFail(error);
})
.then(action => action ? [action] : [])
}
let resultAction = onSuccess(result);
return resultAction ? Promise.resolve([resultAction]) : null;
}
catch(err){
} catch(err){
if(!cmd.failActionCreator){
console.error(err);
throw err //don't swallow errors if they are not handling them
Expand Down
18 changes: 18 additions & 0 deletions test/cmd.spec.js
Expand Up @@ -45,11 +45,14 @@ describe('Cmds', () => {
});

it('resolves with an empty array if the function returns a rejected promise', async () => {
let consoleErr = jest.spyOn(console, 'error').mockImplementation(() => {});
sideEffect.mockReturnValueOnce(Promise.reject(123));
let cmd = Cmd.run(sideEffect);
let result = executeCmd(cmd, dispatch, getState);
expect(sideEffect.mock.calls.length).toBe(1);
await expect(result).resolves.toEqual([]);
expect(consoleErr).toHaveBeenCalled();
consoleErr.mockRestore();
});

it('rethrows the thrown error if the function throws', function(){
Expand All @@ -58,6 +61,15 @@ describe('Cmds', () => {
sideEffect.mockImplementationOnce(() => {throw err});
let cmd = Cmd.run(sideEffect);
expect(() => executeCmd(cmd, dispatch, getState)).toThrow(err);
consoleErr.mockRestore();
});

it('calls console.error if the sideEffect has an error instead of swallowing the error completely', async function(){
let consoleErr = jest.spyOn(console, 'error').mockImplementation(() => {});
let err = new Error("foo")
sideEffect.mockReturnValueOnce(Promise.reject(err));
let cmd = Cmd.run(sideEffect);
await executeCmd(cmd, dispatch, getState)
expect(consoleErr).toHaveBeenCalledWith(err);
consoleErr.mockRestore();
});
Expand Down Expand Up @@ -131,6 +143,7 @@ describe('Cmds', () => {
});

it('runs the rejection value (for promises) through the fail handler and resolves with it in an array', async () => {
let consoleErr = jest.spyOn(console, 'error').mockImplementation(() => {});
sideEffect.mockReturnValueOnce(Promise.reject(123));
let cmd = Cmd.run(sideEffect, {
successActionCreator: actionCreator1,
Expand All @@ -139,6 +152,7 @@ describe('Cmds', () => {

let result = executeCmd(cmd, dispatch, getState);
await expect(result).resolves.toEqual([actionCreator2(123)]);
consoleErr.mockRestore();
});
});
});
Expand All @@ -153,12 +167,16 @@ describe('Cmds', () => {
});

describe('Cmd.list', () => {
let error;

beforeEach(() => {
jest.useFakeTimers();
error = jest.spyOn(console, 'error').mockImplementation(() => {});
});

afterEach(() => {
jest.useRealTimers();
error.mockRestore();
});

describe('when sequence is false', () => {
Expand Down

0 comments on commit a0e7721

Please sign in to comment.