Skip to content

Commit fdf226a

Browse files
committed
feat(exception_handler): change ExceptionHandler to output context
1 parent bd65b63 commit fdf226a

File tree

2 files changed

+119
-15
lines changed

2 files changed

+119
-15
lines changed
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Injectable} from 'angular2/di';
2-
import {isPresent, print} from 'angular2/src/facade/lang';
2+
import {isPresent, print, BaseException} from 'angular2/src/facade/lang';
33
import {ListWrapper, isListLikeIterable} from 'angular2/src/facade/collection';
44
import {DOM} from 'angular2/src/dom/dom_adapter';
55

@@ -13,30 +13,44 @@ import {DOM} from 'angular2/src/dom/dom_adapter';
1313
* # Example
1414
*
1515
* ```javascript
16-
* @Component({
17-
* selector: 'my-app',
18-
* viewInjector: [
19-
* bind(ExceptionHandler).toClass(MyExceptionHandler)
20-
* ]
21-
* })
22-
* @View(...)
23-
* class MyApp { ... }
24-
*
2516
*
2617
* class MyExceptionHandler implements ExceptionHandler {
2718
* call(error, stackTrace = null, reason = null) {
2819
* // do something with the exception
2920
* }
3021
* }
3122
*
23+
* bootstrap(MyApp, [bind(ExceptionHandler).toClass(MyExceptionHandler)])
24+
*
3225
* ```
3326
*/
3427
@Injectable()
3528
export class ExceptionHandler {
36-
call(error: Object, stackTrace: string | List<string> = null, reason: string = null) {
37-
var longStackTrace =
38-
isListLikeIterable(stackTrace) ? ListWrapper.join(<any>stackTrace, "\n\n") : stackTrace;
39-
var reasonStr = isPresent(reason) ? `\n${reason}` : '';
40-
DOM.logError(`${error}${reasonStr}\nSTACKTRACE:\n${longStackTrace}`);
29+
logError: Function = DOM.logError;
30+
31+
call(exception: Object, stackTrace: string | string[] = null, reason: string = null) {
32+
var longStackTrace = isListLikeIterable(stackTrace) ?
33+
(<any>stackTrace).join("\n\n-----async gap-----\n") :
34+
stackTrace;
35+
36+
this.logError(`${exception}\n\n${longStackTrace}`);
37+
38+
if (isPresent(reason)) {
39+
this.logError(`Reason: ${reason}`);
40+
}
41+
42+
var context = this._findContext(exception);
43+
if (isPresent(context)) {
44+
this.logError("Error Context:");
45+
this.logError(context);
46+
}
47+
48+
throw exception;
49+
}
50+
51+
_findContext(exception: any): any {
52+
if (!(exception instanceof BaseException)) return null;
53+
return isPresent(exception.context) ? exception.context :
54+
this._findContext(exception.originalException);
4155
}
4256
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import {
2+
AsyncTestCompleter,
3+
beforeEach,
4+
ddescribe,
5+
describe,
6+
expect,
7+
iit,
8+
inject,
9+
it,
10+
xdescribe,
11+
xit,
12+
IS_DARTIUM,
13+
Log
14+
} from 'angular2/test_lib';
15+
import {BaseException} from 'angular2/src/facade/lang';
16+
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
17+
18+
class _CustomException {
19+
context = "some context";
20+
}
21+
22+
export function main() {
23+
describe('ExceptionHandler', () => {
24+
var log, handler;
25+
26+
beforeEach(() => {
27+
log = new Log();
28+
handler = new ExceptionHandler();
29+
handler.logError = (e) => log.add(e);
30+
});
31+
32+
it("should output exception", () => {
33+
try {
34+
handler.call(new BaseException("message!"));
35+
} catch (e) {
36+
}
37+
expect(log.result()).toContain("message!");
38+
});
39+
40+
it("should output stackTrace", () => {
41+
try {
42+
handler.call(new BaseException("message!"), "stack!");
43+
} catch (e) {
44+
}
45+
expect(log.result()).toContain("stack!");
46+
});
47+
48+
it("should join a long stackTrace", () => {
49+
try {
50+
handler.call(new BaseException("message!"), ["stack1", "stack2"]);
51+
} catch (e) {
52+
}
53+
expect(log.result()).toContain("stack1");
54+
expect(log.result()).toContain("stack2");
55+
});
56+
57+
it("should output reason when present", () => {
58+
try {
59+
handler.call(new BaseException("message!"), null, "reason!");
60+
} catch (e) {
61+
}
62+
expect(log.result()).toContain("reason!");
63+
});
64+
65+
it("should print context", () => {
66+
try {
67+
handler.call(new BaseException("message!", null, null, "context!"));
68+
} catch (e) {
69+
}
70+
expect(log.result()).toContain("context!");
71+
});
72+
73+
it("should print nested context", () => {
74+
try {
75+
var original = new BaseException("message!", null, null, "context!");
76+
handler.call(new BaseException("message", original));
77+
} catch (e) {
78+
}
79+
expect(log.result()).toContain("context!");
80+
});
81+
82+
it("should not print context when the passed-in exception is not a BaseException", () => {
83+
try {
84+
handler.call(new _CustomException());
85+
} catch (e) {
86+
}
87+
expect(log.result()).not.toContain("context");
88+
});
89+
});
90+
}

0 commit comments

Comments
 (0)