Skip to content

Commit 1ac9c4b

Browse files
l46kokcopybara-github
authored andcommitted
Create a builder for CelEvaluationException
PiperOrigin-RevId: 719430537
1 parent f011e20 commit 1ac9c4b

File tree

13 files changed

+375
-121
lines changed

13 files changed

+375
-121
lines changed

bundle/src/test/java/dev/cel/bundle/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ java_library(
3232
"//parser",
3333
"//parser:macro",
3434
"//runtime",
35+
"//runtime:evaluation_exception_builder",
3536
"//runtime:unknown_attributes",
3637
"@cel_spec//proto/cel/expr:expr_java_proto",
3738
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",

bundle/src/test/java/dev/cel/bundle/CelImplTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import dev.cel.runtime.CelAttribute.Qualifier;
9393
import dev.cel.runtime.CelAttributePattern;
9494
import dev.cel.runtime.CelEvaluationException;
95+
import dev.cel.runtime.CelEvaluationExceptionBuilder;
9596
import dev.cel.runtime.CelRuntime;
9697
import dev.cel.runtime.CelRuntime.CelFunctionBinding;
9798
import dev.cel.runtime.CelRuntime.Program;
@@ -754,8 +755,9 @@ public void program_withThrowingFunctionShortcircuited() throws Exception {
754755
"throws",
755756
ImmutableList.of(),
756757
(args) -> {
757-
throw new CelEvaluationException(
758-
"this method always throws", new RuntimeException("reason"));
758+
throw CelEvaluationExceptionBuilder.newBuilder("this method always throws")
759+
.setCause(new RuntimeException("reason"))
760+
.build();
759761
}))
760762
.setResultType(SimpleType.BOOL)
761763
.build();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ java_library(
4040
"//common/types",
4141
"//compiler:compiler_builder",
4242
"//runtime",
43+
"//runtime:evaluation_exception_builder",
4344
"@maven//:com_google_errorprone_error_prone_annotations",
4445
"@maven//:com_google_guava_guava",
4546
],

