Skip to content

Commit c84d501

Browse files
l46kokcopybara-github
authored andcommitted
Remove CelEvaluationException checked exception from function overloads
PiperOrigin-RevId: 828650061
1 parent 9083e06 commit c84d501

File tree

6 files changed

+97
-73
lines changed

6 files changed

+97
-73
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
import dev.cel.runtime.CelAttribute.Qualifier;
104104
import dev.cel.runtime.CelAttributePattern;
105105
import dev.cel.runtime.CelEvaluationException;
106-
import dev.cel.runtime.CelEvaluationExceptionBuilder;
107106
import dev.cel.runtime.CelFunctionBinding;
108107
import dev.cel.runtime.CelRuntime;
109108
import dev.cel.runtime.CelRuntime.Program;
@@ -742,13 +741,13 @@ public void program_withThrowingFunction() throws Exception {
742741
"throws",
743742
ImmutableList.of(),
744743
(args) -> {
745-
throw new CelEvaluationException("this method always throws");
744+
throw new RuntimeException("this method always throws");
746745
}))
747746
.setResultType(SimpleType.BOOL)
748747
.build();
749748
CelRuntime.Program program = cel.createProgram(cel.compile("throws()").getAst());
750749
CelEvaluationException e = Assert.assertThrows(CelEvaluationException.class, program::eval);
751-
assertThat(e).hasMessageThat().contains("this method always throws");
750+
assertThat(e.getCause()).hasMessageThat().contains("this method always throws");
752751
}
753752

