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

cel/cpp: Improve error message for string to int conversion. #867

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions runtime/standard/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/time",
],
)
Expand Down
19 changes: 10 additions & 9 deletions runtime/standard/type_conversion_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "absl/status/statusor.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "base/builtins.h"
Expand Down Expand Up @@ -85,7 +86,8 @@ absl::Status RegisterIntConversionFunctions(FunctionRegistry& registry,
int64_t result;
if (!absl::SimpleAtoi(s.ToString(), &result)) {
return value_factory.CreateErrorValue(
absl::InvalidArgumentError("cannot convert string to int"));
absl::InvalidArgumentError(absl::StrFormat(
"cannot convert string '%s' to int", s.ToString())));
}
return value_factory.CreateIntValue(result);
},
Expand Down Expand Up @@ -330,14 +332,13 @@ absl::Status RegisterTimeConversionFunctions(FunctionRegistry& registry,
CEL_RETURN_IF_ERROR(status);

// timestamp conversion from int.
status =
UnaryFunctionAdapter<Value, int64_t>::RegisterGlobalOverload(
cel::builtin::kTimestamp,
[](ValueManager& value_factory, int64_t epoch_seconds) -> Value {
return value_factory.CreateUncheckedTimestampValue(
absl::FromUnixSeconds(epoch_seconds));
},
registry);
status = UnaryFunctionAdapter<Value, int64_t>::RegisterGlobalOverload(
cel::builtin::kTimestamp,
[](ValueManager& value_factory, int64_t epoch_seconds) -> Value {
return value_factory.CreateUncheckedTimestampValue(
absl::FromUnixSeconds(epoch_seconds));
},
registry);
CEL_RETURN_IF_ERROR(status);

// timestamp() conversion from string.
Expand Down