Skip to content

Commit

Permalink
fix(BaseException): convert JS stack property to getter
Browse files Browse the repository at this point in the history
The stack property of BaseException was previously being set in
the constructor based on the provided message, but subclasses 
of BaseException would change the message after constructing the 
error. This change implements stack as a getter of BaseException,
deferring the creation of the stack until it is asked for.

Fixes angular#2570
  • Loading branch information
jeffbcross committed Jun 18, 2015
1 parent b2c6694 commit 16e83db
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion modules/angular2/src/facade/lang.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class FunctionWrapper {
}

class BaseException extends Error {
final String message;
String message;

BaseException([this.message]);

Expand Down
4 changes: 2 additions & 2 deletions modules/angular2/src/facade/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export type Type = new (...args: any[]) => any;

export class BaseException extends Error {
message;
stack;
constructor(message?: string) {
super(message);
this.message = message;
this.stack = (<any>new Error(message)).stack;
}

toString(): string { return this.message; }

get stack() { return (<any>new Error(this.message)).stack }
}

export var Math = _global.Math;
Expand Down
3 changes: 3 additions & 0 deletions modules/angular2/test/di/exceptions_spec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library angular2.test.di.exceptions_spec;
// Not implemented
main() {}
24 changes: 24 additions & 0 deletions modules/angular2/test/di/exceptions_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
iit,
inject,
it,
xit,
} from 'angular2/test_lib';
import {AbstractBindingError} from 'angular2/di';

export function main() {
describe('exceptions', () => {
it('should have the proper message when thrown', () => {
try {
throw new AbstractBindingError('foo', () => { return 'message'; });
} catch (e) {
expect(e.stack.split('\n')[0]).toBe('Error: message');
}
});
});
}
22 changes: 21 additions & 1 deletion modules/angular2/test/facade/lang_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@ import {
RegExpWrapper,
RegExpMatcherWrapper,
StringWrapper,
CONST_EXPR
CONST_EXPR,
BaseException,
isString
} from 'angular2/src/facade/lang';

export function main() {
describe('BaseException', () => {
it('should include error message in stack, if added after initialization', () => {
try {
var exception = new BaseException();
exception.message = 'Msg';
throw exception;
} catch (e) {
if (isString(e.stack)) {
// In JS, the stack contains the message, so we verify that added message is in the stack
expect(`${e.stack}`.split('\n')[0]).toBe('Error: Msg');
} else {
// In Dart, the stack does not contain the message, so we toString the error directly
expect(e.toString()).toBe('Msg');
}
}
});
});

describe('RegExp', () => {
it('should expose the index for each match', () => {
var re = RegExpWrapper.create('(!)');
Expand Down

0 comments on commit 16e83db

Please sign in to comment.