Skip to content

Commit 1c95b63

Browse files
l46kokcopybara-github
authored andcommitted
Track source of evaluation errors in planner, surface error location
PiperOrigin-RevId: 840916857
1 parent 8b4c579 commit 1c95b63

File tree

73 files changed

+875
-462
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+875
-462
lines changed

common/BUILD.bazel

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ java_library(
5353
exports = ["//common/src/main/java/dev/cel/common:mutable_source"],
5454
)
5555

56-
java_library(
57-
name = "runtime_exception",
58-
# used_by_android
59-
visibility = ["//:internal"],
60-
exports = ["//common/src/main/java/dev/cel/common:runtime_exception"],
61-
)
62-
6356
java_library(
6457
name = "proto_json_adapter",
6558
exports = ["//common/src/main/java/dev/cel/common:proto_json_adapter"],

common/exceptions/BUILD.bazel

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
3+
package(
4+
default_applicable_licenses = ["//:license"],
5+
default_visibility = ["//:internal"],
6+
)
7+
8+
java_library(
9+
name = "runtime_exception",
10+
# used_by_android
11+
exports = ["//common/src/main/java/dev/cel/common/exceptions:runtime_exception"],
12+
)
13+
14+
java_library(
15+
name = "attribute_not_found",
16+
# used_by_android
17+
exports = ["//common/src/main/java/dev/cel/common/exceptions:attribute_not_found"],
18+
)
19+
20+
java_library(
21+
name = "divide_by_zero",
22+
# used_by_android
23+
exports = ["//common/src/main/java/dev/cel/common/exceptions:divide_by_zero"],
24+
)
25+
26+
java_library(
27+
name = "index_out_of_bounds",
28+
# used_by_android
29+
exports = ["//common/src/main/java/dev/cel/common/exceptions:index_out_of_bounds"],
30+
)
31+
32+
java_library(
33+
name = "bad_format",
34+
# used_by_android
35+
exports = ["//common/src/main/java/dev/cel/common/exceptions:bad_format"],
36+
)
37+
38+
java_library(
39+
name = "numeric_overflow",
40+
# used_by_android
41+
exports = ["//common/src/main/java/dev/cel/common/exceptions:numeric_overflow"],
42+
)
43+
44+
java_library(
45+
name = "invalid_argument",
46+
# used_by_android
47+
exports = ["//common/src/main/java/dev/cel/common/exceptions:invalid_argument"],
48+
)

common/src/main/java/dev/cel/common/BUILD.bazel

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,6 @@ java_library(
172172
],
173173
)
174174

