2015-05-20 12:48:15 -04:00
|
|
|
import {Injectable} from 'angular2/di';
|
2015-02-16 08:35:27 -05:00
|
|
|
import {isPresent, print} from 'angular2/src/facade/lang';
|
|
|
|
import {ListWrapper, isListLikeIterable} from 'angular2/src/facade/collection';
|
2015-04-30 14:25:50 -04:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-02-16 08:35:27 -05:00
|
|
|
|
2015-03-31 18:47:11 -04:00
|
|
|
/**
|
2015-04-14 00:00:52 -04:00
|
|
|
* Provides a hook for centralized exception handling.
|
|
|
|
*
|
2015-05-20 12:48:15 -04:00
|
|
|
* The default implementation of `ExceptionHandler` prints error messages to the `Console`. To
|
|
|
|
* intercept error handling,
|
2015-04-14 00:00:52 -04:00
|
|
|
* write a custom exception handler that replaces this default as appropriate for your app.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* @Component({
|
|
|
|
* selector: 'my-app',
|
2015-05-16 18:21:36 -04:00
|
|
|
* appInjector: [
|
2015-04-14 00:00:52 -04:00
|
|
|
* bind(ExceptionHandler).toClass(MyExceptionHandler)
|
|
|
|
* ]
|
|
|
|
* })
|
|
|
|
* @View(...)
|
|
|
|
* class MyApp { ... }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* class MyExceptionHandler implements ExceptionHandler {
|
|
|
|
* call(error, stackTrace = null, reason = null) {
|
|
|
|
* // do something with the exception
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
*
|
2015-04-10 14:15:01 -04:00
|
|
|
* @exportedAs angular2/core
|
2015-03-31 18:47:11 -04:00
|
|
|
*/
|
2015-03-16 17:44:14 -04:00
|
|
|
@Injectable()
|
2015-02-16 08:35:27 -05:00
|
|
|
export class ExceptionHandler {
|
|
|
|
call(error, stackTrace = null, reason = null) {
|
2015-05-20 12:48:15 -04:00
|
|
|
var longStackTrace =
|
|
|
|
isListLikeIterable(stackTrace) ? ListWrapper.join(stackTrace, "\n\n") : stackTrace;
|
2015-02-16 08:35:27 -05:00
|
|
|
var reasonStr = isPresent(reason) ? `\n${reason}` : '';
|
2015-04-30 14:25:50 -04:00
|
|
|
DOM.logError(`${error}${reasonStr}\nSTACKTRACE:\n${longStackTrace}`);
|
2015-02-16 08:35:27 -05:00
|
|
|
}
|
|
|
|
}
|