Skip to content

Commit

Permalink
Add better error messaging for failed expression eval and parsing (#239)
Browse files Browse the repository at this point in the history
* Add better error messaging for failed expression eval and parsing

* Remove nested-error-stacks from dep list
  • Loading branch information
adierkens authored Nov 16, 2023
1 parent 6ea2d4c commit f5b1f77
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 0 additions & 2 deletions core/player/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ javascript_pipeline(
"@npm//p-defer",
"@npm//queue-microtask",
"@npm//tapable-ts",
"@npm//nested-error-stacks",
"@npm//@types/nested-error-stacks",
"@npm//parsimmon",
"@npm//@types/parsimmon",
"@npm//arr-flatten",
Expand Down
2 changes: 1 addition & 1 deletion core/player/src/expressions/__tests__/evaluator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ describe('evaluator', () => {

test('throws errors for unknown expressions', () => {
expect(() => evaluator.evaluate('foo()')).toThrowError(
'Unknown expression function: foo'
'Error evaluating expression: foo()'
);
});

Expand Down
23 changes: 15 additions & 8 deletions core/player/src/expressions/evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SyncWaterfallHook, SyncBailHook } from 'tapable-ts';
import { NestedError } from 'ts-nested-error';
import { parseExpression } from './parser';
import * as DEFAULT_EXPRESSION_HANDLERS from './evaluator-functions';
import { isExpressionNode } from './types';
Expand Down Expand Up @@ -221,21 +222,27 @@ export class ExpressionEvaluator {
[, matchedExp] = Array.from(matches); // In case the expression was surrounded by @[ ]@
}

try {
const storedAST = this.expressionsCache.get(matchedExp);
let storedAST: ExpressionNode;

if (storedAST) {
return this._execAST(storedAST, options);
try {
storedAST =
this.expressionsCache.get(matchedExp) ?? parseExpression(matchedExp);
this.expressionsCache.set(matchedExp, storedAST);
} catch (e: any) {
if (options.throwErrors || !this.hooks.onError.call(e)) {
// Only throw the error if it's not handled by the hook, or throwErrors is true
throw new NestedError(`Error parsing expression: ${exp}`, e);
}

const expAST = parseExpression(matchedExp);
this.expressionsCache.set(matchedExp, expAST);
return;
}

return this._execAST(expAST, options);
try {
return this._execAST(storedAST, options);
} catch (e: any) {
if (options.throwErrors || !this.hooks.onError.call(e)) {
// Only throw the error if it's not handled by the hook, or throwErrors is true
throw e;
throw new NestedError(`Error evaluating expression: ${exp}`, e);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"@types/lz-string": "^1.3.34",
"@types/mdx-js__react": "^1.5.5",
"@types/mkdirp": "^1.0.2",
"@types/nested-error-stacks": "^2.1.0",
"@types/node": "^16.11.12",
"@types/parsimmon": "^1.10.0",
"@types/pubsub-js": "^1.8.3",
Expand Down Expand Up @@ -144,7 +143,6 @@
"mdast-util-toc": "^6.1.0",
"mkdirp": "^1.0.4",
"monaco-editor": "^0.31.1",
"nested-error-stacks": "^2.1.1",
"next": "^12.0.7",
"nextjs-google-analytics": "^1.2.1",
"p-defer": "^3.0.0",
Expand Down

0 comments on commit f5b1f77

Please sign in to comment.