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

42 lines
1.2 KiB
JavaScript
Raw Normal View History

import {Injectable} from 'angular2/di';
import {isPresent, print} from 'angular2/src/facade/lang';
import {ListWrapper, isListLikeIterable} from 'angular2/src/facade/collection';
/**
* Provides a hook for centralized exception handling.
*
* 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',
* injectables: [
* 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
*/
@Injectable()
export class ExceptionHandler {
call(error, stackTrace = null, reason = null) {
var longStackTrace = isListLikeIterable(stackTrace) ? ListWrapper.join(stackTrace, "\n\n") : stackTrace;
var reasonStr = isPresent(reason) ? `\n${reason}` : '';
print(`${error}${reasonStr}\nSTACKTRACE:\n${longStackTrace}`);
}
}