Skip to content

Commit

Permalink
Change standard functions to directly throw CelEvaluationException
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 698181950
  • Loading branch information
l46kok authored and copybara-github committed Nov 21, 2024
1 parent b061eb8 commit c62c975
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@
public final class CelEvaluationException extends CelException {

public CelEvaluationException(String message) {
super(message);
super(formatErrorMessage(message));
}

public CelEvaluationException(String message, Throwable cause) {
super(message, cause);
super(formatErrorMessage(message), cause);
}

public CelEvaluationException(String message, Throwable cause, CelErrorCode errorCode) {
super(message, cause, errorCode);
super(formatErrorMessage(message), cause, errorCode);
}

public CelEvaluationException(String message, CelErrorCode errorCode) {
super(message, errorCode);
}

CelEvaluationException(InterpreterException cause) {
this(cause, cause.getErrorCode());
super(formatErrorMessage(message), errorCode);
}

CelEvaluationException(InterpreterException cause, CelErrorCode errorCode) {
super(cause.getMessage(), cause.getCause(), errorCode);
}

private static String formatErrorMessage(String message) {
return String.format("evaluation error: %s", message);
}
}
94 changes: 31 additions & 63 deletions runtime/src/main/java/dev/cel/runtime/CelStandardFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.int64Add(x, y, bindingHelper.celOptions);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
})),
ADD_UINT64(
Expand All @@ -334,8 +333,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.uint64Add(x, y);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
});
} else {
Expand All @@ -348,8 +346,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.uint64Add(x, y, bindingHelper.celOptions);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
});
}
Expand Down Expand Up @@ -395,8 +392,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.int64Subtract(x, y, bindingHelper.celOptions);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
})),
SUBTRACT_TIMESTAMP_TIMESTAMP(
Expand Down Expand Up @@ -425,8 +421,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.uint64Subtract(x, y);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
});
} else {
Expand All @@ -439,8 +434,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.uint64Subtract(x, y, bindingHelper.celOptions);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
});
}
Expand All @@ -467,8 +461,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.int64Multiply(x, y, bindingHelper.celOptions);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
})),
MULTIPLY_DOUBLE(
Expand All @@ -487,8 +480,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.uint64Multiply(x, y);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
});
} else {
Expand All @@ -501,8 +493,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.uint64Multiply(x, y, bindingHelper.celOptions);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
});
}
Expand All @@ -522,8 +513,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.int64Divide(x, y, bindingHelper.celOptions);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
})),
DIVIDE_UINT64(
Expand Down Expand Up @@ -554,8 +544,7 @@ public enum Arithmetic implements StandardOverload {
return x % y;
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
})),
MODULO_UINT64(
Expand Down Expand Up @@ -584,8 +573,7 @@ public enum Arithmetic implements StandardOverload {
return RuntimeHelpers.int64Negate(x, bindingHelper.celOptions);
} catch (ArithmeticException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
getArithmeticErrorCode(e));
e.getMessage(), e, getArithmeticErrorCode(e));
}
})),
NEGATE_DOUBLE(
Expand Down Expand Up @@ -708,9 +696,8 @@ public enum Conversions implements StandardOverload {
return false;
default:
throw new CelEvaluationException(
new InterpreterException.Builder(
"Type conversion error from 'string' to 'bool': [%s]", str)
.build(),
String.format(
"Type conversion error from 'string' to 'bool': [%s]", str),
CelErrorCode.BAD_FORMAT);
}
})),
Expand All @@ -729,9 +716,7 @@ public enum Conversions implements StandardOverload {
.orElseThrow(
() ->
new CelEvaluationException(
new InterpreterException.Builder(
"double is out of range for int")
.build(),
"double is out of range for int",
CelErrorCode.NUMERIC_OVERFLOW));
}
return arg.longValue();
Expand All @@ -746,8 +731,7 @@ public enum Conversions implements StandardOverload {
return Long.parseLong(arg);
} catch (NumberFormatException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
CelErrorCode.BAD_FORMAT);
e.getMessage(), e, CelErrorCode.BAD_FORMAT);
}
})),
TIMESTAMP_TO_INT64(
Expand All @@ -763,8 +747,7 @@ public enum Conversions implements StandardOverload {
(UnsignedLong arg) -> {
if (arg.compareTo(UnsignedLong.valueOf(Long.MAX_VALUE)) > 0) {
throw new CelEvaluationException(
new InterpreterException.Builder("unsigned out of int range").build(),
CelErrorCode.NUMERIC_OVERFLOW);
"unsigned out of int range", CelErrorCode.NUMERIC_OVERFLOW);
}
return arg.longValue();
});
Expand All @@ -775,8 +758,7 @@ public enum Conversions implements StandardOverload {
(Long arg) -> {
if (bindingHelper.celOptions.errorOnIntWrap() && arg < 0) {
throw new CelEvaluationException(
new InterpreterException.Builder("unsigned out of int range").build(),
CelErrorCode.NUMERIC_OVERFLOW);
"unsigned out of int range", CelErrorCode.NUMERIC_OVERFLOW);
}
return arg;
});
Expand All @@ -801,8 +783,7 @@ public enum Conversions implements StandardOverload {
(Long arg) -> {
if (bindingHelper.celOptions.errorOnIntWrap() && arg < 0) {
throw new CelEvaluationException(
new InterpreterException.Builder("int out of uint range").build(),
CelErrorCode.NUMERIC_OVERFLOW);
"int out of uint range", CelErrorCode.NUMERIC_OVERFLOW);
}
return UnsignedLong.valueOf(arg);
});
Expand All @@ -813,8 +794,7 @@ public enum Conversions implements StandardOverload {
(Long arg) -> {
if (bindingHelper.celOptions.errorOnIntWrap() && arg < 0) {
throw new CelEvaluationException(
new InterpreterException.Builder("int out of uint range").build(),
CelErrorCode.NUMERIC_OVERFLOW);
"int out of uint range", CelErrorCode.NUMERIC_OVERFLOW);
}
return arg;
});
Expand All @@ -832,9 +812,7 @@ public enum Conversions implements StandardOverload {
.orElseThrow(
() ->
new CelEvaluationException(
new InterpreterException.Builder("double out of uint range")
.build(),
CelErrorCode.NUMERIC_OVERFLOW));
"double out of uint range", CelErrorCode.NUMERIC_OVERFLOW));
}
return UnsignedLong.valueOf(BigDecimal.valueOf(arg).toBigInteger());
});
Expand All @@ -849,9 +827,7 @@ public enum Conversions implements StandardOverload {
.orElseThrow(
() ->
new CelEvaluationException(
new InterpreterException.Builder("double out of uint range")
.build(),
CelErrorCode.NUMERIC_OVERFLOW));
"double out of uint range", CelErrorCode.NUMERIC_OVERFLOW));
}
return arg.longValue();
});
Expand All @@ -868,8 +844,7 @@ public enum Conversions implements StandardOverload {
return UnsignedLong.valueOf(arg);
} catch (NumberFormatException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
CelErrorCode.BAD_FORMAT);
e.getMessage(), e, CelErrorCode.BAD_FORMAT);
}
});
} else {
Expand All @@ -881,8 +856,7 @@ public enum Conversions implements StandardOverload {
return UnsignedLongs.parseUnsignedLong(arg);
} catch (NumberFormatException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
CelErrorCode.BAD_FORMAT);
e.getMessage(), e, CelErrorCode.BAD_FORMAT);
}
});
}
Expand All @@ -904,8 +878,7 @@ public enum Conversions implements StandardOverload {
return Double.parseDouble(arg);
} catch (NumberFormatException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
CelErrorCode.BAD_FORMAT);
e.getMessage(), e, CelErrorCode.BAD_FORMAT);
}
})),
UINT64_TO_DOUBLE(
Expand Down Expand Up @@ -973,8 +946,7 @@ public enum Conversions implements StandardOverload {
return RuntimeHelpers.createDurationFromString(d);
} catch (IllegalArgumentException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).build(),
CelErrorCode.BAD_FORMAT);
e.getMessage(), e, CelErrorCode.BAD_FORMAT);
}
})),