extensions/src/main/java/dev/cel/extensions/CelStringExtensions.java

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import dev.cel.common.types.SimpleType;
3333
import dev.cel.compiler.CelCompilerLibrary;
3434
import dev.cel.runtime.CelEvaluationException;
35+
import dev.cel.runtime.CelEvaluationExceptionBuilder;
3536
import dev.cel.runtime.CelRuntime;
3637
import dev.cel.runtime.CelRuntimeBuilder;
3738
import dev.cel.runtime.CelRuntimeLibrary;
@@ -264,17 +265,20 @@ private static String charAt(String s, long i) throws CelEvaluationException {
264265
try {
265266
index = Math.toIntExact(i);
266267
} catch (ArithmeticException e) {
267-
throw new CelEvaluationException(
268-
String.format("charAt failure: Index must not exceed the int32 range: %d", i), e);
268+
throw CelEvaluationExceptionBuilder.newBuilder(
269+
"charAt failure: Index must not exceed the int32 range: %d", i)
270+
.setCause(e)
271+
.build();
269272
}
270273

271274
CelCodePointArray codePointArray = CelCodePointArray.fromString(s);
272275
if (index == codePointArray.length()) {
273276
return "";
274277
}
275278
if (index < 0 || index > codePointArray.length()) {
276-
throw new CelEvaluationException(
277-
String.format("charAt failure: Index out of range: %d", index));
279+
throw CelEvaluationExceptionBuilder.newBuilder(
280+
"charAt failure: Index out of range: %d", index)
281+
.build();
278282
}
279283

280284
return codePointArray.slice(index, index + 1).toString();
@@ -296,10 +300,10 @@ private static Long indexOf(Object[] args) throws CelEvaluationException {
296300
try {
297301
offset = Math.toIntExact(offsetInLong);
298302
} catch (ArithmeticException e) {
299-
throw new CelEvaluationException(
300-
String.format(
301-
"indexOf failure: Offset must not exceed the int32 range: %d", offsetInLong),
302-
e);
303+
throw CelEvaluationExceptionBuilder.newBuilder(
304+
"indexOf failure: Offset must not exceed the int32 range: %d", offsetInLong)
305+
.setCause(e)
306+
.build();
303307
}
304308

305309
return indexOf(str, substr, offset);
@@ -314,8 +318,9 @@ private static Long indexOf(String str, String substr, int offset) throws CelEva
314318
CelCodePointArray substrCpa = CelCodePointArray.fromString(substr);
315319

316320
if (offset < 0 || offset >= strCpa.length()) {
317-
throw new CelEvaluationException(
318-
String.format("indexOf failure: Offset out of range: %d", offset));
321+
throw CelEvaluationExceptionBuilder.newBuilder(
322+
"indexOf failure: Offset out of range: %d", offset)
323+
.build();
319324
}
320325

321326
return safeIndexOf(strCpa, substrCpa, offset);
@@ -376,14 +381,16 @@ private static Long lastIndexOf(CelCodePointArray str, CelCodePointArray substr,
376381
try {
377382
off = Math.toIntExact(offset);
378383
} catch (ArithmeticException e) {
379-
throw new CelEvaluationException(
380-
String.format("lastIndexOf failure: Offset must not exceed the int32 range: %d", offset),
381-
e);
384+
throw CelEvaluationExceptionBuilder.newBuilder(
385+
"lastIndexOf failure: Offset must not exceed the int32 range: %d", offset)
386+
.setCause(e)
387+
.build();
382388
}
383389

384390
if (off < 0 || off >= str.length()) {
385-
throw new CelEvaluationException(
386-
String.format("lastIndexOf failure: Offset out of range: %d", offset));
391+
throw CelEvaluationExceptionBuilder.newBuilder(
392+
"lastIndexOf failure: Offset out of range: %d", offset)
393+
.build();
387394
}
388395

389396
if (off > str.length() - substr.length()) {
@@ -416,9 +423,10 @@ private static String replace(Object[] objects) throws CelEvaluationException {
416423
try {
417424
index = Math.toIntExact(indexInLong);
418425
} catch (ArithmeticException e) {
419-
throw new CelEvaluationException(
420-
String.format("replace failure: Index must not exceed the int32 range: %d", indexInLong),
421-
e);
426+
throw CelEvaluationExceptionBuilder.newBuilder(
427+
"replace failure: Index must not exceed the int32 range: %d", indexInLong)
428+
.setCause(e)
429+
.build();
422430
}
423431

424432
return replace((String) objects[0], (String) objects[1], (String) objects[2], index);
@@ -473,9 +481,10 @@ private static List<String> split(Object[] args) throws CelEvaluationException {
473481
try {
474482
limit = Math.toIntExact(limitInLong);
475483
} catch (ArithmeticException e) {
476-
throw new CelEvaluationException(
477-
String.format("split failure: Limit must not exceed the int32 range: %d", limitInLong),
478-
e);
484+
throw CelEvaluationExceptionBuilder.newBuilder(
485+
"split failure: Limit must not exceed the int32 range: %d", limitInLong)
486+
.setCause(e)
487+
.build();
479488
}
480489

481490
return split((String) args[0], (String) args[1], limit);
@@ -536,18 +545,20 @@ private static Object substring(String s, long i) throws CelEvaluationException
536545
try {
537546
beginIndex = Math.toIntExact(i);
538547
} catch (ArithmeticException e) {
539-
throw new CelEvaluationException(
540-
String.format("substring failure: Index must not exceed the int32 range: %d", i), e);
548+
throw CelEvaluationExceptionBuilder.newBuilder(
549+
"substring failure: Index must not exceed the int32 range: %d", i)
550+
.setCause(e)
551+
.build();
541552
}
542553

543554
CelCodePointArray codePointArray = CelCodePointArray.fromString(s);
544555

545556
boolean indexIsInRange = beginIndex <= codePointArray.length() && beginIndex >= 0;
546557
if (!indexIsInRange) {
547-
throw new CelEvaluationException(
548-
String.format(
558+
throw CelEvaluationExceptionBuilder.newBuilder(
549559
"substring failure: Range [%d, %d) out of bounds",
550-
beginIndex, codePointArray.length()));
560+
beginIndex, codePointArray.length())
561+
.build();
551562
}
552563

553564
if (beginIndex == codePointArray.length()) {
@@ -569,11 +580,11 @@ private static String substring(Object[] args) throws CelEvaluationException {
569580
beginIndex = Math.toIntExact(beginIndexInLong);
570581
endIndex = Math.toIntExact(endIndexInLong);
571582
} catch (ArithmeticException e) {
572-
throw new CelEvaluationException(
573-
String.format(
583+
throw CelEvaluationExceptionBuilder.newBuilder(
574584
"substring failure: Indices must not exceed the int32 range: [%d, %d)",
575-
beginIndexInLong, endIndexInLong),
576-
e);
585+
beginIndexInLong, endIndexInLong)
586+
.setCause(e)
587+
.build();
577588
}
578589

579590
String s = (String) args[0];
@@ -585,8 +596,9 @@ private static String substring(Object[] args) throws CelEvaluationException {
585596
&& beginIndex <= codePointArray.length()
586597
&& endIndex <= codePointArray.length();
587598
if (!indicesIsInRange) {
588-
throw new CelEvaluationException(
589-
String.format("substring failure: Range [%d, %d) out of bounds", beginIndex, endIndex));
599+
throw CelEvaluationExceptionBuilder.newBuilder(
600+
"substring failure: Range [%d, %d) out of bounds", beginIndex, endIndex)
601+
.build();
590602
}
591603

592604
if (beginIndex == endIndex) {

runtime/BUILD.bazel

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ java_library(
1010
exports = [
1111
"//runtime/src/main/java/dev/cel/runtime",
1212
"//runtime/src/main/java/dev/cel/runtime:evaluation_exception",
13+
"//runtime/src/main/java/dev/cel/runtime:metadata",
1314
],
1415
)
1516

17+
java_library(
18+
name = "evaluation_exception_builder",
19+
exports = ["//runtime/src/main/java/dev/cel/runtime:evaluation_exception_builder"],
20+
)
21+
1622
java_library(
1723
name = "interpreter",
1824
exports = ["//runtime/src/main/java/dev/cel/runtime:interpreter"],
@@ -46,5 +52,8 @@ java_library(
4652

4753
java_library(
4854
name = "base",
49-
exports = ["//runtime/src/main/java/dev/cel/runtime:base"],
55+
exports = [
56+
"//runtime/src/main/java/dev/cel/runtime:base",
57+
"//runtime/src/main/java/dev/cel/runtime:metadata",
58+
],
5059
)

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ BASE_SOURCES = [
1414
"FunctionOverload.java",
1515
"InterpreterException.java",
1616
"MessageProvider.java",
17-
"Metadata.java",
1817
"Registrar.java",
1918
"ResolvedOverload.java",
2019
]
@@ -56,6 +55,7 @@ java_library(
5655
tags = [
5756
],
5857
deps = [
58+
":metadata",
5959
"//:auto_value",
6060
"//common",
6161
"//common:error_codes",
@@ -81,6 +81,7 @@ java_library(
8181
":base",
8282
":cel_type_resolver",
8383
":evaluation_listener",
84+
":metadata",
8485
":runtime_helper",
8586
":unknown_attributes",
8687
"//:auto_value",
@@ -150,12 +151,41 @@ RUNTIME_SOURCES = [
150151

151152
java_library(
152153
name = "evaluation_exception",
153-
srcs = ["CelEvaluationException.java"],
154+
srcs = [
155+
"CelEvaluationException.java",
156+
],
154157
tags = [
155158
],
156159
deps = [
157160
"//common",
158161
"//common:error_codes",
162+
"@maven//:org_jspecify_jspecify",
163+
],
164+
)
165+
166+
java_library(
167+
name = "evaluation_exception_builder",
168+
srcs = ["CelEvaluationExceptionBuilder.java"],
169+
tags = [
170+
],
171+
deps = [
172+
":evaluation_exception",
173+
":metadata",
174+
"//common:error_codes",
175+
"//common/annotations",
176+
"//common/internal:safe_string_formatter",
177+
"@maven//:com_google_errorprone_error_prone_annotations",
178+
"@maven//:com_google_guava_guava",
179+
"@maven//:org_jspecify_jspecify",
180+
],
181+
)
182+
183+
java_library(
184+
name = "metadata",
185+
srcs = ["Metadata.java"],
186+
deps = [
187+
"//common/annotations",
188+
"@maven//:com_google_errorprone_error_prone_annotations",
159189
],
160190
)
161191

@@ -166,6 +196,7 @@ java_library(
166196
],
167197
deps = [
168198
":evaluation_exception",
199+
":evaluation_exception_builder",
169200
":evaluation_listener",
170201
":runtime_helper",
171202
":runtime_type_provider_legacy",

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

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

1717
import dev.cel.common.CelErrorCode;
1818
import dev.cel.common.CelException;
19+
import org.jspecify.annotations.Nullable;
1920

2021
/**
2122
* CelEvaluationException encapsulates the potential issues which could arise during the
@@ -24,27 +25,14 @@
2425
public final class CelEvaluationException extends CelException {
2526

2627
public CelEvaluationException(String message) {
27-
super(formatErrorMessage(message));
28+
super(message);
2829
}
2930

30-
public CelEvaluationException(String message, Throwable cause) {
31-
super(formatErrorMessage(message), cause);
31+
public CelEvaluationException(String message, @Nullable Throwable cause) {
32+
super(message, cause);
3233
}
3334

34-
public CelEvaluationException(String message, CelErrorCode errorCode) {
35-
super(formatErrorMessage(message), errorCode);
36-
}
37-
38-
public CelEvaluationException(String message, Throwable cause, CelErrorCode errorCode) {
39-
this(message, cause, errorCode, true);
40-
}
41-
42-
CelEvaluationException(
43-
String message, Throwable cause, CelErrorCode errorCode, boolean formatErrorMessage) {
44-
super(formatErrorMessage ? formatErrorMessage(message) : message, cause, errorCode);
45-
}
46-
47-
private static String formatErrorMessage(String message) {
48-
return String.format("evaluation error: %s", message);
35+
CelEvaluationException(String message, @Nullable Throwable cause, CelErrorCode errorCode) {
36+
super(message, cause, errorCode);
4937
}
5038
}

0 commit comments

Comments
 (0)