Skip to content

Commit f11d057

Browse files
Guarantee that run() never throws an error (#283)
1 parent e7c1ad3 commit f11d057

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

packages/cel/src/parse.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { parse as internalParse } from "./parser.js";
2020
import { create } from "@bufbuild/protobuf";
2121

2222
/**
23-
* Parses a CEL expression string into an abstract syntax tree (AST).
23+
* Parses a CEL expression string into an abstract syntax tree (AST) or
24+
* throws an Error if parsing fails.
2425
*
2526
* This is the first stage of CEL evaluation. The resulting ParsedExpr
2627
* can be passed to plan() for execution planning.

packages/cel/src/run.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
// limitations under the License.
1414

1515
import { celEnv, type CelEnvOptions } from "./env.js";
16+
import { celError, type CelError } from "./error.js";
1617
import { parse } from "./parse.js";
1718
import { plan } from "./plan.js";
18-
import type { CelInput } from "./type.js";
19+
import type { CelInput, CelValue } from "./type.js";
1920

2021
/**
2122
* Convenience function that parses, plans, and executes a CEL expression in one call.
23+
* run() always returns a CelValue or a CelError and never throws exceptions. Use
24+
* isCelError() to distinguish the two cases.
2225
*
2326
* This is the simplest way to evaluate a CEL expression, but for better performance
2427
* and reusability, consider using parse(), plan(), and execution separately.
@@ -27,6 +30,10 @@ export function run(
2730
expr: string,
2831
bindings?: Record<string, CelInput>,
2932
envOptions?: CelEnvOptions,
30-
) {
31-
return plan(celEnv(envOptions), parse(expr))(bindings);
33+
): CelValue | CelError {
34+
try {
35+
return plan(celEnv(envOptions), parse(expr))(bindings);
36+
} catch (e: unknown) {
37+
return celError(e);
38+
}
3239
}

0 commit comments

Comments
 (0)