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