Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move error factories from eval/internal to runtime/internal #304

Merged
merged 1 commit into from
Sep 21, 2023
Merged
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
1 change: 1 addition & 0 deletions eval/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ cc_library(
srcs = ["errors.cc"],
hdrs = ["errors.h"],
deps = [
"//runtime/internal:errors",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
Expand Down
47 changes: 1 addition & 46 deletions eval/internal/errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,11 @@
#include "eval/internal/errors.h"

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "runtime/internal/errors.h"
#include "google/protobuf/arena.h"

namespace cel {
namespace runtime_internal {

const absl::Status* DurationOverflowError() {
static const auto* const kDurationOverflow = new absl::Status(
absl::StatusCode::kInvalidArgument, "Duration is out of range");
return kDurationOverflow;
}

absl::Status CreateNoMatchingOverloadError(absl::string_view fn) {
return absl::UnknownError(
absl::StrCat(kErrNoMatchingOverload, fn.empty() ? "" : " : ", fn));
}

absl::Status CreateNoSuchFieldError(absl::string_view field) {
return absl::Status(
absl::StatusCode::kNotFound,
absl::StrCat(kErrNoSuchField, field.empty() ? "" : " : ", field));
}

absl::Status CreateMissingAttributeError(
absl::string_view missing_attribute_path) {
absl::Status result = absl::InvalidArgumentError(
absl::StrCat(kErrMissingAttribute, missing_attribute_path));
result.SetPayload(kPayloadUrlMissingAttributePath,
absl::Cord(missing_attribute_path));
return result;
}

absl::Status CreateNoSuchKeyError(absl::string_view key) {
return absl::NotFoundError(absl::StrCat(kErrNoSuchKey, " : ", key));
}

absl::Status CreateUnknownFunctionResultError(absl::string_view help_message) {
absl::Status result = absl::UnavailableError(
absl::StrCat("Unknown function result: ", help_message));
result.SetPayload(kPayloadUrlUnknownFunctionResult, absl::Cord("true"));
return result;
}

absl::Status CreateError(absl::string_view message, absl::StatusCode code) {
return absl::Status(code, message);
}

} // namespace runtime_internal

namespace interop_internal {

using ::google::protobuf::Arena;
Expand Down
49 changes: 3 additions & 46 deletions eval/internal/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,12 @@
#ifndef THIRD_PARTY_CEL_CPP_EVAL_INTERNAL_ERRORS_H_
#define THIRD_PARTY_CEL_CPP_EVAL_INTERNAL_ERRORS_H_

#include "google/protobuf/arena.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "runtime/internal/errors.h" // IWYU pragma: export
#include "google/protobuf/arena.h"

namespace cel {
namespace runtime_internal {

constexpr absl::string_view kErrNoMatchingOverload =
"No matching overloads found";
constexpr absl::string_view kErrNoSuchField = "no_such_field";
constexpr absl::string_view kErrNoSuchKey = "Key not found in map";
// Error name for MissingAttributeError indicating that evaluation has
// accessed an attribute whose value is undefined. go/terminal-unknown
constexpr absl::string_view kErrMissingAttribute = "MissingAttributeError: ";
constexpr absl::string_view kPayloadUrlMissingAttributePath =
"missing_attribute_path";
constexpr absl::string_view kPayloadUrlUnknownFunctionResult =
"cel_is_unknown_function_result";

// Exclusive bounds for valid duration values.
constexpr absl::Duration kDurationHigh = absl::Seconds(315576000001);
constexpr absl::Duration kDurationLow = absl::Seconds(-315576000001);

const absl::Status* DurationOverflowError();

// At runtime, no matching overload could be found for a function invocation.
absl::Status CreateNoMatchingOverloadError(absl::string_view fn);

// No such field for struct access.
absl::Status CreateNoSuchFieldError(absl::string_view field);

// No such key for map access.
absl::Status CreateNoSuchKeyError(absl::string_view key);

// A missing attribute was accessed. Attributes may be declared as missing to
// they are not well defined at evaluation time.
absl::Status CreateMissingAttributeError(
absl::string_view missing_attribute_path);

// Function result is unknown. The evaluator may convert this to an
// UnknownValue if enabled.
absl::Status CreateUnknownFunctionResultError(absl::string_view help_message);

// The default error type uses absl::StatusCode::kUnknown. In general, a more
// specific error should be used.
absl::Status CreateError(absl::string_view message,
absl::StatusCode code = absl::StatusCode::kUnknown);

} // namespace runtime_internal

namespace interop_internal {
// Factories for interop error values.
// const pointer Results are arena allocated to support interop with cel::Handle
Expand Down
12 changes: 12 additions & 0 deletions runtime/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ cc_test(
"//internal:testing",
],
)

cc_library(
name = "errors",
srcs = ["errors.cc"],
hdrs = ["errors.h"],
deps = [
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
"@com_google_absl//absl/time",
],
)
64 changes: 64 additions & 0 deletions runtime/internal/errors.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 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.
#include "runtime/internal/errors.h"

#include "absl/status/status.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"

namespace cel::runtime_internal {

const absl::Status* DurationOverflowError() {
static const auto* const kDurationOverflow = new absl::Status(
absl::StatusCode::kInvalidArgument, "Duration is out of range");
return kDurationOverflow;
}

absl::Status CreateNoMatchingOverloadError(absl::string_view fn) {
return absl::UnknownError(
absl::StrCat(kErrNoMatchingOverload, fn.empty() ? "" : " : ", fn));
}

absl::Status CreateNoSuchFieldError(absl::string_view field) {
return absl::Status(
absl::StatusCode::kNotFound,
absl::StrCat(kErrNoSuchField, field.empty() ? "" : " : ", field));
}

absl::Status CreateMissingAttributeError(
absl::string_view missing_attribute_path) {
absl::Status result = absl::InvalidArgumentError(
absl::StrCat(kErrMissingAttribute, missing_attribute_path));
result.SetPayload(kPayloadUrlMissingAttributePath,
absl::Cord(missing_attribute_path));
return result;
}

absl::Status CreateNoSuchKeyError(absl::string_view key) {
return absl::NotFoundError(absl::StrCat(kErrNoSuchKey, " : ", key));
}

absl::Status CreateUnknownFunctionResultError(absl::string_view help_message) {
absl::Status result = absl::UnavailableError(
absl::StrCat("Unknown function result: ", help_message));
result.SetPayload(kPayloadUrlUnknownFunctionResult, absl::Cord("true"));
return result;
}

absl::Status CreateError(absl::string_view message, absl::StatusCode code) {
return absl::Status(code, message);
}

} // namespace cel::runtime_internal
68 changes: 68 additions & 0 deletions runtime/internal/errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2022 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.
//
// Factories and constants for well-known CEL errors.
#ifndef THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_ERRORS_H_
#define THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_ERRORS_H_

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"

namespace cel::runtime_internal {

constexpr absl::string_view kErrNoMatchingOverload =
"No matching overloads found";
constexpr absl::string_view kErrNoSuchField = "no_such_field";
constexpr absl::string_view kErrNoSuchKey = "Key not found in map";
// Error name for MissingAttributeError indicating that evaluation has
// accessed an attribute whose value is undefined. go/terminal-unknown
constexpr absl::string_view kErrMissingAttribute = "MissingAttributeError: ";
constexpr absl::string_view kPayloadUrlMissingAttributePath =
"missing_attribute_path";
constexpr absl::string_view kPayloadUrlUnknownFunctionResult =
"cel_is_unknown_function_result";

// Exclusive bounds for valid duration values.
constexpr absl::Duration kDurationHigh = absl::Seconds(315576000001);
constexpr absl::Duration kDurationLow = absl::Seconds(-315576000001);

const absl::Status* DurationOverflowError();

// At runtime, no matching overload could be found for a function invocation.
absl::Status CreateNoMatchingOverloadError(absl::string_view fn);

// No such field for struct access.
absl::Status CreateNoSuchFieldError(absl::string_view field);

// No such key for map access.
absl::Status CreateNoSuchKeyError(absl::string_view key);

// A missing attribute was accessed. Attributes may be declared as missing to
// they are not well defined at evaluation time.
absl::Status CreateMissingAttributeError(
absl::string_view missing_attribute_path);

// Function result is unknown. The evaluator may convert this to an
// UnknownValue if enabled.
absl::Status CreateUnknownFunctionResultError(absl::string_view help_message);

// The default error type uses absl::StatusCode::kUnknown. In general, a more
// specific error should be used.
absl::Status CreateError(absl::string_view message,
absl::StatusCode code = absl::StatusCode::kUnknown);

} // namespace cel::runtime_internal

#endif // THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_ERRORS_H_
4 changes: 2 additions & 2 deletions runtime/standard/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ cc_library(
"//base:function_adapter",
"//base:handle",
"//base:kind",
"//eval/internal:errors",
"//internal:number",
"//internal:status_macros",
"//runtime:function_registry",
"//runtime:register_function_helper",
"//runtime:runtime_options",
"//runtime/internal:errors",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand Down Expand Up @@ -156,11 +156,11 @@ cc_library(
"//base:data",
"//base:function_adapter",
"//base:handle",
"//eval/internal:errors",
"//internal:status_macros",
"//runtime:function_registry",
"//runtime:register_function_helper",
"//runtime:runtime_options",
"//runtime/internal:errors",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
Expand Down
2 changes: 1 addition & 1 deletion runtime/standard/equality_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
#include "base/values/type_value.h"
#include "base/values/uint_value.h"
#include "base/values/unknown_value.h"
#include "eval/internal/errors.h"
#include "internal/number.h"
#include "internal/status_macros.h"
#include "runtime/function_registry.h"
#include "runtime/internal/errors.h"
#include "runtime/register_function_helper.h"
#include "runtime/runtime_options.h"

Expand Down
2 changes: 1 addition & 1 deletion runtime/standard/logical_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "base/values/bool_value.h"
#include "base/values/error_value.h"
#include "base/values/unknown_value.h"
#include "eval/internal/errors.h"
#include "internal/status_macros.h"
#include "runtime/internal/errors.h"
#include "runtime/register_function_helper.h"

namespace cel {
Expand Down