3232import dev .cel .common .types .SimpleType ;
3333import dev .cel .compiler .CelCompilerLibrary ;
3434import dev .cel .runtime .CelEvaluationException ;
35+ import dev .cel .runtime .CelEvaluationExceptionBuilder ;
3536import dev .cel .runtime .CelRuntime ;
3637import dev .cel .runtime .CelRuntimeBuilder ;
3738import 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 ) {
0 commit comments