-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy patherror_value.cc
More file actions
250 lines (202 loc) · 7.41 KB
/
error_value.cc
File metadata and controls
250 lines (202 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2023 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 <cstddef>
#include <new>
#include <string>
#include <utility>
#include "absl/base/no_destructor.h"
#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/strings/cord.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "common/type.h"
#include "common/value.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel {
namespace {
std::string ErrorDebugString(const absl::Status& value) {
ABSL_DCHECK(!value.ok()) << "use of moved-from ErrorValue";
return value.ToString(absl::StatusToStringMode::kWithEverything);
}
const absl::Status& DefaultErrorValue() {
static const absl::NoDestructor<absl::Status> value(
absl::UnknownError("unknown error"));
return *value;
}
} // namespace
ErrorValue::ErrorValue() : ErrorValue(DefaultErrorValue()) {}
ErrorValue NoSuchFieldError(absl::string_view field) {
return ErrorValue(absl::NotFoundError(
absl::StrCat("no_such_field", field.empty() ? "" : " : ", field)));
}
ErrorValue NoSuchKeyError(absl::string_view key) {
return ErrorValue(
absl::NotFoundError(absl::StrCat("Key not found in map : ", key)));
}
ErrorValue NoSuchTypeError(absl::string_view type) {
return ErrorValue(
absl::NotFoundError(absl::StrCat("type not found: ", type)));
}
ErrorValue DuplicateKeyError() {
return ErrorValue(absl::AlreadyExistsError("duplicate key in map"));
}
ErrorValue TypeConversionError(absl::string_view from, absl::string_view to) {
return ErrorValue(absl::InvalidArgumentError(
absl::StrCat("type conversion error from '", from, "' to '", to, "'")));
}
ErrorValue TypeConversionError(const Type& from, const Type& to) {
return TypeConversionError(from.DebugString(), to.DebugString());
}
ErrorValue IndexOutOfBoundsError(size_t index) {
return ErrorValue(
absl::InvalidArgumentError(absl::StrCat("index out of bounds: ", index)));
}
ErrorValue IndexOutOfBoundsError(ptrdiff_t index) {
return ErrorValue(
absl::InvalidArgumentError(absl::StrCat("index out of bounds: ", index)));
}
ErrorValue IntDivisionByZeroError() {
return ErrorValue(absl::InvalidArgumentError("int division by zero"));
}
ErrorValue UintDivisionByZeroError() {
return ErrorValue(absl::InvalidArgumentError("uint division by zero"));
}
ErrorValue IntModuloByZeroError() {
return ErrorValue(absl::InvalidArgumentError("int modulo by zero"));
}
ErrorValue UintModuloByZeroError() {
return ErrorValue(absl::InvalidArgumentError("uint modulo by zero"));
}
ErrorValue IntOverflowError() {
return ErrorValue(absl::OutOfRangeError("int overflow"));
}
ErrorValue UintOverflowError() {
return ErrorValue(absl::OutOfRangeError("uint overflow"));
}
ErrorValue DurationOverflowError() {
return ErrorValue(absl::OutOfRangeError("duration overflow"));
}
ErrorValue TimestampOverflowError() {
return ErrorValue(absl::OutOfRangeError("timestamp overflow"));
}
ErrorValue DoubleToIntOutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("double out of int range"));
}
ErrorValue DoubleToUintOutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("double out of uint range"));
}
ErrorValue IntToUintOutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("int out of uint range"));
}
ErrorValue UintToIntOutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("uint out of int range"));
}
ErrorValue Int64ToInt32OutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("int64 out of int32_t range"));
}
ErrorValue Uint64ToUint32OutOfRangeError() {
return ErrorValue(absl::OutOfRangeError("uint64 out of uint32_t range"));
}
bool IsNoSuchField(const ErrorValue& value) {
return absl::IsNotFound(value.NativeValue()) &&
absl::StartsWith(value.NativeValue().message(), "no_such_field");
}
bool IsNoSuchKey(const ErrorValue& value) {
return absl::IsNotFound(value.NativeValue()) &&
absl::StartsWith(value.NativeValue().message(),
"Key not found in map");
}
std::string ErrorValue::DebugString() const {
return ErrorDebugString(NativeValue());
}
absl::Status ErrorValue::SerializeTo(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<absl::Cord*> value) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(value != nullptr);
ABSL_DCHECK(*this);
return absl::FailedPreconditionError(
absl::StrCat(GetTypeName(), " is unserializable"));
}
absl::Status ErrorValue::ConvertToJson(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Message*> json) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(json != nullptr);
ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
ABSL_DCHECK(*this);
return absl::FailedPreconditionError(
absl::StrCat(GetTypeName(), " is not convertable to JSON"));
}
absl::Status ErrorValue::Equal(
const Value& other,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(arena != nullptr);
ABSL_DCHECK(result != nullptr);
ABSL_DCHECK(*this);
*result = FalseValue();
return absl::OkStatus();
}
ErrorValue ErrorValue::Clone(absl::Nonnull<google::protobuf::Arena*> arena) const {
ABSL_DCHECK(arena != nullptr);
ABSL_DCHECK(*this);
if (arena_ == nullptr || arena_ != arena) {
return ErrorValue(arena,
google::protobuf::Arena::Create<absl::Status>(arena, ToStatus()));
}
return *this;
}
absl::Status ErrorValue::ToStatus() const& {
ABSL_DCHECK(*this);
if (arena_ == nullptr) {
return *std::launder(
reinterpret_cast<const absl::Status*>(&status_.val[0]));
}
return *status_.ptr;
}
absl::Status ErrorValue::ToStatus() && {
ABSL_DCHECK(*this);
if (arena_ == nullptr) {
return std::move(
*std::launder(reinterpret_cast<absl::Status*>(&status_.val[0])));
}
return *status_.ptr;
}
ErrorValue::operator bool() const {
if (arena_ == nullptr) {
return !std::launder(reinterpret_cast<const absl::Status*>(&status_.val[0]))
->ok();
}
return status_.ptr != nullptr && !status_.ptr->ok();
}
void swap(ErrorValue& lhs, ErrorValue& rhs) noexcept {
ErrorValue tmp(std::move(lhs));
lhs = std::move(rhs);
rhs = std::move(tmp);
}
} // namespace cel