2015-05-20 12:48:15 -04:00
|
|
|
import {Injectable} from 'angular2/di';
|
2015-07-22 20:13:42 -04:00
|
|
|
import {isPresent, print, BaseException} from 'angular2/src/facade/lang';
|
2015-02-16 08:35:27 -05:00
|
|
|
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
|
|
|
|
*
|
|
|
|
* class MyExceptionHandler implements ExceptionHandler {
|
|
|
|
* call(error, stackTrace = null, reason = null) {
|
|
|
|
* // do something with the exception
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2015-07-22 20:13:42 -04:00
|
|
|
* bootstrap(MyApp, [bind(ExceptionHandler).toClass(MyExceptionHandler)])
|
|
|
|
*
|
2015-04-14 00:00:52 -04:00
|
|
|
* ```
|
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 {
|
2015-07-22 20:13:42 -04:00
|
|
|
logError: Function = DOM.logError;
|
|
|
|
|
|
|
|
call(exception: Object, stackTrace: string | string[] = null, reason: string = null) {
|
|
|
|
var longStackTrace = isListLikeIterable(stackTrace) ?
|
|
|
|
(<any>stackTrace).join("\n\n-----async gap-----\n") :
|
|
|
|
stackTrace;
|
|
|
|
|
|
|
|
this.logError(`${exception}\n\n${longStackTrace}`);
|
|
|
|
|
|
|
|
if (isPresent(reason)) {
|
|
|
|
this.logError(`Reason: ${reason}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
var context = this._findContext(exception);
|
|
|
|
if (isPresent(context)) {
|
|
|
|
this.logError("Error Context:");
|
|
|
|
this.logError(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
_findContext(exception: any): any {
|
|
|
|
if (!(exception instanceof BaseException)) return null;
|
|
|
|
return isPresent(exception.context) ? exception.context :
|
|
|
|
this._findContext(exception.originalException);
|
2015-02-16 08:35:27 -05:00
|
|
|
}
|
|
|
|
}
|