Skip to content

Commit 8c0344f

Browse files
fix(defaultErrorHandler): Reduce console.error noise when redirected
feature(hooks): Pass ignored (synchronous success/error) hook exceptions to the `defaultErrorHandler` test(*): Silence error logging in unit tests Partially addresses #2860
1 parent fef9640 commit 8c0344f

File tree

7 files changed

+19
-10
lines changed

7 files changed

+19
-10
lines changed

src/state/stateService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ export class StateService {
487487
console.error($error$);
488488
console.error($error$.stack);
489489
} else if ($error$ instanceof Rejection) {
490-
console.error($error$);
490+
console.error($error$.toString());
491491
if ($error$.detail && $error$.detail.stack)
492492
console.error($error$.detail.stack);
493493
} else {

src/transition/rejectFactory.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Rejection {
3737

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

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

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

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

7272
/** Returns a TransitionRejection due to aborted transition */
7373
static errored(detail?: any) {
7474
// TODO think about how to encapsulate an Error() object
75-
let message = "The transition errored.";
75+
let message = "The transition errored";
7676
return new Rejection(RejectType.ERROR, message, detail);
7777
}
7878
}

src/transition/transitionHook.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ export class TransitionHook {
8989
static runSynchronousHooks(hooks: TransitionHook[], swallowExceptions: boolean = false): Promise<any> {
9090
let results: Promise<HookResult>[] = [];
9191
for (let i = 0; i < hooks.length; i++) {
92+
let hook = hooks[i];
9293
try {
93-
results.push(hooks[i].invokeHook());
94+
results.push(hook.invokeHook());
9495
} catch (exception) {
9596
if (!swallowExceptions) {
9697
return Rejection.errored(exception).toPromise();
9798
}
9899

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

test/ng1/stateEventsSpec.js

+2
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ describe('UI-Router v0.2.x $state events', function () {
272272
it('aborts pending transitions even when going back to the current state', inject(function ($state, $q) {
273273
initStateTo(A);
274274
logEvents = true;
275+
$state.defaultErrorHandler(function() {});
275276

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

288290
var superseded = $state.transitionTo(B, {});
289291
$state.transitionTo(C, {});

test/ng1/stateSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,7 @@ describe('otherwise and state redirects', function() {
19091909
}));
19101910

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

19881989
$transitions.onStart({ to: 'home' }, function($transition$) {
19891990
if (!$transition$.options().reload && count++ < 5) {
1990-
console.log("forcing re-enter (reload) of home state ");
19911991
var options = $transition$.options();
19921992
return $state.target($transition$.to(), $transition$.params("to"), extend({}, options, {reload: true}));
19931993
}

test/ng1/transitionSpec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ describe('transition', function () {
283283
});
284284

285285
describe('.onSuccess()', function() {
286+
beforeEach(inject($uiRouter => {
287+
$uiRouter.stateService.defaultErrorHandler(function() {})
288+
}));
289+
286290
it('should only be called if the transition succeeds', inject(function($transitions, $q) {
287291
transitionProvider.onSuccess({ from: "*", to: "*" }, function(trans) { states.push(trans.to().name); });
288292
transitionProvider.onEnter({ from: "A", entering: "C" }, function() { return false; });

test/ng1/viewHookSpec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ describe("view hooks", () => {
101101
expect($state.current.name).toBe('baz');
102102
});
103103

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

108+
$state.defaultErrorHandler(function() {});
108109
$state.go('bar'); $q.flush(); $timeout.flush();
109110
expect(log).toBe('canexit;');
110111
expect($state.current.name).toBe('foo');

0 commit comments

Comments
 (0)