From 95f2df830b6af834554e63b2445e1c76e96ed055 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Mon, 7 Nov 2016 14:38:14 -0500 Subject: [PATCH] Revert "Improve errors" --- lib/router/router.js | 9 +++---- lib/router/transition-aborted-error.js | 27 --------------------- lib/router/transition.js | 12 ++++++--- lib/router/unrecognized-url-error.js | 26 ++++++-------------- test/tests/router_test.js | 24 +++++++++--------- test/tests/test_helpers.js | 21 +++------------- test/tests/transition-aborted-error_test.js | 20 --------------- 7 files changed, 36 insertions(+), 103 deletions(-) delete mode 100644 lib/router/transition-aborted-error.js delete mode 100644 test/tests/transition-aborted-error_test.js diff --git a/lib/router/router.js b/lib/router/router.js index 2f301059..2ace96fb 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -2,8 +2,7 @@ import RouteRecognizer from 'route-recognizer'; import { Promise } from 'rsvp'; import { trigger, log, slice, forEach, merge, extractQueryParams, getChangelist, promiseLabel, callHook } from './utils'; import TransitionState from './transition-state'; -import { logAbort, Transition } from './transition'; -import TransitionAbortedError from './transition-aborted-error'; +import { logAbort, Transition, TransitionAborted } from './transition'; import NamedTransitionIntent from './transition-intent/named-transition-intent'; import URLTransitionIntent from './transition-intent/url-transition-intent'; @@ -495,7 +494,7 @@ function handlerEnteredOrUpdated(currentHandlerInfos, handlerInfo, enter, transi } if (transition && transition.isAborted) { - throw new TransitionAbortedError(); + throw new TransitionAborted(); } handler.context = context; @@ -503,7 +502,7 @@ function handlerEnteredOrUpdated(currentHandlerInfos, handlerInfo, enter, transi callHook(handler, 'setup', context, transition); if (transition && transition.isAborted) { - throw new TransitionAbortedError(); + throw new TransitionAborted(); } currentHandlerInfos.push(handlerInfo); @@ -695,7 +694,7 @@ function finalizeTransition(transition, newState) { // Resolve with the final handler. return handlerInfos[handlerInfos.length - 1].handler; } catch(e) { - if (!(e instanceof TransitionAbortedError)) { + if (!(e instanceof TransitionAborted)) { //var erroneousHandler = handlerInfos.pop(); var infos = transition.state.handlerInfos; transition.trigger(true, 'error', e, transition, infos[infos.length-1].handler); diff --git a/lib/router/transition-aborted-error.js b/lib/router/transition-aborted-error.js deleted file mode 100644 index 8a0d9dc5..00000000 --- a/lib/router/transition-aborted-error.js +++ /dev/null @@ -1,27 +0,0 @@ -import { oCreate } from './utils'; - -function TransitionAbortedError(message) { - if (!(this instanceof TransitionAbortedError)) { - return new TransitionAbortedError(message); - } - - let error = Error.call(this, message); - - if (Error.captureStackTrace) { - Error.captureStackTrace(this, TransitionAbortedError); - } else { - this.stack = error.stack; - } - - this.description = error.description; - this.fileName = error.fileName; - this.lineNumber = error.lineNumber; - this.message = error.message || 'TransitionAborted'; - this.name = 'TransitionAborted'; - this.number = error.number; - this.code = error.code; -} - -TransitionAbortedError.prototype = oCreate(Error.prototype); - -export default TransitionAbortedError; diff --git a/lib/router/transition.js b/lib/router/transition.js index 7030c620..4b765cb5 100644 --- a/lib/router/transition.js +++ b/lib/router/transition.js @@ -1,6 +1,5 @@ import { Promise } from 'rsvp'; import { trigger, slice, log, promiseLabel } from './utils'; -import TransitionAbortedError from './transition-aborted-error'; /** A Transition is a thennable (a promise-like object) that represents @@ -326,11 +325,16 @@ Transition.prototype.send = Transition.prototype.trigger; /** @private - Logs and returns an instance of TransitionAbortedError. + Logs and returns a TransitionAborted error. */ function logAbort(transition) { log(transition.router, transition.sequence, "detected abort."); - return new TransitionAbortedError(); + return new TransitionAborted(); } -export { Transition, logAbort, TransitionAbortedError as TransitionAborted }; +function TransitionAborted(message) { + this.message = (message || "TransitionAborted"); + this.name = "TransitionAborted"; +} + +export { Transition, logAbort, TransitionAborted }; diff --git a/lib/router/unrecognized-url-error.js b/lib/router/unrecognized-url-error.js index b91c5cac..8e835a85 100644 --- a/lib/router/unrecognized-url-error.js +++ b/lib/router/unrecognized-url-error.js @@ -1,25 +1,13 @@ import { oCreate } from './utils'; +/** + Promise reject reasons passed to promise rejection + handlers for failed transitions. + */ function UnrecognizedURLError(message) { - if (!(this instanceof UnrecognizedURLError)) { - return new UnrecognizedURLError(message); - } - - let error = Error.call(this, message); - - if (Error.captureStackTrace) { - Error.captureStackTrace(this, UnrecognizedURLError); - } else { - this.stack = error.stack; - } - - this.description = error.description; - this.fileName = error.fileName; - this.lineNumber = error.lineNumber; - this.message = error.message || 'UnrecognizedURL'; - this.name = 'UnrecognizedURLError'; - this.number = error.number; - this.code = error.code; + this.message = (message || "UnrecognizedURLError"); + this.name = "UnrecognizedURLError"; + Error.call(this); } UnrecognizedURLError.prototype = oCreate(Error.prototype); diff --git a/test/tests/router_test.js b/test/tests/router_test.js index fcc1d28a..24b1df8b 100644 --- a/test/tests/router_test.js +++ b/test/tests/router_test.js @@ -1,12 +1,4 @@ -import { - module, - test, - flushBackburner, - transitionTo, - transitionToWithAbort, - shouldNotHappen, - assertAbort -} from 'tests/test_helpers'; +import { module, test, flushBackburner, transitionTo, transitionToWithAbort, shouldNotHappen } from "tests/test_helpers"; import Router from "router"; import { reject, Promise } from "rsvp"; @@ -1723,6 +1715,12 @@ test("can redirect from error handler", function(assert) { }); }); +function assertAbort(assert) { + return function _assertAbort(e) { + assert.equal(e.name, "TransitionAborted", "transition was aborted"); + }; +} + test("can redirect from setup/enter", function(assert) { assert.expect(5); @@ -1969,7 +1967,9 @@ test("willTransition function fired with cancellable transition passed in", func transition.abort(); }; - return router.transitionTo('about').then(shouldNotHappen(assert), assertAbort(assert)); + return router.transitionTo('about').then(shouldNotHappen(assert), function(e) { + assert.equal(e.name, 'TransitionAborted', 'reject object is a TransitionAborted'); + }); }); }); @@ -1996,7 +1996,9 @@ test("transitions can be aborted in the willTransition event", function(assert) }; router.handleURL('/index').then(function() { - return router.transitionTo('about').then(shouldNotHappen(assert), assertAbort(assert)); + return router.transitionTo('about').then(shouldNotHappen(assert), function(e) { + assert.equal(e.name, 'TransitionAborted', 'reject object is a TransitionAborted'); + }); }); }); diff --git a/test/tests/test_helpers.js b/test/tests/test_helpers.js index 27a654c7..5304e23c 100644 --- a/test/tests/test_helpers.js +++ b/test/tests/test_helpers.js @@ -1,7 +1,6 @@ import { Backburner } from "backburner"; import { resolve, configure } from "rsvp"; import { oCreate } from 'router/utils'; -import TransitionAbortedError from 'router/transition-aborted-error'; var slice = Array.prototype.slice; @@ -40,11 +39,6 @@ function module(name, options) { }); } -function assertAbort(assert) { - return function _assertAbort(e) { - assert.ok(e instanceof TransitionAbortedError, 'transition was redirected/aborted'); - }; -} // Helper method that performs a transition and flushes // the backburner queue. Helpful for when you want to write @@ -57,7 +51,9 @@ function transitionTo(router) { function transitionToWithAbort(assert, router) { var args = slice.call(arguments, 2); - router.transitionTo.apply(router, args).then(shouldNotHappen, assertAbort(assert)); + router.transitionTo.apply(router, args).then(shouldNotHappen, function(reason) { + assert.equal(reason.name, "TransitionAborted", "transition was redirected/aborted"); + }); flushBackburner(); } @@ -83,13 +79,4 @@ test("backburnerized testing works as expected", function(assert) { }); }); -export { - module, - test, - flushBackburner, - transitionTo, - transitionToWithAbort, - shouldNotHappen, - stubbedHandlerInfoFactory, - assertAbort -}; +export { module, test, flushBackburner, transitionTo, transitionToWithAbort, shouldNotHappen, stubbedHandlerInfoFactory }; diff --git a/test/tests/transition-aborted-error_test.js b/test/tests/transition-aborted-error_test.js deleted file mode 100644 index 38a97f1d..00000000 --- a/test/tests/transition-aborted-error_test.js +++ /dev/null @@ -1,20 +0,0 @@ -import { module, test } from './test_helpers'; -import TransitionAbortedError from 'router/transition-aborted-error'; - -module('transition-aborted-error'); - -test('correct inheritance and name', function(assert) { - var error; - - try { - throw new TransitionAbortedError('Message'); - } catch(e) { - error = e; - } - - // it would be more correct with TransitionAbortedError, but other libraries may rely on this name - assert.equal(error.name, 'TransitionAborted', "TransitionAbortedError has the name 'TransitionAborted'"); - - assert.ok(error instanceof TransitionAbortedError); - assert.ok(error instanceof Error); -});