754753
@Test
@@ -770,9 +769,7 @@ public void program_withThrowingFunctionShortcircuited() throws Exception {
770769
"throws",
771770
ImmutableList.of(),
772771
(args) -> {
773-
throw CelEvaluationExceptionBuilder.newBuilder("this method always throws")
774-
.setCause(new RuntimeException("reason"))
775-
.build();
772+
throw new RuntimeException("this method always throws");
776773
}))
777774
.setResultType(SimpleType.BOOL)
778775
.build();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ java_library(
8484
deps = [
8585
"//checker:checker_builder",
8686
"//common:compiler_common",
87+
"//common:error_codes",
88+
"//common:runtime_exception",
8789
"//common/internal",
8890
"//common/types",
8991
"//compiler:compiler_builder",

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

Lines changed: 76 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
import com.google.common.collect.Lists;
2727
import com.google.errorprone.annotations.Immutable;
2828
import dev.cel.checker.CelCheckerBuilder;
29+
import dev.cel.common.CelErrorCode;
2930
import dev.cel.common.CelFunctionDecl;
3031
import dev.cel.common.CelOverloadDecl;
32+
import dev.cel.common.CelRuntimeException;
3133
import dev.cel.common.internal.CelCodePointArray;
3234
import dev.cel.common.types.ListType;
3335
import dev.cel.common.types.SimpleType;
3436
import dev.cel.compiler.CelCompilerLibrary;
35-
import dev.cel.runtime.CelEvaluationException;
36-
import dev.cel.runtime.CelEvaluationExceptionBuilder;
3737
import dev.cel.runtime.CelFunctionBinding;
3838
import dev.cel.runtime.CelRuntimeBuilder;
3939
import dev.cel.runtime.CelRuntimeLibrary;
@@ -253,7 +253,7 @@ String getFunction() {
253253
}
254254

255255
private static final CelExtensionLibrary<CelStringExtensions> LIBRARY =
256-
new CelExtensionLibrary<CelStringExtensions>() {
256+
new CelExtensionLibrary<>() {
257257
private final CelStringExtensions version0 = new CelStringExtensions();
258258

259259
@Override
@@ -291,56 +291,59 @@ public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
291291
functions.forEach(function -> runtimeBuilder.addFunctionBindings(function.functionBindings));
292292
}
293293

294-
private static String charAt(String s, long i) throws CelEvaluationException {
294+
private static String charAt(String s, long i) {
295295
int index;
296296
try {
297297
index = Math.toIntExact(i);
298298
} catch (ArithmeticException e) {
299-
throw CelEvaluationExceptionBuilder.newBuilder(
300-
"charAt failure: Index must not exceed the int32 range: %d", i)
301-
.setCause(e)
302-
.build();
299+
throw new CelRuntimeException(
300+
new IllegalArgumentException(
301+
String.format("charAt failure: Index must not exceed the int32 range: %d", i), e),
302+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
303303
}
304304

305305
CelCodePointArray codePointArray = CelCodePointArray.fromString(s);
306306
if (index == codePointArray.length()) {
307307
return "";
308308
}
309309
if (index < 0 || index > codePointArray.length()) {
310-
throw CelEvaluationExceptionBuilder.newBuilder(
311-
"charAt failure: Index out of range: %d", index)
312-
.build();
310+
throw new CelRuntimeException(
311+
new IllegalArgumentException(
312+
String.format("charAt failure: Index out of range: %d", index)),
313+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
313314
}
314315

315316
return codePointArray.slice(index, index + 1).toString();
316317
}
317318

318-
private static Long indexOf(String str, String substr) throws CelEvaluationException {
319+
private static Long indexOf(String str, String substr) {
319320
Object[] params = {str, substr, 0L};
320321
return indexOf(params);
321322
}
322323

323324
/**
324325
* @param args Object array with indices of: [0: string], [1: substring], [2: offset]
325326
*/
326-
private static Long indexOf(Object[] args) throws CelEvaluationException {
327+
private static Long indexOf(Object[] args) {
327328
String str = (String) args[0];
328329
String substr = (String) args[1];
329330
long offsetInLong = (Long) args[2];
330331
int offset;
331332
try {
332333
offset = Math.toIntExact(offsetInLong);
333334
} catch (ArithmeticException e) {
334-
throw CelEvaluationExceptionBuilder.newBuilder(
335-
"indexOf failure: Offset must not exceed the int32 range: %d", offsetInLong)
336-
.setCause(e)
337-
.build();
335+
throw new CelRuntimeException(
336+
new IllegalArgumentException(
337+
String.format(
338+
"indexOf failure: Offset must not exceed the int32 range: %d", offsetInLong),
339+
e),
340+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
338341
}
339342

340343
return indexOf(str, substr, offset);
341344
}
342345

343-
private static Long indexOf(String str, String substr, int offset) throws CelEvaluationException {
346+
private static Long indexOf(String str, String substr, int offset) {
344347
if (substr.isEmpty()) {
345348
return (long) offset;
346349
}
@@ -349,9 +352,10 @@ private static Long indexOf(String str, String substr, int offset) throws CelEva
349352
CelCodePointArray substrCpa = CelCodePointArray.fromString(substr);
350353

351354
if (offset < 0 || offset >= strCpa.length()) {
352-
throw CelEvaluationExceptionBuilder.newBuilder(
353-
"indexOf failure: Offset out of range: %d", offset)
354-
.build();
355+
throw new CelRuntimeException(
356+
new IllegalArgumentException(
357+
String.format("indexOf failure: Offset out of range: %d", offset)),
358+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
355359
}
356360

357361
return safeIndexOf(strCpa, substrCpa, offset);
@@ -384,7 +388,7 @@ private static String join(List<String> stringList, String separator) {
384388
return Joiner.on(separator).join(stringList);
385389
}
386390

387-
private static Long lastIndexOf(String str, String substr) throws CelEvaluationException {
391+
private static Long lastIndexOf(String str, String substr) {
388392
CelCodePointArray strCpa = CelCodePointArray.fromString(str);
389393
CelCodePointArray substrCpa = CelCodePointArray.fromString(substr);
390394
if (substrCpa.isEmpty()) {
@@ -398,16 +402,15 @@ private static Long lastIndexOf(String str, String substr) throws CelEvaluationE
398402
return lastIndexOf(strCpa, substrCpa, (long) strCpa.length() - 1);
399403
}
400404

401-
private static Long lastIndexOf(Object[] args) throws CelEvaluationException {
405+
private static Long lastIndexOf(Object[] args) {
402406
CelCodePointArray strCpa = CelCodePointArray.fromString((String) args[0]);
403407
CelCodePointArray substrCpa = CelCodePointArray.fromString((String) args[1]);
404408
long offset = (long) args[2];
405409

406410
return lastIndexOf(strCpa, substrCpa, offset);
407411
}
408412

409-
private static Long lastIndexOf(CelCodePointArray str, CelCodePointArray substr, long offset)
410-
throws CelEvaluationException {
413+
private static Long lastIndexOf(CelCodePointArray str, CelCodePointArray substr, long offset) {
411414
if (substr.isEmpty()) {
412415
return offset;
413416
}
@@ -416,16 +419,19 @@ private static Long lastIndexOf(CelCodePointArray str, CelCodePointArray substr,
416419
try {
417420
off = Math.toIntExact(offset);
418421
} catch (ArithmeticException e) {
419-
throw CelEvaluationExceptionBuilder.newBuilder(
420-
"lastIndexOf failure: Offset must not exceed the int32 range: %d", offset)
421-
.setCause(e)
422-
.build();
422+
throw new CelRuntimeException(
423+
new IllegalArgumentException(
424+
String.format(
425+
"lastIndexOf failure: Offset must not exceed the int32 range: %d", offset),
426+
e),
427+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
423428
}
424429

425430
if (off < 0 || off >= str.length()) {
426-
throw CelEvaluationExceptionBuilder.newBuilder(
427-
"lastIndexOf failure: Offset out of range: %d", offset)
428-
.build();
431+
throw new CelRuntimeException(
432+
new IllegalArgumentException(
433+
String.format("lastIndexOf failure: Offset out of range: %d", offset)),
434+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
429435
}
430436

431437
if (off > str.length() - substr.length()) {
@@ -452,16 +458,18 @@ private static String replaceAll(Object[] objects) {
452458
return replace((String) objects[0], (String) objects[1], (String) objects[2], -1);
453459
}
454460

455-
private static String replace(Object[] objects) throws CelEvaluationException {
461+
private static String replace(Object[] objects) {
456462
Long indexInLong = (Long) objects[3];
457463
int index;
458464
try {
459465
index = Math.toIntExact(indexInLong);
460466
} catch (ArithmeticException e) {
461-
throw CelEvaluationExceptionBuilder.newBuilder(
462-
"replace failure: Index must not exceed the int32 range: %d", indexInLong)
463-
.setCause(e)
464-
.build();
467+
throw new CelRuntimeException(
468+
new IllegalArgumentException(
469+
String.format(
470+
"replace failure: Index must not exceed the int32 range: %d", indexInLong),
471+
e),
472+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
465473
}
466474

467475
return replace((String) objects[0], (String) objects[1], (String) objects[2], index);
@@ -510,16 +518,18 @@ private static List<String> split(String str, String separator) {
510518
/**
511519
* @param args Object array with indices of: [0: string], [1: separator], [2: limit]
512520
*/
513-
private static List<String> split(Object[] args) throws CelEvaluationException {
521+
private static List<String> split(Object[] args) {
514522
long limitInLong = (Long) args[2];
515523
int limit;
516524
try {
517525
limit = Math.toIntExact(limitInLong);
518526
} catch (ArithmeticException e) {
519-
throw CelEvaluationExceptionBuilder.newBuilder(
520-
"split failure: Limit must not exceed the int32 range: %d", limitInLong)
521-
.setCause(e)
522-
.build();
527+
throw new CelRuntimeException(
528+
new IllegalArgumentException(
529+
String.format(
530+
"split failure: Limit must not exceed the int32 range: %d", limitInLong),
531+
e),
532+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
523533
}
524534

525535
return split((String) args[0], (String) args[1], limit);
@@ -575,25 +585,27 @@ private static List<String> explode(String str, int limit) {
575585
return exploded;
576586
}
577587

578-
private static Object substring(String s, long i) throws CelEvaluationException {
588+
private static Object substring(String s, long i) {
579589
int beginIndex;
580590
try {
581591
beginIndex = Math.toIntExact(i);
582592
} catch (ArithmeticException e) {
583-
throw CelEvaluationExceptionBuilder.newBuilder(
584-
"substring failure: Index must not exceed the int32 range: %d", i)
585-
.setCause(e)
586-
.build();
593+
throw new CelRuntimeException(
594+
new IllegalArgumentException(
595+
String.format("substring failure: Index must not exceed the int32 range: %d", i), e),
596+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
587597
}
588598

589599
CelCodePointArray codePointArray = CelCodePointArray.fromString(s);
590600

591601
boolean indexIsInRange = beginIndex <= codePointArray.length() && beginIndex >= 0;
592602
if (!indexIsInRange) {
593-
throw CelEvaluationExceptionBuilder.newBuilder(
594-
"substring failure: Range [%d, %d) out of bounds",
595-
beginIndex, codePointArray.length())
596-
.build();
603+
throw new CelRuntimeException(
604+
new IllegalArgumentException(
605+
String.format(
606+
"substring failure: Range [%d, %d) out of bounds",
607+
beginIndex, codePointArray.length())),
608+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
597609
}
598610

599611
if (beginIndex == codePointArray.length()) {
@@ -606,7 +618,7 @@ private static Object substring(String s, long i) throws CelEvaluationException
606618
/**
607619
* @param args Object array with indices of [0: string], [1: beginIndex], [2: endIndex]
608620
*/
609-
private static String substring(Object[] args) throws CelEvaluationException {
621+
private static String substring(Object[] args) {
610622
Long beginIndexInLong = (Long) args[1];
611623
Long endIndexInLong = (Long) args[2];
612624
int beginIndex;
@@ -615,11 +627,13 @@ private static String substring(Object[] args) throws CelEvaluationException {
615627
beginIndex = Math.toIntExact(beginIndexInLong);
616628
endIndex = Math.toIntExact(endIndexInLong);
617629
} catch (ArithmeticException e) {
618-
throw CelEvaluationExceptionBuilder.newBuilder(
619-
"substring failure: Indices must not exceed the int32 range: [%d, %d)",
620-
beginIndexInLong, endIndexInLong)
621-
.setCause(e)
622-
.build();
630+
throw new CelRuntimeException(
631+
new IllegalArgumentException(
632+
String.format(
633+
"substring failure: Indices must not exceed the int32 range: [%d, %d)",
634+
beginIndexInLong, endIndexInLong),
635+
e),
636+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
623637
}
624638

625639
String s = (String) args[0];
@@ -631,9 +645,11 @@ private static String substring(Object[] args) throws CelEvaluationException {
631645
&& beginIndex <= codePointArray.length()
632646
&& endIndex <= codePointArray.length();
633647
if (!indicesIsInRange) {
634-
throw CelEvaluationExceptionBuilder.newBuilder(
635-
"substring failure: Range [%d, %d) out of bounds", beginIndex, endIndex)
636-
.build();
648+
throw new CelRuntimeException(
649+
new IllegalArgumentException(
650+
String.format(
651+
"substring failure: Range [%d, %d) out of bounds", beginIndex, endIndex)),
652+
CelErrorCode.INDEX_OUT_OF_BOUNDS);
637653
}
638654

639655
if (beginIndex == endIndex) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
public interface CelFunctionOverload {
2323

2424
/** Evaluate a set of arguments throwing a {@code CelException} on error. */
25-
Object apply(Object[] args) throws CelEvaluationException;
25+
Object apply(Object[] args);
2626

2727
/**
2828
* Helper interface for describing unary functions where the type-parameter is used to improve
@@ -31,7 +31,7 @@ public interface CelFunctionOverload {
3131
@Immutable
3232
@FunctionalInterface
3333
interface Unary<T> {
34-
Object apply(T arg) throws CelEvaluationException;
34+
Object apply(T arg);
3535
}
3636

3737
/**
@@ -41,6 +41,6 @@ interface Unary<T> {
4141
@Immutable
4242
@FunctionalInterface
4343
interface Binary<T1, T2> {
44-
Object apply(T1 arg1, T2 arg2) throws CelEvaluationException;
44+
Object apply(T1 arg1, T2 arg2);
4545
}
4646
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.primitives.UnsignedLong;
2121
import dev.cel.common.CelErrorCode;
22+
import dev.cel.common.CelRuntimeException;
2223
import dev.cel.expr.conformance.proto3.TestAllTypes;
2324
import java.util.Optional;
2425
import org.junit.Assert;
@@ -83,8 +84,8 @@ public void findOverload_badInput_throwsException() throws Exception {
8384
UnsignedLong.class,
8485
(arg) -> {
8586
if (arg.equals(UnsignedLong.MAX_VALUE)) {
86-
throw new CelEvaluationException(
87-
"numeric overflow", null, CelErrorCode.NUMERIC_OVERFLOW);
87+
throw new CelRuntimeException(
88+
new ArithmeticException("numeric overflow"), CelErrorCode.NUMERIC_OVERFLOW);
8889
}
8990
return arg.plus(UnsignedLong.ONE);
9091
}));
@@ -94,9 +95,9 @@ public void findOverload_badInput_throwsException() throws Exception {
9495
assertThat(overload).isPresent();
9596
assertThat(overload.get().getOverloadId()).isEqualTo("increment_uint");
9697
assertThat(overload.get().getParameterTypes()).containsExactly(UnsignedLong.class);
97-
CelEvaluationException e =
98+
CelRuntimeException e =
9899
Assert.assertThrows(
99-
CelEvaluationException.class,
100+
CelRuntimeException.class,
100101
() -> overload.get().getDefinition().apply(new Object[] {UnsignedLong.MAX_VALUE}));
101102
assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.NUMERIC_OVERFLOW);
102103
}

0 commit comments

Comments
 (0)