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

[BUGFIX release] mark handled errors and dont reraise #12549

Closed
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
39 changes: 37 additions & 2 deletions packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
stashParamNames,
calculateCacheKey
} from 'ember-routing/utils';
import { guidFor } from 'ember-metal/utils';
import RouterState from './router_state';
import { getOwner } from 'container/owner';

Expand All @@ -33,6 +34,7 @@ function K() { return this; }

var slice = [].slice;


/**
The `Ember.Router` class manages the application state and URLs. Refer to
the [routing guide](http://emberjs.com/guides/routing/) for documentation.
Expand Down Expand Up @@ -283,8 +285,7 @@ var EmberRouter = EmberObject.extend(Evented, {

_doURLTransition(routerJsMethod, url) {
var transition = this.router[routerJsMethod](url || '/');
didBeginTransition(transition, this);
return transition;
return didBeginTransition(transition, this);
},

transitionTo(...args) {
Expand Down Expand Up @@ -708,6 +709,24 @@ var EmberRouter = EmberObject.extend(Evented, {
run.cancel(this._slowTransitionTimer);
}
this._slowTransitionTimer = null;
},

_handledErrors: computed(function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go down this path, it seems simpler to just brand the given error with a hasBeenHandled symbol. No need to keep these data-structures around.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error[hasBeenHandledSymbol] = true

although this wont work with primitive values, i think thats fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly branding the error seems ok to me (it really isn't a big change for this PR as the basic infrastructure to brand is done), but I believe that we have a few tests in Ember itself that reject("asdf") or throw "asdf" which will need to be changed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first attempt, but the problem I hit was that there's no insurance that the error isn't a primitive type, and there are several tests where it IS a primitive type. This wouldn't be a difficult change (to fire an assertion of the type isn't an object, and update the tests) but it would probably be a problem for a lot of apps. It would need to be noted/documented/blogged about or something.

return {};
}),

// These three helper functions are used to ensure errors aren't
// re-raised if they're handled in a route's error action.
_markErrorAsHandled(error) {
this.get('_handledErrors')[error] = true;
},

_isErrorHandled(error) {
return this.get('_handledErrors')[error];
},

_clearHandledError(error) {
delete this.get('_handledErrors')[error];
}
});

Expand Down Expand Up @@ -849,6 +868,7 @@ function routeHasBeenDefined(router, name) {
(owner.hasRegistration(`template:${name}`) || owner.hasRegistration(`route:${name}`));
}


function triggerEvent(handlerInfos, ignoreFailure, args) {
var name = args.shift();

Expand All @@ -868,6 +888,11 @@ function triggerEvent(handlerInfos, ignoreFailure, args) {
if (handler.actions[name].apply(handler, args) === true) {
eventWasHandled = true;
} else {
// Should only hit here if a non-bubbling error action is triggered on a route.
if (name === 'error') {
var errorId = guidFor(args[0]);
handler.router._markErrorAsHandled(errorId);
}
return;
}
}
Expand Down Expand Up @@ -1031,6 +1056,16 @@ function didBeginTransition(transition, router) {
router.set('currentState', routerState);
}
router.set('targetState', routerState);

return transition.catch(function(error) {
var errorId = guidFor(error);

if (router._isErrorHandled(errorId)) {
router._clearHandledError(errorId);
} else {
throw error;
}
});
}

function resemblesURL(str) {
Expand Down
2 changes: 2 additions & 0 deletions packages/ember/tests/routing/basic_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ QUnit.test('The Special page returning an error invokes SpecialRoute\'s error ha
actions: {
error(reason) {
equal(reason, 'Setup error', 'SpecialRoute#error received the error thrown from setup');
return true;
}
}
});
Expand Down Expand Up @@ -1059,6 +1060,7 @@ function testOverridableErrorHandler(handlersName) {
attrs[handlersName] = {
error(reason) {
equal(reason, 'Setup error', 'error was correctly passed to custom ApplicationRoute handler');
return true;
}
};

Expand Down
176 changes: 176 additions & 0 deletions packages/ember/tests/routing/substates_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,182 @@ QUnit.test('Default error event moves into nested route', function() {
equal(appController.get('currentPath'), 'grandma.error', 'Initial route fully loaded');
});

QUnit.test('Error events that aren\'t bubbled don\t throw application assertions', function() {
expect(2);

templates['grandma'] = 'GRANDMA {{outlet}}';

Router.map(function() {
this.route('grandma', function() {
this.route('mom', { resetNamespace: true }, function() {
this.route('sally');
});
});
});

App.ApplicationController = Ember.Controller.extend();

App.MomSallyRoute = Ember.Route.extend({
model() {
step(1, 'MomSallyRoute#model');

return Ember.RSVP.reject({
msg: 'did it broke?'
});
},
actions: {
error(err) {
equal(err.msg, 'did it broke?');
return false;
}
}
});

bootApplication('/grandma/mom/sally');
});

QUnit.test('Non-bubbled errors that re-throw aren\'t swallowed', function() {
expect(2);

templates['grandma'] = 'GRANDMA {{outlet}}';

Router.map(function() {
this.route('grandma', function() {
this.route('mom', { resetNamespace: true }, function() {
this.route('sally');
});
});
});

App.ApplicationController = Ember.Controller.extend();

App.MomSallyRoute = Ember.Route.extend({
model() {
step(1, 'MomSallyRoute#model');

return Ember.RSVP.reject({
msg: 'did it broke?'
});
},
actions: {
error(err) {
// returns undefined which is falsey
throw err;
}
}
});

throws(function() {
bootApplication('/grandma/mom/sally');
}, function(err) { return err.msg === 'did it broke?';});
});

QUnit.test('Handled errors that re-throw aren\'t swallowed', function() {
expect(4);

var handledError;

templates['grandma'] = 'GRANDMA {{outlet}}';

Router.map(function() {
this.route('grandma', function() {
this.route('mom', { resetNamespace: true }, function() {
this.route('sally');
this.route('this-route-throws');
});
});
});

App.ApplicationController = Ember.Controller.extend();

App.MomSallyRoute = Ember.Route.extend({
model() {
step(1, 'MomSallyRoute#model');

return Ember.RSVP.reject({
msg: 'did it broke?'
});
},
actions: {
error(err) {
step(2, 'MomSallyRoute#error');

handledError = err;

this.transitionTo('mom.this-route-throws');

// Marks error as handled
return false;
}
}
});

App.MomThisRouteThrowsRoute = Ember.Route.extend({
model() {
step(3, 'MomThisRouteThrows#model');

throw handledError;
}
});

throws(function() {
bootApplication('/grandma/mom/sally');
}, function(err) { return err.msg === 'did it broke?'; });
});

QUnit.test('Handled errors that are thrown through rejection aren\'t swallowed', function() {
expect(4);

var handledError;

templates['grandma'] = 'GRANDMA {{outlet}}';

Router.map(function() {
this.route('grandma', function() {
this.route('mom', { resetNamespace: true }, function() {
this.route('sally');
this.route('this-route-throws');
});
});
});

App.ApplicationController = Ember.Controller.extend();

App.MomSallyRoute = Ember.Route.extend({
model() {
step(1, 'MomSallyRoute#model');

return Ember.RSVP.reject({
msg: 'did it broke?'
});
},
actions: {
error(err) {
step(2, 'MomSallyRoute#error');

handledError = err;

this.transitionTo('mom.this-route-throws');

// Marks error as handled
return false;
}
}
});

App.MomThisRouteThrowsRoute = Ember.Route.extend({
model() {
step(3, 'MomThisRouteThrows#model');

return Ember.RSVP.reject(handledError);
}
});

throws(function() {
bootApplication('/grandma/mom/sally');
}, function(err) { return err.msg === 'did it broke?'; });
});

QUnit.test('Setting a query param during a slow transition should work', function() {
var deferred = RSVP.defer();

Expand Down