Skip to content

Commit

Permalink
Remove InterpreterException
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 719502577
  • Loading branch information
l46kok authored and copybara-github committed Jan 29, 2025
1 parent b071767 commit 3d9942e
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 396 deletions.
2 changes: 1 addition & 1 deletion runtime/src/main/java/dev/cel/runtime/Activation.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static Activation fromProto(Message message, CelOptions celOptions) {
} catch (IllegalArgumentException e) {
variables.put(
field.getName(),
new InterpreterException.Builder(
CelEvaluationExceptionBuilder.newBuilder(
"illegal field value. field=%s, value=%s", field.getName(), fieldValue)
.setCause(e)
.build());
Expand Down
13 changes: 6 additions & 7 deletions runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ BASE_SOURCES = [
"DefaultMetadata.java",
"FunctionResolver.java",
"FunctionOverload.java",
"InterpreterException.java",
"MessageProvider.java",
"Registrar.java",
"ResolvedOverload.java",
Expand Down Expand Up @@ -55,18 +54,15 @@ java_library(
tags = [
],
deps = [
":evaluation_exception",
":metadata",
"//:auto_value",
"//common",
"//common:error_codes",
"//common:runtime_exception",
"//common/annotations",
"//common/internal:safe_string_formatter",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:org_jspecify_jspecify",
],
)

Expand All @@ -80,6 +76,8 @@ java_library(
deps = [
":base",
":cel_type_resolver",
":evaluation_exception",
":evaluation_exception_builder",
":evaluation_listener",
":metadata",
":runtime_helper",
Expand Down Expand Up @@ -197,7 +195,6 @@ java_library(
],
deps = [
":evaluation_exception",
":evaluation_exception_builder",
":evaluation_listener",
":runtime_helper",
":runtime_type_provider_legacy",
Expand All @@ -206,12 +203,14 @@ java_library(
"//common",
"//common:error_codes",
"//common:options",
"//common:runtime_exception",
"//common/annotations",
"//common/internal:cel_descriptor_pools",
"//common/internal:comparison_functions",
"//common/internal:default_message_factory",
"//common/internal:dynamic_proto",
"//common/internal:proto_message_factory",
"//common/internal:safe_string_formatter",
"//common/types:cel_types",
"//common/values:cel_value_provider",
"//common/values:proto_message_value_provider",
Expand Down Expand Up @@ -295,9 +294,9 @@ java_library(
tags = [
],
deps = [
":base",
":unknown_attributes",
"//common/annotations",
"//runtime",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:org_jspecify_jspecify",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ public CelEvaluationExceptionBuilder setMetadata(Metadata metadata, long exprId)
*/
@Internal
public CelEvaluationException build() {
// TODO: Temporary until InterpreterException removal is complete
if (!message.startsWith("evaluation error")) {
message = SafeStringFormatter.format("evaluation error%s: %s", errorLocation, message);
}

return new CelEvaluationException(message, cause, errorCode);
return new CelEvaluationException(
SafeStringFormatter.format("evaluation error%s: %s", errorLocation, message),
cause,
errorCode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand All @@ -38,7 +37,7 @@ private CelLateFunctionBindings(ImmutableMap<String, ResolvedOverload> functions

@Override
public Optional<ResolvedOverload> findOverload(
String functionName, List<String> overloadIds, Object[] args) throws CelException {
String functionName, List<String> overloadIds, Object[] args) throws CelEvaluationException {
return DefaultDispatcher.findOverload(functionName, overloadIds, functions, args);
}

Expand All @@ -59,12 +58,6 @@ private static ResolvedOverload createResolvedOverload(CelRuntime.CelFunctionBin
return CelResolvedOverload.of(
binding.getOverloadId(),
binding.getArgTypes(),
(args) -> {
try {
return binding.getDefinition().apply(args);
} catch (CelException e) {
throw InterpreterException.wrapOrThrow(e);
}
});
(args) -> binding.getDefinition().apply(args));
}
}
53 changes: 19 additions & 34 deletions runtime/src/main/java/dev/cel/runtime/CelRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
import javax.annotation.concurrent.ThreadSafe;
import com.google.protobuf.Message;
Expand Down Expand Up @@ -192,30 +191,26 @@ private Object evalInternal(
Optional<CelFunctionResolver> lateBoundFunctionResolver,
CelEvaluationListener listener)
throws CelEvaluationException {
try {
Interpretable impl = getInterpretable();
if (getOptions().enableUnknownTracking()) {
Preconditions.checkState(
impl instanceof UnknownTrackingInterpretable,
"Environment misconfigured. Requested unknown tracking without a compatible"
+ " implementation.");

UnknownTrackingInterpretable interpreter = (UnknownTrackingInterpretable) impl;
return interpreter.evalTrackingUnknowns(
RuntimeUnknownResolver.builder()
.setResolver(context.variableResolver())
.setAttributeResolver(context.createAttributeResolver())
.build(),
lateBoundFunctionResolver,
listener);
} else {
if (lateBoundFunctionResolver.isPresent()) {
return impl.eval(context.variableResolver(), lateBoundFunctionResolver.get(), listener);
}
return impl.eval(context.variableResolver(), listener);
Interpretable impl = getInterpretable();
if (getOptions().enableUnknownTracking()) {
Preconditions.checkState(
impl instanceof UnknownTrackingInterpretable,
"Environment misconfigured. Requested unknown tracking without a compatible"
+ " implementation.");

UnknownTrackingInterpretable interpreter = (UnknownTrackingInterpretable) impl;
return interpreter.evalTrackingUnknowns(
RuntimeUnknownResolver.builder()
.setResolver(context.variableResolver())
.setAttributeResolver(context.createAttributeResolver())
.build(),
lateBoundFunctionResolver,
listener);
} else {
if (lateBoundFunctionResolver.isPresent()) {
return impl.eval(context.variableResolver(), lateBoundFunctionResolver.get(), listener);
}
} catch (InterpreterException e) {
throw unwrapOrCreateEvaluationException(e);
return impl.eval(context.variableResolver(), listener);
}
}

Expand All @@ -229,16 +224,6 @@ private Object evalInternal(
static Program from(Interpretable interpretable, CelOptions options) {
return new AutoValue_CelRuntime_Program(interpretable, options);
}

@CheckReturnValue
private static CelEvaluationException unwrapOrCreateEvaluationException(
InterpreterException e) {
if (e.getCause() instanceof CelEvaluationException) {
return (CelEvaluationException) e.getCause();
}

return new CelEvaluationException(e.getMessage(), e.getCause(), e.getErrorCode());
}
}

/**
Expand Down
13 changes: 1 addition & 12 deletions runtime/src/main/java/dev/cel/runtime/CelRuntimeLegacyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,7 @@ public CelRuntimeLegacyImpl build() {
.forEach(
(String overloadId, CelFunctionBinding func) ->
dispatcher.add(
overloadId,
func.getArgTypes(),
(args) -> {
try {
return func.getDefinition().apply(args);
} catch (CelEvaluationException e) {
throw new InterpreterException.Builder(e.getMessage())
.setCause(e)
.setErrorCode(e.getErrorCode())
.build();
}
}));
overloadId, func.getArgTypes(), (args) -> func.getDefinition().apply(args)));

RuntimeTypeProvider runtimeTypeProvider;

Expand Down
Loading

0 comments on commit 3d9942e

Please sign in to comment.