Skip to content

Commit

Permalink
fix: invoke error callbacks in places where it is not called
Browse files Browse the repository at this point in the history
Fixes #22
  • Loading branch information
troch committed Sep 4, 2015
1 parent 66ae677 commit 92f9719
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
11 changes: 7 additions & 4 deletions modules/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ class Router5 {
let opts = this.options

// callback
let cb = (err, state) => {
let cb = (err, state, invokeErrCb = true) => {
browser.addPopstateListener(this.boundOnPopState)
if (done) done(err, state)
if (err && invokeErrCb) this._invokeListeners('$error', state, null, err)
}

// Get start path
Expand All @@ -164,7 +165,7 @@ class Router5 {
// If no supplied start state, get start state
startState = this.matchPath(startPath)
// Navigate to default function
let navigateToDefault = () => this.navigate(opts.defaultRoute, opts.defaultParams, {replace: true}, cb)
let navigateToDefault = () => this.navigate(opts.defaultRoute, opts.defaultParams, {replace: true}, (err, state) => cb(err, state, false))
// If matched start path
if (startState) {
this.lastStateAttempt = startState
Expand All @@ -174,14 +175,14 @@ class Router5 {
cb(null, state)
}
else if (opts.defaultRoute) navigateToDefault()
else cb(err)
else cb(err, null, false)
})
} else if (opts.defaultRoute) {
// If default, navigate to default
navigateToDefault()
} else {
// No start match, no default => do nothing
cb(null)
cb(constants.ROUTE_NOT_FOUND, null)
}
} else {
// Initialise router with provided start state
Expand Down Expand Up @@ -561,6 +562,7 @@ class Router5 {

if (!path) {
if (done) done(constants.ROUTE_NOT_FOUND)
this._invokeListeners('$error', toState, fromState, constants.ROUTE_NOT_FOUND)
return
}

Expand All @@ -572,6 +574,7 @@ class Router5 {
// (no desactivation and no callbacks)
if (sameStates && !opts.reload) {
if (done) done(constants.SAME_STATES)
this._invokeListeners('$error', toState, fromState, constants.SAME_STATES)
return
}

Expand Down
8 changes: 4 additions & 4 deletions tests/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ function testRouter(useHash) {
});
});

it('should start with no error if no matched start state and no default route', function (done) {
it('should start with a not found error if no matched start state and no default route', function (done) {
router.stop();
router.setOption('defaultRoute', null);
window.history.replaceState({}, '', base + getExpectedPath(useHash, ''));
router.start(function (err) {
expect(err).toBe(null);
expect(err).toBe(Router5.ERR.ROUTE_NOT_FOUND);
done();
});
});
Expand All @@ -150,8 +150,8 @@ function testRouter(useHash) {
router.stop();
window.history.replaceState({}, '', base + getExpectedPath(useHash, '/users/list/'));
router.start(function (err, state) {
expect(err).toBe(null);
expect(state).toBe(undefined);
expect(err).toBe(Router5.ERR.ROUTE_NOT_FOUND);
expect(state).toBe(null);
done();
});
});
Expand Down

0 comments on commit 92f9719

Please sign in to comment.