Skip to content

Commit

Permalink
fix(defaultErrorHandler): Reduce console.error noise when redirected
Browse files Browse the repository at this point in the history
feature(hooks): Pass ignored (synchronous success/error) hook exceptions to the `defaultErrorHandler`
test(*): Silence error logging in unit tests

Partially addresses #2860
  • Loading branch information
christopherthielen committed Aug 11, 2016
1 parent fef9640 commit 8c0344f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/state/stateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ export class StateService {
console.error($error$);
console.error($error$.stack);
} else if ($error$ instanceof Rejection) {
console.error($error$);
console.error($error$.toString());
if ($error$.detail && $error$.detail.stack)
console.error($error$.detail.stack);
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/transition/rejectFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Rejection {

/** Returns a TransitionRejection due to transition superseded */
static superseded(detail?: any, options?: any) {
let message = "The transition has been superseded by a different transition (see detail).";
let message = "The transition has been superseded by a different transition";
let rejection = new Rejection(RejectType.SUPERSEDED, message, detail);
if (options && options.redirected) {
rejection.redirected = true;
Expand All @@ -52,27 +52,27 @@ export class Rejection {

/** Returns a TransitionRejection due to invalid transition */
static invalid(detail?: any) {
let message = "This transition is invalid (see detail)";
let message = "This transition is invalid";
return new Rejection(RejectType.INVALID, message, detail);
}

/** Returns a TransitionRejection due to ignored transition */
static ignored(detail?: any) {
let message = "The transition was ignored.";
let message = "The transition was ignored";
return new Rejection(RejectType.IGNORED, message, detail);
}

/** Returns a TransitionRejection due to aborted transition */
static aborted(detail?: any) {
// TODO think about how to encapsulate an Error() object
let message = "The transition has been aborted.";
let message = "The transition has been aborted";
return new Rejection(RejectType.ABORTED, message, detail);
}

/** Returns a TransitionRejection due to aborted transition */
static errored(detail?: any) {
// TODO think about how to encapsulate an Error() object
let message = "The transition errored.";
let message = "The transition errored";
return new Rejection(RejectType.ERROR, message, detail);
}
}
6 changes: 4 additions & 2 deletions src/transition/transitionHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ export class TransitionHook {
static runSynchronousHooks(hooks: TransitionHook[], swallowExceptions: boolean = false): Promise<any> {
let results: Promise<HookResult>[] = [];
for (let i = 0; i < hooks.length; i++) {
let hook = hooks[i];
try {
results.push(hooks[i].invokeHook());
results.push(hook.invokeHook());
} catch (exception) {
if (!swallowExceptions) {
return Rejection.errored(exception).toPromise();
}

console.error("Swallowed exception during synchronous hook handler: " + exception); // TODO: What to do here?
let errorHandler = hook.transition.router.stateService.defaultErrorHandler();
errorHandler(exception);
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/ng1/stateEventsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ describe('UI-Router v0.2.x $state events', function () {
it('aborts pending transitions even when going back to the current state', inject(function ($state, $q) {
initStateTo(A);
logEvents = true;
$state.defaultErrorHandler(function() {});

var superseded = $state.transitionTo(B, {});
$state.transitionTo(A, {});
Expand All @@ -284,6 +285,7 @@ describe('UI-Router v0.2.x $state events', function () {
it('aborts pending transitions (last call wins)', inject(function ($state, $q) {
initStateTo(A);
logEvents = true;
$state.defaultErrorHandler(function() {});

var superseded = $state.transitionTo(B, {});
$state.transitionTo(C, {});
Expand Down
2 changes: 1 addition & 1 deletion test/ng1/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,7 @@ describe('otherwise and state redirects', function() {
}));

it("should not go into an infinite loop", inject(function($location, $rootScope, $state, $urlRouter, $httpBackend) {
$state.defaultErrorHandler(function() {});
$httpBackend.expectGET("login.html").respond("login page");
$location.url("notmatched");
$urlRouter.update(true);
Expand Down Expand Up @@ -1987,7 +1988,6 @@ describe('transition hook', function() {

$transitions.onStart({ to: 'home' }, function($transition$) {
if (!$transition$.options().reload && count++ < 5) {
console.log("forcing re-enter (reload) of home state ");
var options = $transition$.options();
return $state.target($transition$.to(), $transition$.params("to"), extend({}, options, {reload: true}));
}
Expand Down
4 changes: 4 additions & 0 deletions test/ng1/transitionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ describe('transition', function () {
});

describe('.onSuccess()', function() {
beforeEach(inject($uiRouter => {
$uiRouter.stateService.defaultErrorHandler(function() {})
}));

it('should only be called if the transition succeeds', inject(function($transitions, $q) {
transitionProvider.onSuccess({ from: "*", to: "*" }, function(trans) { states.push(trans.to().name); });
transitionProvider.onEnter({ from: "A", entering: "C" }, function() { return false; });
Expand Down
3 changes: 2 additions & 1 deletion test/ng1/viewHookSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ describe("view hooks", () => {
expect($state.current.name).toBe('baz');
});

it("can cancel the transition by returning a rejected promise", inject(($q) => {
it("can cancel the transition by returning a rejected promise", inject(($q, $state) => {
ctrl.prototype.uiCanExit = function() { log += "canexit;"; return $q.reject('nope'); };
initial();

$state.defaultErrorHandler(function() {});
$state.go('bar'); $q.flush(); $timeout.flush();
expect(log).toBe('canexit;');
expect($state.current.name).toBe('foo');
Expand Down

0 comments on commit 8c0344f

Please sign in to comment.