2626import com .google .common .collect .Lists ;
2727import com .google .errorprone .annotations .Immutable ;
2828import dev .cel .checker .CelCheckerBuilder ;
29+ import dev .cel .common .CelErrorCode ;
2930import dev .cel .common .CelFunctionDecl ;
3031import dev .cel .common .CelOverloadDecl ;
32+ import dev .cel .common .CelRuntimeException ;
3133import dev .cel .common .internal .CelCodePointArray ;
3234import dev .cel .common .types .ListType ;
3335import dev .cel .common .types .SimpleType ;
3436import dev .cel .compiler .CelCompilerLibrary ;
35- import dev .cel .runtime .CelEvaluationException ;
36- import dev .cel .runtime .CelEvaluationExceptionBuilder ;
3737import dev .cel .runtime .CelFunctionBinding ;
3838import dev .cel .runtime .CelRuntimeBuilder ;
3939import 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 ) {
0 commit comments