Skip to content

Commit 69bb558

Browse files
l46kokcopybara-github
authored andcommitted
Replace InterpreterExceptions with CelEvaluationException in the runtime path
PiperOrigin-RevId: 721092577
1 parent 3138203 commit 69bb558

15 files changed

+183
-263
lines changed

runtime/src/main/java/dev/cel/runtime/Activation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public static Activation fromProto(Message message, CelOptions celOptions) {
171171
} catch (IllegalArgumentException e) {
172172
variables.put(
173173
field.getName(),
174-
new InterpreterException.Builder(
174+
CelEvaluationExceptionBuilder.newBuilder(
175175
"illegal field value. field=%s, value=%s", field.getName(), fieldValue)
176176
.setCause(e)
177177
.build());

runtime/src/main/java/dev/cel/runtime/BUILD.bazel

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ java_library(
5555
tags = [
5656
],
5757
deps = [
58+
":evaluation_exception",
5859
":metadata",
5960
"//:auto_value",
6061
"//common",
@@ -80,6 +81,8 @@ java_library(
8081
deps = [
8182
":base",
8283
":cel_type_resolver",
84+
":evaluation_exception",
85+
":evaluation_exception_builder",
8386
":evaluation_listener",
8487
":metadata",
8588
":runtime_helper",
@@ -197,7 +200,6 @@ java_library(
197200
],
198201
deps = [
199202
":evaluation_exception",
200-
":evaluation_exception_builder",
201203
":evaluation_listener",
202204
":runtime_helper",
203205
":runtime_type_provider_legacy",
@@ -206,12 +208,14 @@ java_library(
206208
"//common",
207209
"//common:error_codes",
208210
"//common:options",
211+
"//common:runtime_exception",
209212
"//common/annotations",
210213
"//common/internal:cel_descriptor_pools",
211214
"//common/internal:comparison_functions",
212215
"//common/internal:default_message_factory",
213216
"//common/internal:dynamic_proto",
214217
"//common/internal:proto_message_factory",
218+
"//common/internal:safe_string_formatter",
215219
"//common/types:cel_types",
216220
"//common/values:cel_value_provider",
217221
"//common/values:proto_message_value_provider",
@@ -295,9 +299,9 @@ java_library(
295299
tags = [
296300
],
297301
deps = [
298-
":base",
299302
":unknown_attributes",
300303
"//common/annotations",
304+
"//runtime",
301305
"@maven//:com_google_errorprone_error_prone_annotations",
302306
"@maven//:org_jspecify_jspecify",
303307
],

runtime/src/main/java/dev/cel/runtime/CelLateFunctionBindings.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.errorprone.annotations.Immutable;
21-
import dev.cel.common.CelException;
2221
import java.util.Arrays;
2322
import java.util.List;
2423
import java.util.Optional;
@@ -38,7 +37,7 @@ private CelLateFunctionBindings(ImmutableMap<String, ResolvedOverload> functions
3837

3938
@Override
4039
public Optional<ResolvedOverload> findOverload(
41-
String functionName, List<String> overloadIds, Object[] args) throws CelException {
40+
String functionName, List<String> overloadIds, Object[] args) throws CelEvaluationException {
4241
return DefaultDispatcher.findOverload(functionName, overloadIds, functions, args);
4342
}
4443

@@ -59,12 +58,6 @@ private static ResolvedOverload createResolvedOverload(CelRuntime.CelFunctionBin
5958
return CelResolvedOverload.of(
6059
binding.getOverloadId(),
6160
binding.getArgTypes(),
62-
(args) -> {
63-
try {
64-
return binding.getDefinition().apply(args);
65-
} catch (CelException e) {
66-
throw InterpreterException.wrapOrThrow(e);
67-
}
68-
});
61+
(args) -> binding.getDefinition().apply(args));
6962
}
7063
}

runtime/src/main/java/dev/cel/runtime/CelRuntime.java

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.common.base.Preconditions;
1919
import com.google.common.collect.ImmutableList;
2020
import com.google.errorprone.annotations.CanIgnoreReturnValue;
21-
import com.google.errorprone.annotations.CheckReturnValue;
2221
import com.google.errorprone.annotations.Immutable;
2322
import javax.annotation.concurrent.ThreadSafe;
2423
import com.google.protobuf.Message;
@@ -192,30 +191,26 @@ private Object evalInternal(
192191
Optional<CelFunctionResolver> lateBoundFunctionResolver,
193192
CelEvaluationListener listener)
194193
throws CelEvaluationException {
195-
try {
196-
Interpretable impl = getInterpretable();
197-
if (getOptions().enableUnknownTracking()) {
198-
Preconditions.checkState(
199-
impl instanceof UnknownTrackingInterpretable,
200-
"Environment misconfigured. Requested unknown tracking without a compatible"
201-
+ " implementation.");
202-
203-
UnknownTrackingInterpretable interpreter = (UnknownTrackingInterpretable) impl;
204-
return interpreter.evalTrackingUnknowns(
205-
RuntimeUnknownResolver.builder()
206-
.setResolver(context.variableResolver())
207-
.setAttributeResolver(context.createAttributeResolver())
208-
.build(),
209-
lateBoundFunctionResolver,
210-
listener);
211-
} else {
212-
if (lateBoundFunctionResolver.isPresent()) {
213-
return impl.eval(context.variableResolver(), lateBoundFunctionResolver.get(), listener);
214-
}
215-
return impl.eval(context.variableResolver(), listener);
194+
Interpretable impl = getInterpretable();
195+
if (getOptions().enableUnknownTracking()) {
196+
Preconditions.checkState(
197+
impl instanceof UnknownTrackingInterpretable,
198+
"Environment misconfigured. Requested unknown tracking without a compatible"
199+
+ " implementation.");
200+
201+
UnknownTrackingInterpretable interpreter = (UnknownTrackingInterpretable) impl;
202+
return interpreter.evalTrackingUnknowns(
203+
RuntimeUnknownResolver.builder()
204+
.setResolver(context.variableResolver())
205+
.setAttributeResolver(context.createAttributeResolver())
206+
.build(),
207+
lateBoundFunctionResolver,
208+
listener);
209+
} else {
210+
if (lateBoundFunctionResolver.isPresent()) {
211+
return impl.eval(context.variableResolver(), lateBoundFunctionResolver.get(), listener);
216212
}
217-
} catch (InterpreterException e) {
218-
throw unwrapOrCreateEvaluationException(e);
213+
return impl.eval(context.variableResolver(), listener);
219214
}
220215
}
221216

@@ -229,16 +224,6 @@ private Object evalInternal(
229224
static Program from(Interpretable interpretable, CelOptions options) {
230225
return new AutoValue_CelRuntime_Program(interpretable, options);
231226
}
232-
233-
@CheckReturnValue
234-
private static CelEvaluationException unwrapOrCreateEvaluationException(
235-
InterpreterException e) {
236-
if (e.getCause() instanceof CelEvaluationException) {
237-
return (CelEvaluationException) e.getCause();
238-
}
239-
240-
return new CelEvaluationException(e.getMessage(), e.getCause(), e.getErrorCode());
241-
}
242227
}
243228

244229
/**

runtime/src/main/java/dev/cel/runtime/CelRuntimeLegacyImpl.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,7 @@ public CelRuntimeLegacyImpl build() {
257257
.forEach(
258258
(String overloadId, CelFunctionBinding func) ->
259259
dispatcher.add(
260-
overloadId,
261-
func.getArgTypes(),
262-
(args) -> {
263-
try {
264-
return func.getDefinition().apply(args);
265-
} catch (CelEvaluationException e) {
266-
throw new InterpreterException.Builder(e.getMessage())
267-
.setCause(e)
268-
.setErrorCode(e.getErrorCode())
269-
.build();
270-
}
271-
}));
260+
overloadId, func.getArgTypes(), (args) -> func.getDefinition().apply(args)));
272261

273262
RuntimeTypeProvider runtimeTypeProvider;
274263

0 commit comments

Comments
 (0)