iceberg-cpp
Loading...
Searching...
No Matches
error_collector.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
24
25#include <string>
26#include <vector>
27
28#include "iceberg/iceberg_export.h"
29#include "iceberg/result.h"
30
31namespace iceberg {
32
33#define ICEBERG_BUILDER_RETURN_IF_ERROR(result) \
34 if (auto&& result_name = result; !result_name) [[unlikely]] { \
35 errors_.emplace_back(std::move(result_name.error())); \
36 return *this; \
37 }
38
39#define ICEBERG_BUILDER_ASSIGN_OR_RETURN_IMPL(result_name, lhs, rexpr) \
40 auto&& result_name = (rexpr); \
41 ICEBERG_BUILDER_RETURN_IF_ERROR(result_name) \
42 lhs = std::move(result_name.value());
43
44#define ICEBERG_BUILDER_ASSIGN_OR_RETURN(lhs, rexpr) \
45 ICEBERG_BUILDER_ASSIGN_OR_RETURN_IMPL( \
46 ICEBERG_ASSIGN_OR_RAISE_NAME(result_, __COUNTER__), lhs, rexpr)
47
48#define ICEBERG_BUILDER_ASSIGN_OR_RETURN_WITH_ERROR_IMPL(result_name, lhs, rexpr, ...) \
49 auto&& result_name = (rexpr); \
50 if (!result_name) [[unlikely]] { \
51 return AddError(ErrorKind::kInvalidArgument, __VA_ARGS__); \
52 } \
53 lhs = std::move(result_name.value());
54
55#define ICEBERG_BUILDER_ASSIGN_OR_RETURN_WITH_ERROR(lhs, rexpr, ...) \
56 ICEBERG_BUILDER_ASSIGN_OR_RETURN_WITH_ERROR_IMPL( \
57 ICEBERG_ASSIGN_OR_RAISE_NAME(result_, __COUNTER__), lhs, rexpr, __VA_ARGS__)
58
59#define ICEBERG_BUILDER_CHECK(expr, ...) \
60 do { \
61 if (!(expr)) [[unlikely]] { \
62 return AddError(ErrorKind::kInvalidArgument, __VA_ARGS__); \
63 } \
64 } while (false)
65
93class ICEBERG_EXPORT ErrorCollector {
94 public:
95 ErrorCollector() = default;
96 virtual ~ErrorCollector() = default;
97
98 ErrorCollector(ErrorCollector&&) = default;
99 ErrorCollector& operator=(ErrorCollector&&) = default;
100
101 ErrorCollector(const ErrorCollector&) = default;
102 ErrorCollector& operator=(const ErrorCollector&) = default;
103
111 template <typename... Args>
112 auto& AddError(this auto& self, ErrorKind kind, const std::format_string<Args...> fmt,
113 Args&&... args) {
114 self.errors_.emplace_back(kind, std::format(fmt, std::forward<Args>(args)...));
115 return self;
116 }
117
126 auto& AddError(this auto& self, Error err) {
127 self.errors_.push_back(std::move(err));
128 return self;
129 }
130
141 auto& AddError(this auto& self, std::unexpected<Error> err) {
142 self.errors_.push_back(std::move(err.error()));
143 return self;
144 }
145
149 [[nodiscard]] bool has_errors() const { return !errors_.empty(); }
150
154 [[nodiscard]] size_t error_count() const { return errors_.size(); }
155
164 [[nodiscard]] Status CheckErrors() const {
165 if (!errors_.empty()) {
166 std::string error_msg = "Validation failed due to the following errors:\n";
167 for (const auto& [kind, message] : errors_) {
168 error_msg += " - " + message + "\n";
169 }
170 return ValidationFailed("{}", error_msg);
171 }
172 return {};
173 }
174
179 void ClearErrors() { errors_.clear(); }
180
182 [[nodiscard]] const std::vector<Error>& errors() const { return errors_; }
183
184 protected:
185 std::vector<Error> errors_;
186};
187
188} // namespace iceberg
Base class for collecting errors in the builder pattern.
Definition error_collector.h:93
const std::vector< Error > & errors() const
Get read-only access to all collected errors.
Definition error_collector.h:182
Status CheckErrors() const
Check for accumulated errors and return them if any exist.
Definition error_collector.h:164
void ClearErrors()
Clear all accumulated errors.
Definition error_collector.h:179
auto & AddError(this auto &self, ErrorKind kind, const std::format_string< Args... > fmt, Args &&... args)
Add a specific error and return reference to derived class.
Definition error_collector.h:112
auto & AddError(this auto &self, std::unexpected< Error > err)
Add an unexpected result's error and return reference to derived class.
Definition error_collector.h:141
auto & AddError(this auto &self, Error err)
Add an existing error object and return reference to derived class.
Definition error_collector.h:126
bool has_errors() const
Check if any errors have been collected.
Definition error_collector.h:149
size_t error_count() const
Get the number of errors collected.
Definition error_collector.h:154
Error with a kind and a message.
Definition result.h:66