Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions common/exceptions/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
7 changes: 1 addition & 6 deletions common/src/main/java/dev/cel/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -249,7 +248,6 @@ java_library(
":source",
":source_location",
"//:auto_value",
"//common/annotations",
"//common/ast",
"//common/internal",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -360,8 +358,5 @@ java_library(
srcs = ["Operator.java"],
tags = [
],
deps = [
"//common/ast",
"@maven//:com_google_guava_guava",
],
deps = ["@maven//:com_google_guava_guava"],
)
5 changes: 0 additions & 5 deletions common/src/main/java/dev/cel/common/CelException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions common/src/main/java/dev/cel/common/CelRuntimeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
* Wrapper for an unchecked runtime exception with a CelErrorCode supplied.
*
* <p>Note: This is not to be confused with the notion of CEL Runtime. Use {@code
* CelEvaluationException} instead to signify an evaluation error.
*
* <p>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;
Expand Down
87 changes: 87 additions & 0 deletions common/src/main/java/dev/cel/common/exceptions/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
Original file line number Diff line number Diff line change
@@ -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<String> fields) {
return new CelAttributeNotFoundException(formatErrorMessage(fields));
}

private static String formatErrorMessage(Collection<String> fields) {
String maybePlural = "";
if (fields.size() > 1) {
maybePlural = "s";
}

return String.format(
"Error resolving field%s '%s'. Field selections must be performed on messages or maps.",
maybePlural, String.join(", ", fields));
}

private CelAttributeNotFoundException(String message) {
super(message, CelErrorCode.ATTRIBUTE_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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;

/** Indicates that a data conversion failed due to a mismatch in the format specification. */
@Internal
public final class CelBadFormatException extends CelRuntimeException {

public CelBadFormatException(Throwable cause) {
super(cause, CelErrorCode.BAD_FORMAT);
}

public CelBadFormatException(String errorMessage) {
super(errorMessage, CelErrorCode.BAD_FORMAT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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;

/** Indicates that a division by zero occurred. */
@Internal
public final class CelDivideByZeroException extends CelRuntimeException {

public CelDivideByZeroException() {
super("/ by zero", CelErrorCode.DIVIDE_BY_ZERO);
}

public CelDivideByZeroException(Throwable cause) {
super(cause, CelErrorCode.DIVIDE_BY_ZERO);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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;

/** Indicates that a list index access was attempted using an index that is out of bounds. */
@Internal
public final class CelIndexOutOfBoundsException extends CelRuntimeException {

public CelIndexOutOfBoundsException(Object index) {
super("Index out of bounds: " + index, CelErrorCode.INDEX_OUT_OF_BOUNDS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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;

/** Indicates that an invalid argument was supplied to a function. */
@Internal
public final class CelInvalidArgumentException extends CelRuntimeException {

public CelInvalidArgumentException(Throwable cause) {
super(cause, CelErrorCode.INVALID_ARGUMENT);
}
}
Loading