iceberg-cpp
Loading...
Searching...
No Matches
result.h
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
22#include <concepts>
23#include <expected>
24#include <format>
25#include <string>
26#include <type_traits>
27
28#include "iceberg/iceberg_export.h"
29
30namespace iceberg {
31
33enum class ErrorKind {
34 kAlreadyExists,
35 kAuthenticationFailed,
36 kBadRequest,
37 kCommitFailed,
38 kCommitStateUnknown,
39 kDecompressError,
40 kForbidden,
41 kInternalServerError,
42 kInvalid, // For general invalid errors
43 kInvalidArgument,
44 kInvalidArrowData,
45 kInvalidExpression,
46 kInvalidManifest,
47 kInvalidManifestList,
48 kInvalidSchema,
49 kIOError,
50 kJsonParseError,
51 kNamespaceNotEmpty,
52 kNoSuchNamespace,
53 kNoSuchPlanId,
54 kNoSuchPlanTask,
55 kNoSuchTable,
56 kNoSuchWarehouse,
57 kNoSuchView,
58 kNotAllowed,
59 kNotAuthorized,
60 kNotFound,
61 kNotImplemented,
62 kNotSupported,
63 kRestError,
64 kRetryableValidationFailed,
65 kServiceUnavailable,
66 kTokenExpired,
67 kUnknownError,
68 kValidationFailed,
69};
70
72struct ICEBERG_EXPORT [[nodiscard]] Error {
73 ErrorKind kind;
74 std::string message;
75};
76
78template <typename T>
80 using type = Error;
81};
82
84template <typename T, typename E = typename DefaultError<T>::type>
85using Result = std::expected<T, E>;
86
87using Status = Result<void>;
88
90#define DEFINE_ERROR_FUNCTION(name) \
91 template <typename... Args> \
92 inline auto name(const std::format_string<Args...> fmt, Args&&... args) \
93 -> std::unexpected<Error> { \
94 return std::unexpected<Error>( \
95 {ErrorKind::k##name, std::format(fmt, std::forward<Args>(args)...)}); \
96 } \
97 inline auto name(std::string message) -> std::unexpected<Error> { \
98 return std::unexpected<Error>({ErrorKind::k##name, std::move(message)}); \
99 }
100
101DEFINE_ERROR_FUNCTION(AlreadyExists)
102DEFINE_ERROR_FUNCTION(AuthenticationFailed)
103DEFINE_ERROR_FUNCTION(BadRequest)
104DEFINE_ERROR_FUNCTION(CommitFailed)
105DEFINE_ERROR_FUNCTION(CommitStateUnknown)
106DEFINE_ERROR_FUNCTION(DecompressError)
107DEFINE_ERROR_FUNCTION(Forbidden)
108DEFINE_ERROR_FUNCTION(InternalServerError)
109DEFINE_ERROR_FUNCTION(Invalid)
110DEFINE_ERROR_FUNCTION(InvalidArgument)
111DEFINE_ERROR_FUNCTION(InvalidArrowData)
112DEFINE_ERROR_FUNCTION(InvalidExpression)
113DEFINE_ERROR_FUNCTION(InvalidManifest)
114DEFINE_ERROR_FUNCTION(InvalidManifestList)
115DEFINE_ERROR_FUNCTION(InvalidSchema)
116DEFINE_ERROR_FUNCTION(IOError)
117DEFINE_ERROR_FUNCTION(JsonParseError)
118DEFINE_ERROR_FUNCTION(NamespaceNotEmpty)
119DEFINE_ERROR_FUNCTION(NoSuchNamespace)
120DEFINE_ERROR_FUNCTION(NoSuchPlanId)
121DEFINE_ERROR_FUNCTION(NoSuchPlanTask)
122DEFINE_ERROR_FUNCTION(NoSuchTable)
123DEFINE_ERROR_FUNCTION(NoSuchWarehouse)
124DEFINE_ERROR_FUNCTION(NoSuchView)
125DEFINE_ERROR_FUNCTION(NotAllowed)
126DEFINE_ERROR_FUNCTION(NotAuthorized)
127DEFINE_ERROR_FUNCTION(NotFound)
128DEFINE_ERROR_FUNCTION(NotImplemented)
129DEFINE_ERROR_FUNCTION(NotSupported)
130DEFINE_ERROR_FUNCTION(RestError)
131DEFINE_ERROR_FUNCTION(RetryableValidationFailed)
132DEFINE_ERROR_FUNCTION(ServiceUnavailable)
133DEFINE_ERROR_FUNCTION(TokenExpired)
134DEFINE_ERROR_FUNCTION(UnknownError)
135DEFINE_ERROR_FUNCTION(ValidationFailed)
136
137#undef DEFINE_ERROR_FUNCTION
138
139template <typename T>
140concept AsResult = std::derived_from<std::remove_cvref_t<T>,
141 Result<typename std::remove_cvref_t<T>::value_type>>;
142
143template <AsResult T>
144using ResultValueT = typename std::remove_cvref_t<T>::value_type;
145
146} // namespace iceberg
Definition result.h:140
/brief Default error trait
Definition result.h:79
Error with a kind and a message.
Definition result.h:72