Skip to content

Commit 9faa264

Browse files
l46kokcopybara-github
authored andcommitted
Add a named constructor that accepts CelRuntimeException to CelEvaluationExceptionBuilder
PiperOrigin-RevId: 720652803
1 parent 1ac9c4b commit 9faa264

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ java_library(
172172
":evaluation_exception",
173173
":metadata",
174174
"//common:error_codes",
175+
"//common:runtime_exception",
175176
"//common/annotations",
176177
"//common/internal:safe_string_formatter",
177178
"@maven//:com_google_errorprone_error_prone_annotations",

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1818
import dev.cel.common.CelErrorCode;
19+
import dev.cel.common.CelRuntimeException;
1920
import dev.cel.common.annotations.Internal;
2021
import dev.cel.common.internal.SafeStringFormatter;
2122
import org.jspecify.annotations.Nullable;
@@ -74,6 +75,19 @@ public static CelEvaluationExceptionBuilder newBuilder(String message, Object...
7475
return new CelEvaluationExceptionBuilder(SafeStringFormatter.format(message, args));
7576
}
7677

78+
/**
79+
* Constructs a new builder for {@link CelEvaluationException}
80+
*
81+
* <p>CEL Library Internals. Do not use.
82+
*/
83+
@Internal
84+
public static CelEvaluationExceptionBuilder newBuilder(CelRuntimeException celRuntimeException) {
85+
Throwable cause = celRuntimeException.getCause();
86+
return new CelEvaluationExceptionBuilder(cause.getMessage())
87+
.setCause(cause)
88+
.setErrorCode(celRuntimeException.getErrorCode());
89+
}
90+
7791
private CelEvaluationExceptionBuilder(String message) {
7892
this.message = message;
7993
this.errorCode = CelErrorCode.INTERNAL_ERROR;

runtime/src/test/java/dev/cel/runtime/CelEvaluationExceptionBuilderTest.java

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

1919
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2020
import dev.cel.common.CelErrorCode;
21+
import dev.cel.common.CelRuntimeException;
2122
import org.junit.Test;
2223
import org.junit.runner.RunWith;
2324

@@ -77,4 +78,19 @@ public int getPosition(long exprId) {
7778
assertThat(e).hasCauseThat().isEqualTo(cause);
7879
assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.BAD_FORMAT);
7980
}
81+
82+
@Test
83+
public void builder_fromCelRuntimeException() {
84+
IllegalStateException cause = new IllegalStateException("cause error message");
85+
CelRuntimeException celRuntimeException =
86+
new CelRuntimeException(cause, CelErrorCode.BAD_FORMAT);
87+
CelEvaluationExceptionBuilder builder =
88+
CelEvaluationExceptionBuilder.newBuilder(celRuntimeException);
89+
90+
CelEvaluationException e = builder.build();
91+
92+
assertThat(e).hasMessageThat().isEqualTo("evaluation error: cause error message");
93+
assertThat(e).hasCauseThat().isEqualTo(cause);
94+
assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.BAD_FORMAT);
95+
}
8096
}

0 commit comments

Comments
 (0)