175-
java_library(
176-
name = "runtime_exception",
177-
srcs = ["CelRuntimeException.java"],
178-
# used_by_android
179-
tags = [
180-
],
181-
deps = [
182-
":error_codes",
183-
"//common/annotations",
184-
],
185-
)
186-
187175
java_library(
188176
name = "mutable_ast",
189177
srcs = ["CelMutableAst.java"],
@@ -205,7 +193,6 @@ java_library(
205193
],
206194
deps = [
207195
":cel_source",
208-
"//:auto_value",
209196
"//common/ast:mutable_expr",
210197
"@maven//:com_google_errorprone_error_prone_annotations",
211198
"@maven//:com_google_guava_guava",
@@ -249,7 +236,6 @@ java_library(
249236
":source",
250237
":source_location",
251238
"//:auto_value",
252-
"//common/annotations",
253239
"//common/ast",
254240
"//common/internal",
255241
"@maven//:com_google_errorprone_error_prone_annotations",
@@ -360,8 +346,5 @@ java_library(
360346
srcs = ["Operator.java"],
361347
tags = [
362348
],
363-
deps = [
364-
"//common/ast",
365-
"@maven//:com_google_guava_guava",
366-
],
349+
deps = ["@maven//:com_google_guava_guava"],
367350
)

common/src/main/java/dev/cel/common/CelException.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ public CelException(String message, Throwable cause) {
2727
super(message, cause);
2828
}
2929

30-
public CelException(String message, CelErrorCode errorCode) {
31-
super(message);
32-
this.errorCode = errorCode;
33-
}
34-
3530
public CelException(String message, Throwable cause, CelErrorCode errorCode) {
3631
super(message, cause);
3732
this.errorCode = errorCode;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
3+
package(
4+
default_applicable_licenses = ["//:license"],
5+
default_visibility = [
6+
"//common/exceptions:__pkg__",
7+
"//publish:__pkg__",
8+
],
9+
)
10+
11+
java_library(
12+
name = "runtime_exception",
13+
srcs = ["CelRuntimeException.java"],
14+
# used_by_android
15+
tags = [
16+
],
17+
deps = [
18+
"//common:error_codes",
19+
"//common/annotations",
20+
],
21+
)
22+
23+
java_library(
24+
name = "attribute_not_found",
25+
srcs = ["CelAttributeNotFoundException.java"],
26+
# used_by_android
27+
tags = [
28+
],
29+
deps = [
30+
"//common:error_codes",
31+
"//common/annotations",
32+
"//common/exceptions:runtime_exception",
33+
],
34+
)
35+
36+
java_library(
37+
name = "divide_by_zero",
38+
srcs = ["CelDivideByZeroException.java"],
39+
# used_by_android
40+
tags = [
41+
],
42+
deps = [
43+
"//common:error_codes",
44+
"//common/annotations",
45+
"//common/exceptions:runtime_exception",
46+
],
47+
)
48+
49+
java_library(
50+
name = "index_out_of_bounds",
51+
srcs = ["CelIndexOutOfBoundsException.java"],
52+
# used_by_android
53+
tags = [
54+
],
55+
deps = [
56+
"//common:error_codes",
57+
"//common/annotations",
58+
"//common/exceptions:runtime_exception",
59+
],
60+
)
61+
62+
java_library(
63+
name = "bad_format",
64+
srcs = ["CelBadFormatException.java"],
65+
# used_by_android
66+
tags = [
67+
],
68+
deps = [
69+
"//common:error_codes",
70+
"//common/annotations",
71+
"//common/exceptions:runtime_exception",
72+
],
73+
)
74+
75+
java_library(
76+
name = "numeric_overflow",
77+
srcs = ["CelNumericOverflowException.java"],
78+
# used_by_android
79+
tags = [
80+
],
81+
deps = [
82+
"//common:error_codes",
83+
"//common/annotations",
84+
"//common/exceptions:runtime_exception",
85+
],
86+
)
87+
88+
java_library(
89+
name = "invalid_argument",
90+
srcs = ["CelInvalidArgumentException.java"],
91+
# used_by_android
92+
tags = [
93+
],
94+
deps = [
95+
"//common:error_codes",
96+
"//common/annotations",
97+
"//common/exceptions:runtime_exception",
98+
],
99+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.exceptions;
16+
17+
import dev.cel.common.CelErrorCode;
18+
import dev.cel.common.annotations.Internal;
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
22+
/** Indicates an attempt to access a map or object using an invalid attribute or key. */
23+
@Internal
24+
public final class CelAttributeNotFoundException extends CelRuntimeException {
25+
26+
public static CelAttributeNotFoundException of(String message) {
27+
return new CelAttributeNotFoundException(message);
28+
}
29+
30+
public static CelAttributeNotFoundException forMissingMapKey(String key) {
31+
return new CelAttributeNotFoundException(String.format("key '%s' is not present in map.", key));
32+
}
33+
34+
public static CelAttributeNotFoundException forFieldResolution(String... fields) {
35+
return forFieldResolution(Arrays.asList(fields));
36+
}
37+
38+
public static CelAttributeNotFoundException forFieldResolution(Collection<String> fields) {
39+
return new CelAttributeNotFoundException(formatErrorMessage(fields));
40+
}
41+
42+
private static String formatErrorMessage(Collection<String> fields) {
43+
String maybePlural = "";
44+
if (fields.size() > 1) {
45+
maybePlural = "s";
46+
}
47+
48+
return String.format(
49+
"Error resolving field%s '%s'. Field selections must be performed on messages or maps.",
50+
maybePlural, String.join(", ", fields));
51+
}
52+
53+
private CelAttributeNotFoundException(String message) {
54+
super(message, CelErrorCode.ATTRIBUTE_NOT_FOUND);
55+
}
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.exceptions;
16+
17+
import dev.cel.common.CelErrorCode;
18+
import dev.cel.common.annotations.Internal;
19+
20+
/** Indicates that a data conversion failed due to a mismatch in the format specification. */
21+
@Internal
22+
public final class CelBadFormatException extends CelRuntimeException {
23+
24+
public CelBadFormatException(Throwable cause) {
25+
super(cause, CelErrorCode.BAD_FORMAT);
26+
}
27+
28+
public CelBadFormatException(String errorMessage) {
29+
super(errorMessage, CelErrorCode.BAD_FORMAT);
30+
}
31+
}

runtime/src/main/java/dev/cel/runtime/standard/ArithmeticHelpers.java renamed to common/src/main/java/dev/cel/common/exceptions/CelDivideByZeroException.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package dev.cel.runtime.standard;
15+
package dev.cel.common.exceptions;
1616

1717
import dev.cel.common.CelErrorCode;
18+
import dev.cel.common.annotations.Internal;
1819

19-
final class ArithmeticHelpers {
20+
/** Indicates that a division by zero occurred. */
21+
@Internal
22+
public final class CelDivideByZeroException extends CelRuntimeException {
2023

21-
static CelErrorCode getArithmeticErrorCode(ArithmeticException e) {
22-
String exceptionMessage = e.getMessage();
23-
// The two known cases for an arithmetic exception is divide by zero and overflow.
24-
if (exceptionMessage.equals("/ by zero")) {
25-
return CelErrorCode.DIVIDE_BY_ZERO;
26-
}
27-
return CelErrorCode.NUMERIC_OVERFLOW;
24+
public CelDivideByZeroException() {
25+
super("/ by zero", CelErrorCode.DIVIDE_BY_ZERO);
2826
}
2927

30-
private ArithmeticHelpers() {}
28+
public CelDivideByZeroException(Throwable cause) {
29+
super(cause, CelErrorCode.DIVIDE_BY_ZERO);
30+
}
3131
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.exceptions;
16+
17+
import dev.cel.common.CelErrorCode;
18+
import dev.cel.common.annotations.Internal;
19+
20+
/** Indicates that a list index access was attempted using an index that is out of bounds. */
21+
@Internal
22+
public final class CelIndexOutOfBoundsException extends CelRuntimeException {
23+
24+
public CelIndexOutOfBoundsException(Object index) {
25+
super("Index out of bounds: " + index, CelErrorCode.INDEX_OUT_OF_BOUNDS);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.exceptions;
16+
17+
import dev.cel.common.CelErrorCode;
18+
import dev.cel.common.annotations.Internal;
19+
20+
/** Indicates that an invalid argument was supplied to a function. */
21+
@Internal
22+
public final class CelInvalidArgumentException extends CelRuntimeException {
23+
24+
public CelInvalidArgumentException(Throwable cause) {
25+
super(cause, CelErrorCode.INVALID_ARGUMENT);
26+
}
27+
}

0 commit comments

Comments
 (0)