angular-cn/modules/angular2/src/core/exception_handler.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2015-05-20 12:48:15 -04:00
import {Injectable} from 'angular2/di';
import {isPresent, print} from 'angular2/src/facade/lang';
import {ListWrapper, isListLikeIterable} from 'angular2/src/facade/collection';
import {DOM} from 'angular2/src/dom/dom_adapter';
/**
* 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,
* write a custom exception handler that replaces this default as appropriate for your app.
*
* # Example
*
* ```javascript
* @Component({
* selector: 'my-app',
* viewInjector: [
* bind(ExceptionHandler).toClass(MyExceptionHandler)
* ]
* })
* @View(...)
* class MyApp { ... }
*
*
* class MyExceptionHandler implements ExceptionHandler {
* call(error, stackTrace = null, reason = null) {
* // do something with the exception
* }
* }
*
* ```
*/
@Injectable()
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;
var reasonStr = isPresent(reason) ? `\n${reason}` : '';
DOM.logError(`${error}${reasonStr}\nSTACKTRACE:\n${longStackTrace}`);
}
}