Skip to content

Commit

Permalink
suggestion for verbose error reporting as per #721
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-arad committed Dec 23, 2016
1 parent 6ed404b commit 17c5f9b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 3 additions & 5 deletions src/core/computedvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
};

peekUntracked() {
let hasError = true;
try {
const res = this.peek();
hasError = false;
return res;
} finally {
if (hasError)
handleExceptionInDerivation(this);
} catch(e) {
handleExceptionInDerivation(this, e);
throw e;
}

}
Expand Down
16 changes: 9 additions & 7 deletions src/core/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,24 @@ export function trackDerivedFunction<T>(derivation: IDerivation, f: () => T) {
derivation.runId = ++globalState.runId;
const prevTracking = globalState.trackingDerivation;
globalState.trackingDerivation = derivation;
let hasException = true;
let hasException = false;
let result: T;
try {
result = f.call(derivation);
hasException = false;
} catch(e){
hasException = true;
handleExceptionInDerivation(derivation, e);
throw e;
} finally {
if (hasException) {
handleExceptionInDerivation(derivation);
} else {
if (!hasException) {
globalState.trackingDerivation = prevTracking;
bindDependencies(derivation);
}
}
return result;
}

export function handleExceptionInDerivation(derivation: IDerivation) {
export function handleExceptionInDerivation(derivation: IDerivation, cause?:Error) {
const message = (
`[mobx] An uncaught exception occurred while calculating your computed value, autorun or transformer. Or inside the render() method of an observer based React component. ` +
`These functions should never throw exceptions as MobX will not always be able to recover from them. ` +
Expand All @@ -145,7 +146,8 @@ export function handleExceptionInDerivation(derivation: IDerivation) {
if (isSpyEnabled()) {
spyReport({
type: "error",
message
message,
cause
});
}
console.warn(message); // In next major, maybe don't emit this message at all?
Expand Down

0 comments on commit 17c5f9b

Please sign in to comment.