Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose More Information About Expression Parsing Errors #328

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions core/player/src/expressions/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
/**
* An expression to AST parser based on JSEP: http://jsep.from.so/
*/
import type { ExpressionNode, ExpressionNodeType, NodeLocation } from './types';
import type {
ErrorWithLocation,
ExpressionNode,
ExpressionNodeType,
NodeLocation,
} from './types';
import { ExpNodeOpaqueIdentifier } from './types';

const PERIOD_CODE = 46; // '.'
Expand Down Expand Up @@ -62,16 +67,8 @@ const binaryOps: Record<string, number> = {
'%': 14,
};

interface ErrorWithLocation extends Error {
/** The place in the string where the error occurs */
index: number;

/** a helpful description */
description: string;
}

/** Wrap the message and index in an error and throw it */
function throwError(message: string, index: number) {
function throwError(message: string, index: number): ErrorWithLocation {
const err = new Error(`${message} at character ${index}`);

(err as ErrorWithLocation).index = index;
Expand Down
8 changes: 8 additions & 0 deletions core/player/src/expressions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,11 @@ export type ExpressionNode =
| ObjectNode;

export type ExpressionNodeType = ExpressionNode['type'];

export interface ErrorWithLocation extends Error {
/** The place in the string where the error occurs */
index: number;

/** a helpful description */
description: string;
}
11 changes: 11 additions & 0 deletions core/player/src/expressions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isExpressionNode } from './types';
import type {
ErrorWithLocation,
ExpressionHandler,
ExpressionNode,
ExpressionObjectType,
Expand Down Expand Up @@ -148,3 +149,13 @@
'value' in expr
);
}

/**
* Type guard for ErrorWithLocation
*/
export function isErrorWithLocation(error: Error): error is ErrorWithLocation {
return (

Check warning on line 157 in core/player/src/expressions/utils.ts

View check run for this annotation

Codecov / codecov/patch

core/player/src/expressions/utils.ts#L157

Added line #L157 was not covered by tests
(error as ErrorWithLocation).index !== undefined &&
(error as ErrorWithLocation).description !== undefined
);
}