Expand All @@ -988,8 +960,7 @@ public enum Conversions implements StandardOverload {
return Timestamps.parse(ts);
} catch (ParseException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).build(),
CelErrorCode.BAD_FORMAT);
e.getMessage(), e, CelErrorCode.BAD_FORMAT);
}
})),
TIMESTAMP_TO_TIMESTAMP(
Expand Down Expand Up @@ -1031,8 +1002,7 @@ public enum StringMatchers implements StandardOverload {
return RuntimeHelpers.matches(string, regexp, bindingHelper.celOptions);
} catch (PatternSyntaxException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
CelErrorCode.INVALID_ARGUMENT);
e.getMessage(), e, CelErrorCode.INVALID_ARGUMENT);
}
})),
// Duplicate receiver-style matches overload.
Expand All @@ -1047,8 +1017,7 @@ public enum StringMatchers implements StandardOverload {
return RuntimeHelpers.matches(string, regexp, bindingHelper.celOptions);
} catch (PatternSyntaxException e) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).setCause(e).build(),
CelErrorCode.INVALID_ARGUMENT);
e.getMessage(), e, CelErrorCode.INVALID_ARGUMENT);
}
})),
CONTAINS_STRING(
Expand Down Expand Up @@ -2076,8 +2045,7 @@ private static ZoneId timeZone(String tz) throws CelEvaluationException {
try {
int ind = tz.indexOf(":");
if (ind == -1) {
throw new CelEvaluationException(
new InterpreterException.Builder(e.getMessage()).build());
throw new CelEvaluationException(e.getMessage());
}

int hourOffset = Integer.parseInt(tz.substring(0, ind));
Expand All @@ -2092,7 +2060,7 @@ private static ZoneId timeZone(String tz) throws CelEvaluationException {
return ZoneId.of(formattedOffset);

} catch (DateTimeException e2) {
throw new CelEvaluationException(new InterpreterException.Builder(e2.getMessage()).build());
throw new CelEvaluationException(e2.getMessage());
}
}
}
Expand Down

0 comments on commit c62c975

Please sign in to comment.