feat(change_detector): wrap exceptions into ChangeDetectionError
ChangeDetectionError references the original error and the location where the error happened
This commit is contained in:
parent
3d534928b5
commit
d642c6afb5
|
@ -19,6 +19,22 @@ class ExpressionChangedAfterItHasBeenChecked extends Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class ChangeDetectionError extends Error {
|
||||||
|
message:string;
|
||||||
|
originalException:any;
|
||||||
|
location:string;
|
||||||
|
|
||||||
|
constructor(record:Record, originalException:any) {
|
||||||
|
this.originalException = originalException;
|
||||||
|
this.location = record.protoRecord.expressionAsString;
|
||||||
|
this.message = `${this.originalException} in [${this.location}]`;
|
||||||
|
}
|
||||||
|
|
||||||
|
toString():string {
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class ChangeDetector {
|
export class ChangeDetector {
|
||||||
_rootRecordRange:RecordRange;
|
_rootRecordRange:RecordRange;
|
||||||
_enforceNoNewChanges:boolean;
|
_enforceNoNewChanges:boolean;
|
||||||
|
@ -42,28 +58,32 @@ export class ChangeDetector {
|
||||||
var record = this._rootRecordRange.findFirstEnabledRecord();
|
var record = this._rootRecordRange.findFirstEnabledRecord();
|
||||||
var currentRange, currentGroup;
|
var currentRange, currentGroup;
|
||||||
|
|
||||||
while (isPresent(record)) {
|
try {
|
||||||
if (record.check()) {
|
while (isPresent(record)) {
|
||||||
count++;
|
if (record.check()) {
|
||||||
if (record.terminatesExpression()) {
|
count++;
|
||||||
if (throwOnChange) throw new ExpressionChangedAfterItHasBeenChecked(record);
|
if (record.terminatesExpression()) {
|
||||||
currentRange = record.recordRange;
|
if (throwOnChange) throw new ExpressionChangedAfterItHasBeenChecked(record);
|
||||||
currentGroup = record.groupMemento();
|
currentRange = record.recordRange;
|
||||||
updatedRecords = this._addRecord(updatedRecords, record);
|
currentGroup = record.groupMemento();
|
||||||
|
updatedRecords = this._addRecord(updatedRecords, record);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (isPresent(updatedRecords)) {
|
if (isPresent(updatedRecords)) {
|
||||||
var nextEnabled = record.nextEnabled;
|
var nextEnabled = record.nextEnabled;
|
||||||
if (isBlank(nextEnabled) || // we have reached the last enabled record
|
if (isBlank(nextEnabled) || // we have reached the last enabled record
|
||||||
currentRange != nextEnabled.recordRange || // the next record is in a different range
|
currentRange != nextEnabled.recordRange || // the next record is in a different range
|
||||||
currentGroup != nextEnabled.groupMemento()) { // the next record is in a different group
|
currentGroup != nextEnabled.groupMemento()) { // the next record is in a different group
|
||||||
currentRange.dispatcher.onRecordChange(currentGroup, updatedRecords);
|
currentRange.dispatcher.onRecordChange(currentGroup, updatedRecords);
|
||||||
updatedRecords = null;
|
updatedRecords = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
record = record.findNextEnabled();
|
record = record.findNextEnabled();
|
||||||
|
}
|
||||||
|
} catch(e) {
|
||||||
|
throw new ChangeDetectionError(record, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'test_lib/test_lib';
|
import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'test_lib/test_lib';
|
||||||
|
|
||||||
import {isPresent, isBlank, isJsObject} from 'facade/lang';
|
import {isPresent, isBlank, isJsObject, BaseException} from 'facade/lang';
|
||||||
import {List, ListWrapper, MapWrapper} from 'facade/collection';
|
import {List, ListWrapper, MapWrapper} from 'facade/collection';
|
||||||
import {ContextWithVariableBindings} from 'change_detection/parser/context_with_variable_bindings';
|
import {ContextWithVariableBindings} from 'change_detection/parser/context_with_variable_bindings';
|
||||||
import {Parser} from 'change_detection/parser/parser';
|
import {Parser} from 'change_detection/parser/parser';
|
||||||
|
@ -12,15 +12,16 @@ import {
|
||||||
ProtoRecordRange,
|
ProtoRecordRange,
|
||||||
RecordRange,
|
RecordRange,
|
||||||
ChangeDispatcher,
|
ChangeDispatcher,
|
||||||
ProtoRecord
|
ProtoRecord,
|
||||||
|
ChangeDetectionError
|
||||||
} from 'change_detection/change_detector';
|
} from 'change_detection/change_detector';
|
||||||
|
|
||||||
import {Record} from 'change_detection/record';
|
import {Record} from 'change_detection/record';
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
function ast(exp:string) {
|
function ast(exp:string, location:string = 'location') {
|
||||||
var parser = new Parser(new Lexer());
|
var parser = new Parser(new Lexer());
|
||||||
return parser.parseBinding(exp, 'location');
|
return parser.parseBinding(exp, location);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createChangeDetector(memo:string, exp:string, context = null, formatters = null,
|
function createChangeDetector(memo:string, exp:string, context = null, formatters = null,
|
||||||
|
@ -271,7 +272,7 @@ export function main() {
|
||||||
var context = new TestData("not collection / null");
|
var context = new TestData("not collection / null");
|
||||||
var c = createChangeDetector('a', 'a', context, null, true);
|
var c = createChangeDetector('a', 'a', context, null, true);
|
||||||
expect(() => c["changeDetector"].detectChanges())
|
expect(() => c["changeDetector"].detectChanges())
|
||||||
.toThrowError("Collection records must be array like, map like or null");
|
.toThrowError(new RegExp("Collection records must be array like, map like or null"));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("list", () => {
|
describe("list", () => {
|
||||||
|
@ -454,6 +455,20 @@ export function main() {
|
||||||
}).toThrowError(new RegExp("Expression 'a in location' has changed after it was checked"));
|
}).toThrowError(new RegExp("Expression 'a in location' has changed after it was checked"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("error handling", () => {
|
||||||
|
it("should wrap exceptions into ChangeDetectionError", () => {
|
||||||
|
try {
|
||||||
|
var rr = createRange(new TestDispatcher(), ast("invalidProp", "someComponent"), 1);
|
||||||
|
detectChangesInRange(rr);
|
||||||
|
|
||||||
|
throw new BaseException("fail");
|
||||||
|
} catch (e) {
|
||||||
|
expect(e).toBeAnInstanceOf(ChangeDetectionError);
|
||||||
|
expect(e.location).toEqual("invalidProp in someComponent");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue