Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable default catch handler #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ Reflux.use(RefluxPromise(Q.Promise));
// Uses bluebird
import bluebird from "bluebird";
Reflux.use(RefluxPromise(bluebird))

// Catching and logging unhandled exceptions in action handlers
import bluebird from "bluebird";
Reflux.use(RefluxPromise(bluebird, function(err) {
console.log(err);
}))
```

### Extensions to Asynchronous actions
Expand Down
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function createFunctions(Reflux, PromiseFactory) {
function createFunctions(Reflux, PromiseFactory, catchHandler) {

const _ = Reflux.utils;

Expand Down Expand Up @@ -59,9 +59,10 @@ function createFunctions(Reflux, PromiseFactory) {
}
});

// Ensure that the promise does trigger "Uncaught (in promise)" errors in console if no error handler is added
// See: https://github.com/reflux/reflux-promise/issues/4
createdPromise.catch(function() {});
// Attach promise catch handler if provided
if (typeof (catchHandler) === "function") {
createdPromise.catch(catchHandler);
}

return createdPromise;
}
Expand Down Expand Up @@ -129,9 +130,9 @@ function createFunctions(Reflux, PromiseFactory) {
/**
* Sets up reflux with Promise functionality
*/
export default function(promiseFactory) {
export default function(promiseFactory, catchHandler) {
return function(Reflux) {
const { triggerPromise, promise, listenAndPromise } = createFunctions(Reflux, promiseFactory);
const { triggerPromise, promise, listenAndPromise } = createFunctions(Reflux, promiseFactory, catchHandler);
Reflux.PublisherMethods.triggerAsync = triggerPromise;
Reflux.PublisherMethods.promise = promise;
Reflux.PublisherMethods.listenAndPromise = listenAndPromise;
Expand Down
39 changes: 39 additions & 0 deletions test/creatingActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,42 @@ describe('Creating actions using promises', function() {
});

});

describe('Promise catch error handler', function () {
var actions, actionNames, errPromiseResolve;

var errPromise = Q.Promise(function(resolve, reject) {
// errPromise resolve function is exposed
errPromiseResolve = resolve;
});

// when errorHandler is called,
// errPromise gets resolved with the error object
var errorHandler = function(err) {
errPromiseResolve(err);
};

Reflux.use(RefluxPromise(Q.Promise, errorHandler));

actionNames = { 'foo': { asyncResult: true }};
actions = Reflux.createActions(actionNames);

it('should be called with error when action promise throws', function() {
var actionError = new Error('action error');
var actionPromise = Q.Promise(function(resolve, reject) {
throw actionError;
resolve('bar');
});

actions.foo.listenAndPromise(function() {
return actionPromise;
});

actions.foo();

return Q.all([
assert.isRejected(actionPromise, actionError, "actionPromsie not rejected with error"),
assert.becomes(errPromise, actionError, "error handler not executed with thrown error"),
]);
});
});