Skip to content

Commit 1dcba89

Browse files
BC-BREAK: Moved defaultErrorHandler from TransitionService to StateService
1 parent 558fc80 commit 1dcba89

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

src/state/stateService.ts

+38
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,13 @@ export class StateService {
299299

300300
if (error.type === RejectType.ABORTED) {
301301
router.urlRouter.update();
302+
return services.$q.reject(error);
302303
}
303304
}
304305

306+
var errorHandler = this.defaultErrorHandler();
307+
errorHandler(error);
308+
305309
return services.$q.reject(error);
306310
};
307311

@@ -474,6 +478,40 @@ export class StateService {
474478
});
475479
};
476480

481+
/** @hidden */
482+
private _defaultErrorHandler: ((_error) => void) = function $defaultErrorHandler($error$) {
483+
if ($error$ instanceof Error && $error$.stack) {
484+
console.error($error$.stack);
485+
} else {
486+
console.error($error$);
487+
}
488+
};
489+
490+
/**
491+
* Sets or gets the default [[transitionTo]] error handler.
492+
*
493+
* The error handler is called when a [[Transition]] is rejected or when any error occurred during the Transition.
494+
* This includes errors caused by resolves and transition hooks.
495+
*
496+
* The built-in default error handler logs the error to the console.
497+
*
498+
* You can provide your own custom handler.
499+
*
500+
* @example
501+
* ```js
502+
*
503+
* stateService.defaultErrorHandler(function() {
504+
* // Do not log transitionTo errors
505+
* });
506+
* ```
507+
*
508+
* @param handler a global error handler function
509+
* @returns the current global error handler
510+
*/
511+
defaultErrorHandler(handler?: (error) => void): (error) => void {
512+
return this._defaultErrorHandler = handler || this._defaultErrorHandler;
513+
}
514+
477515
/**
478516
* @ngdoc function
479517
* @name ui.router.state.$state#get

src/transition/transitionService.ts

-22
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,6 @@ export class TransitionService implements IHookRegistry {
118118
/** @hidden */
119119
getHooks : (hookName: string) => IEventHook[];
120120

121-
/** @hidden */
122-
private _defaultErrorHandler: ((_error) => void) = function $defaultErrorHandler($error$) {
123-
if ($error$ instanceof Error) {
124-
console.error($error$);
125-
}
126-
};
127-
128-
/**
129-
* Sets or gets the default transition error handler.
130-
*
131-
* The error handler is called when a [[Transition]] is rejected and when any error occurred during the Transition.
132-
* This includes errors caused by resolves and transition hooks.
133-
*
134-
* The built-in default error handler logs thrown javascript Errors to the console.
135-
*
136-
* @param handler a global error handler function
137-
* @returns the current global error handler
138-
*/
139-
defaultErrorHandler(handler?: (error) => void): (error) => void {
140-
return this._defaultErrorHandler = handler || this._defaultErrorHandler;
141-
}
142-
143121
/**
144122
* Creates a new [[Transition]] object
145123
*

test/ng1/stateSpec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ describe('state', function () {
1010

1111
var $injector, $stateProvider, locationProvider, templateParams, template, ctrlName, errors;
1212

13-
beforeEach(module('ui.router', function($locationProvider, $transitionsProvider) {
13+
beforeEach(module('ui.router', function($locationProvider) {
1414
errors = [];
1515
locationProvider = $locationProvider;
1616
$locationProvider.html5Mode(false);
17-
$transitionsProvider.defaultErrorHandler(function(error) { errors.push(error); } );
1817
}));
1918

2019
var log, logEvents, logEnterExit;
@@ -169,7 +168,8 @@ describe('state', function () {
169168
$provide.value('AppInjectable', AppInjectable);
170169
}));
171170

172-
beforeEach(inject(function (_$injector_) {
171+
beforeEach(inject(function (_$injector_, $state) {
172+
$state.defaultErrorHandler(function(error) { errors.push(error); } );
173173
$injector = _$injector_;
174174
log = '';
175175
logEvents = logEnterExit = false;
@@ -1855,7 +1855,7 @@ describe('exceptions in onEnter', function() {
18551855
// Test for #2772
18561856
it('trigger transition.onError', inject(function ($state, $q, $transitions) {
18571857
var called;
1858-
$transitions.defaultErrorHandler(function() { });
1858+
$state.defaultErrorHandler(function() { });
18591859
$transitions.onError({}, function() {
18601860
called = true;
18611861
});

0 commit comments

Comments
 (0)