diff --git a/common/exceptions/BUILD.bazel b/common/exceptions/BUILD.bazel new file mode 100644 index 000000000..6ffb2dcb9 --- /dev/null +++ b/common/exceptions/BUILD.bazel @@ -0,0 +1,42 @@ +load("@rules_java//java:defs.bzl", "java_library") + +package( + default_applicable_licenses = ["//:license"], + default_visibility = ["//:internal"], +) + +java_library( + name = "attribute_not_found", + # used_by_android + exports = ["//common/src/main/java/dev/cel/common/exceptions:attribute_not_found"], +) + +java_library( + name = "divide_by_zero", + # used_by_android + exports = ["//common/src/main/java/dev/cel/common/exceptions:divide_by_zero"], +) + +java_library( + name = "index_out_of_bounds", + # used_by_android + exports = ["//common/src/main/java/dev/cel/common/exceptions:index_out_of_bounds"], +) + +java_library( + name = "bad_format", + # used_by_android + exports = ["//common/src/main/java/dev/cel/common/exceptions:bad_format"], +) + +java_library( + name = "numeric_overflow", + # used_by_android + exports = ["//common/src/main/java/dev/cel/common/exceptions:numeric_overflow"], +) + +java_library( + name = "invalid_argument", + # used_by_android + exports = ["//common/src/main/java/dev/cel/common/exceptions:invalid_argument"], +) diff --git a/common/src/main/java/dev/cel/common/BUILD.bazel b/common/src/main/java/dev/cel/common/BUILD.bazel index 7cd10f392..a5fffe049 100644 --- a/common/src/main/java/dev/cel/common/BUILD.bazel +++ b/common/src/main/java/dev/cel/common/BUILD.bazel @@ -205,7 +205,6 @@ java_library( ], deps = [ ":cel_source", - "//:auto_value", "//common/ast:mutable_expr", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:com_google_guava_guava", @@ -249,7 +248,6 @@ java_library( ":source", ":source_location", "//:auto_value", - "//common/annotations", "//common/ast", "//common/internal", "@maven//:com_google_errorprone_error_prone_annotations", @@ -360,8 +358,5 @@ java_library( srcs = ["Operator.java"], tags = [ ], - deps = [ - "//common/ast", - "@maven//:com_google_guava_guava", - ], + deps = ["@maven//:com_google_guava_guava"], ) diff --git a/common/src/main/java/dev/cel/common/CelException.java b/common/src/main/java/dev/cel/common/CelException.java index 9d80a9ba1..55c8623a4 100644 --- a/common/src/main/java/dev/cel/common/CelException.java +++ b/common/src/main/java/dev/cel/common/CelException.java @@ -27,11 +27,6 @@ public CelException(String message, Throwable cause) { super(message, cause); } - public CelException(String message, CelErrorCode errorCode) { - super(message); - this.errorCode = errorCode; - } - public CelException(String message, Throwable cause, CelErrorCode errorCode) { super(message, cause); this.errorCode = errorCode; diff --git a/common/src/main/java/dev/cel/common/CelRuntimeException.java b/common/src/main/java/dev/cel/common/CelRuntimeException.java index 6f194c474..3856d77f5 100644 --- a/common/src/main/java/dev/cel/common/CelRuntimeException.java +++ b/common/src/main/java/dev/cel/common/CelRuntimeException.java @@ -20,15 +20,17 @@ * Wrapper for an unchecked runtime exception with a CelErrorCode supplied. * *
Note: This is not to be confused with the notion of CEL Runtime. Use {@code - * CelEvaluationException} instead to signify an evaluation error. - * - *
TODO: Make this class abstract and define specific exception classes that
- * corresponds to the CelErrorCode.
+ * CelEvaluationException} instead to signify an evaluation error. corresponds to the CelErrorCode.
*/
@Internal
public class CelRuntimeException extends RuntimeException {
private final CelErrorCode errorCode;
+ public CelRuntimeException(String errorMessage, CelErrorCode errorCode) {
+ super(errorMessage);
+ this.errorCode = errorCode;
+ }
+
public CelRuntimeException(Throwable cause, CelErrorCode errorCode) {
super(cause);
this.errorCode = errorCode;
diff --git a/common/src/main/java/dev/cel/common/exceptions/BUILD.bazel b/common/src/main/java/dev/cel/common/exceptions/BUILD.bazel
new file mode 100644
index 000000000..6bd1ad9ca
--- /dev/null
+++ b/common/src/main/java/dev/cel/common/exceptions/BUILD.bazel
@@ -0,0 +1,87 @@
+load("@rules_java//java:defs.bzl", "java_library")
+
+package(
+ default_applicable_licenses = ["//:license"],
+ default_visibility = [
+ "//common/exceptions:__pkg__",
+ "//publish:__pkg__",
+ ],
+)
+
+java_library(
+ name = "attribute_not_found",
+ srcs = ["CelAttributeNotFoundException.java"],
+ # used_by_android
+ tags = [
+ ],
+ deps = [
+ "//common:error_codes",
+ "//common:runtime_exception",
+ "//common/annotations",
+ ],
+)
+
+java_library(
+ name = "divide_by_zero",
+ srcs = ["CelDivideByZeroException.java"],
+ # used_by_android
+ tags = [
+ ],
+ deps = [
+ "//common:error_codes",
+ "//common:runtime_exception",
+ "//common/annotations",
+ ],
+)
+
+java_library(
+ name = "index_out_of_bounds",
+ srcs = ["CelIndexOutOfBoundsException.java"],
+ # used_by_android
+ tags = [
+ ],
+ deps = [
+ "//common:error_codes",
+ "//common:runtime_exception",
+ "//common/annotations",
+ ],
+)
+
+java_library(
+ name = "bad_format",
+ srcs = ["CelBadFormatException.java"],
+ # used_by_android
+ tags = [
+ ],
+ deps = [
+ "//common:error_codes",
+ "//common:runtime_exception",
+ "//common/annotations",
+ ],
+)
+
+java_library(
+ name = "numeric_overflow",
+ srcs = ["CelNumericOverflowException.java"],
+ # used_by_android
+ tags = [
+ ],
+ deps = [
+ "//common:error_codes",
+ "//common:runtime_exception",
+ "//common/annotations",
+ ],
+)
+
+java_library(
+ name = "invalid_argument",
+ srcs = ["CelInvalidArgumentException.java"],
+ # used_by_android
+ tags = [
+ ],
+ deps = [
+ "//common:error_codes",
+ "//common:runtime_exception",
+ "//common/annotations",
+ ],
+)
diff --git a/common/src/main/java/dev/cel/common/exceptions/CelAttributeNotFoundException.java b/common/src/main/java/dev/cel/common/exceptions/CelAttributeNotFoundException.java
new file mode 100644
index 000000000..4aa43693d
--- /dev/null
+++ b/common/src/main/java/dev/cel/common/exceptions/CelAttributeNotFoundException.java
@@ -0,0 +1,57 @@
+// Copyright 2025 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package dev.cel.common.exceptions;
+
+import dev.cel.common.CelErrorCode;
+import dev.cel.common.CelRuntimeException;
+import dev.cel.common.annotations.Internal;
+import java.util.Arrays;
+import java.util.Collection;
+
+/** Indicates an attempt to access a map or object using an invalid attribute or key. */
+@Internal
+public final class CelAttributeNotFoundException extends CelRuntimeException {
+
+ public static CelAttributeNotFoundException of(String message) {
+ return new CelAttributeNotFoundException(message);
+ }
+
+ public static CelAttributeNotFoundException forMissingMapKey(String key) {
+ return new CelAttributeNotFoundException(String.format("key '%s' is not present in map.", key));
+ }
+
+ public static CelAttributeNotFoundException forFieldResolution(String... fields) {
+ return forFieldResolution(Arrays.asList(fields));
+ }
+
+ public static CelAttributeNotFoundException forFieldResolution(Collection