feat(exception_handler): change ExceptionHandler to output context

This commit is contained in:
vsavkin
2015-07-22 17:13:42 -07:00
parent bd65b63c65
commit fdf226ab69
2 changed files with 119 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import {Injectable} from 'angular2/di';
import {isPresent, print} from 'angular2/src/facade/lang';
import {isPresent, print, BaseException} from 'angular2/src/facade/lang';
import {ListWrapper, isListLikeIterable} from 'angular2/src/facade/collection';
import {DOM} from 'angular2/src/dom/dom_adapter';
@ -13,15 +13,6 @@ import {DOM} from 'angular2/src/dom/dom_adapter';
* # Example
*
* ```javascript
* @Component({
* selector: 'my-app',
* viewInjector: [
* bind(ExceptionHandler).toClass(MyExceptionHandler)
* ]
* })
* @View(...)
* class MyApp { ... }
*
*
* class MyExceptionHandler implements ExceptionHandler {
* call(error, stackTrace = null, reason = null) {
@ -29,14 +20,37 @@ import {DOM} from 'angular2/src/dom/dom_adapter';
* }
* }
*
* bootstrap(MyApp, [bind(ExceptionHandler).toClass(MyExceptionHandler)])
*
* ```
*/
@Injectable()
export class ExceptionHandler {
call(error: Object, stackTrace: string | List<string> = null, reason: string = null) {
var longStackTrace =
isListLikeIterable(stackTrace) ? ListWrapper.join(<any>stackTrace, "\n\n") : stackTrace;
var reasonStr = isPresent(reason) ? `\n${reason}` : '';
DOM.logError(`${error}${reasonStr}\nSTACKTRACE:\n${longStackTrace}`);
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);
}
}