Skip to content

Commit 46cc8dd

Browse files
committed
CXXCBC-748: Create core error type
1 parent 477437c commit 46cc8dd

File tree

8 files changed

+86
-44
lines changed

8 files changed

+86
-44
lines changed

core/error.hxx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2+
/*
3+
* Copyright 2022-Present Couchbase, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
6+
* except in compliance with the License. You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under
11+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12+
* ANY KIND, either express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
#pragma once
17+
18+
#include <string>
19+
#include <system_error>
20+
21+
namespace couchbase::core
22+
{
23+
struct error {
24+
std::error_code ec;
25+
std::string message;
26+
};
27+
} // namespace couchbase::core

core/impl/error.cxx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "error.hxx"
1919

20+
#include "core/error.hxx"
2021
#include "core/error_context/analytics.hxx"
2122
#include "core/error_context/analytics_json.hxx"
2223
#include "core/error_context/http.hxx"
@@ -116,7 +117,7 @@ error::operator==(const couchbase::error& other) const -> bool
116117
namespace core::impl
117118
{
118119
auto
119-
make_error(const core::error_context::query& core_ctx) -> error
120+
make_error(const core::error_context::query& core_ctx) -> couchbase::error
120121
{
121122
if (!core_ctx.ec) {
122123
return {};
@@ -125,7 +126,7 @@ make_error(const core::error_context::query& core_ctx) -> error
125126
}
126127

127128
auto
128-
make_error(const query_error_context& core_ctx) -> error
129+
make_error(const query_error_context& core_ctx) -> couchbase::error
129130
{
130131
if (!core_ctx.ec()) {
131132
return {};
@@ -134,7 +135,7 @@ make_error(const query_error_context& core_ctx) -> error
134135
}
135136

136137
auto
137-
make_error(const core::error_context::search& core_ctx) -> error
138+
make_error(const core::error_context::search& core_ctx) -> couchbase::error
138139
{
139140
if (!core_ctx.ec) {
140141
return {};
@@ -143,7 +144,7 @@ make_error(const core::error_context::search& core_ctx) -> error
143144
}
144145

145146
auto
146-
make_error(const core::error_context::analytics& core_ctx) -> error
147+
make_error(const core::error_context::analytics& core_ctx) -> couchbase::error
147148
{
148149
if (!core_ctx.ec) {
149150
return {};
@@ -152,7 +153,7 @@ make_error(const core::error_context::analytics& core_ctx) -> error
152153
}
153154

154155
auto
155-
make_error(const core::error_context::http& core_ctx) -> error
156+
make_error(const core::error_context::http& core_ctx) -> couchbase::error
156157
{
157158
if (!core_ctx.ec) {
158159
return {};
@@ -161,7 +162,7 @@ make_error(const core::error_context::http& core_ctx) -> error
161162
}
162163

163164
auto
164-
make_error(const couchbase::core::key_value_error_context& core_ctx) -> error
165+
make_error(const couchbase::core::key_value_error_context& core_ctx) -> couchbase::error
165166
{
166167
if (!core_ctx.ec()) {
167168
return {};
@@ -170,7 +171,7 @@ make_error(const couchbase::core::key_value_error_context& core_ctx) -> error
170171
}
171172

172173
auto
173-
make_error(const couchbase::core::subdocument_error_context& core_ctx) -> error
174+
make_error(const couchbase::core::subdocument_error_context& core_ctx) -> couchbase::error
174175
{
175176
if (!core_ctx.ec()) {
176177
return {};
@@ -179,15 +180,15 @@ make_error(const couchbase::core::subdocument_error_context& core_ctx) -> error
179180
}
180181

181182
auto
182-
make_error(const couchbase::core::transaction_error_context& ctx) -> error
183+
make_error(const couchbase::core::transaction_error_context& ctx) -> couchbase::error
183184
{
184185
return { ctx.ec(), {}, {}, { ctx.cause() } };
185186
}
186187

187188
auto
188-
make_error(const core::transactions::op_exception& exc) -> error
189+
make_error(const core::transactions::op_exception& exc) -> couchbase::error
189190
{
190-
std::optional<error> cause;
191+
std::optional<couchbase::error> cause;
191192
if (std::holds_alternative<key_value_error_context>(exc.ctx().cause())) {
192193
cause = make_error(std::get<key_value_error_context>(exc.ctx().cause()));
193194
}
@@ -196,15 +197,15 @@ make_error(const core::transactions::op_exception& exc) -> error
196197
}
197198

198199
if (cause.has_value()) {
199-
return error{
200+
return couchbase::error{
200201
transaction_op_errc_from_external_exception(exc.cause()),
201202
exc.what(),
202203
{},
203204
cause.value(),
204205
};
205206
}
206207

207-
return error{
208+
return couchbase::error{
208209
transaction_op_errc_from_external_exception(exc.cause()),
209210
exc.what(),
210211
{},
@@ -218,7 +219,13 @@ make_error(const couchbase::core::transactions::transaction_operation_failed& co
218219
return { couchbase::errc::transaction_op::transaction_op_failed,
219220
core_tof.what(),
220221
internal_error_context::build_error_context(tao::json::empty_object, core_tof),
221-
error(transaction_op_errc_from_external_exception(core_tof.cause())) };
222+
couchbase::error(transaction_op_errc_from_external_exception(core_tof.cause())) };
223+
}
224+
225+
auto
226+
make_error(const core::error& err) -> couchbase::error
227+
{
228+
return { err.ec, err.message };
222229
}
223230
} // namespace core::impl
224231
} // namespace couchbase

core/impl/error.hxx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#pragma once
1919

20+
#include "core/error.hxx"
2021
#include "core/error_context/key_value_error_context.hxx"
2122
#include "core/error_context/query_error_context.hxx"
2223
#include "core/error_context/subdocument_error_context.hxx"
@@ -33,32 +34,36 @@
3334
namespace couchbase::core::impl
3435
{
3536
auto
36-
make_error(const core::error_context::query& core_ctx) -> error;
37+
make_error(const core::error_context::query& core_ctx) -> couchbase::error;
3738

3839
auto
39-
make_error(const core::error_context::search& core_ctx) -> error;
40+
make_error(const core::error_context::search& core_ctx) -> couchbase::error;
4041

4142
auto
42-
make_error(const core::error_context::analytics& core_ctx) -> error;
43+
make_error(const core::error_context::analytics& core_ctx) -> couchbase::error;
4344

4445
auto
45-
make_error(const core::error_context::http& core_ctx) -> error;
46+
make_error(const core::error_context::http& core_ctx) -> couchbase::error;
4647

4748
auto
48-
make_error(const couchbase::core::key_value_error_context& core_ctx) -> error;
49+
make_error(const couchbase::core::key_value_error_context& core_ctx) -> couchbase::error;
4950

5051
auto
51-
make_error(const couchbase::core::subdocument_error_context& core_ctx) -> error;
52+
make_error(const couchbase::core::subdocument_error_context& core_ctx) -> couchbase::error;
5253

5354
auto
54-
make_error(const couchbase::core::query_error_context& core_ctx) -> error;
55+
make_error(const couchbase::core::query_error_context& core_ctx) -> couchbase::error;
5556

5657
auto
57-
make_error(const couchbase::core::transaction_error_context& core_ctx) -> error;
58+
make_error(const couchbase::core::transaction_error_context& core_ctx) -> couchbase::error;
5859

5960
auto
60-
make_error(const couchbase::core::transactions::op_exception& exc) -> error;
61+
make_error(const couchbase::core::transactions::op_exception& exc) -> couchbase::error;
6162

6263
auto
63-
make_error(const couchbase::core::transactions::transaction_operation_failed& core_tof) -> error;
64+
make_error(const couchbase::core::transactions::transaction_operation_failed& core_tof)
65+
-> couchbase::error;
66+
67+
auto
68+
make_error(const core::error& err) -> couchbase::error;
6469
} // namespace couchbase::core::impl

core/impl/get_all_replicas.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private:
7070
};
7171

7272
using movable_get_all_replicas_handler =
73-
utils::movable_function<void(error, get_all_replicas_result)>;
73+
utils::movable_function<void(couchbase::error, get_all_replicas_result)>;
7474

7575
void
7676
initiate_get_all_replicas_operation(std::shared_ptr<cluster> core,

core/impl/get_any_replica.hxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ private:
6565
std::optional<std::chrono::milliseconds> timeout_{};
6666
};
6767

68-
using movable_get_any_replica_handler = utils::movable_function<void(error, get_replica_result)>;
68+
using movable_get_any_replica_handler =
69+
utils::movable_function<void(couchbase::error, get_replica_result)>;
6970

7071
void
7172
initiate_get_any_replica_operation(std::shared_ptr<cluster> core,

core/transactions/attempt_context_impl.cxx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,8 @@ void
634634
attempt_context_impl::get_multi(
635635
const std::vector<couchbase::transactions::transaction_get_multi_spec>& specs,
636636
const couchbase::transactions::transaction_get_multi_options& options,
637-
std::function<void(error, std::optional<couchbase::transactions::transaction_get_multi_result>)>&&
638-
cb)
637+
std::function<void(couchbase::error,
638+
std::optional<couchbase::transactions::transaction_get_multi_result>)>&& cb)
639639
{
640640
std::vector<core::document_id> ids;
641641
ids.reserve(specs.size());
@@ -666,17 +666,19 @@ auto
666666
attempt_context_impl::get_multi(
667667
const std::vector<couchbase::transactions::transaction_get_multi_spec>& specs,
668668
const couchbase::transactions::transaction_get_multi_options& options)
669-
-> std::pair<error, std::optional<couchbase::transactions::transaction_get_multi_result>>
669+
-> std::pair<couchbase::error,
670+
std::optional<couchbase::transactions::transaction_get_multi_result>>
670671
{
671672
auto barrier = std::make_shared<std::promise<
672-
std::pair<error, std::optional<couchbase::transactions::transaction_get_multi_result>>>>();
673+
std::pair<couchbase::error,
674+
std::optional<couchbase::transactions::transaction_get_multi_result>>>>();
673675
auto f = barrier->get_future();
674-
get_multi(
675-
specs,
676-
options,
677-
[barrier](error err, std::optional<couchbase::transactions::transaction_get_multi_result> res) {
678-
return barrier->set_value(std::make_pair(std::move(err), std::move(res)));
679-
});
676+
get_multi(specs,
677+
options,
678+
[barrier](couchbase::error err,
679+
std::optional<couchbase::transactions::transaction_get_multi_result> res) {
680+
return barrier->set_value(std::make_pair(std::move(err), std::move(res)));
681+
});
680682
return f.get();
681683
}
682684

@@ -749,7 +751,7 @@ attempt_context_impl::get_multi_replicas_from_preferred_server_group(
749751
const couchbase::transactions::transaction_get_multi_replicas_from_preferred_server_group_options&
750752
options,
751753
std::function<
752-
void(error,
754+
void(couchbase::error,
753755
std::optional<couchbase::transactions::
754756
transaction_get_multi_replicas_from_preferred_server_group_result>)>&& cb)
755757
{
@@ -790,20 +792,20 @@ attempt_context_impl::get_multi_replicas_from_preferred_server_group(
790792
const couchbase::transactions::transaction_get_multi_replicas_from_preferred_server_group_options&
791793
options)
792794
-> std::pair<
793-
error,
795+
couchbase::error,
794796
std::optional<
795797
couchbase::transactions::transaction_get_multi_replicas_from_preferred_server_group_result>>
796798
{
797799
auto barrier = std::make_shared<std::promise<std::pair<
798-
error,
800+
couchbase::error,
799801
std::optional<couchbase::transactions::
800802
transaction_get_multi_replicas_from_preferred_server_group_result>>>>();
801803
auto f = barrier->get_future();
802804
get_multi_replicas_from_preferred_server_group(
803805
specs,
804806
options,
805807
[barrier](
806-
error err,
808+
couchbase::error err,
807809
std::optional<
808810
couchbase::transactions::transaction_get_multi_replicas_from_preferred_server_group_result>
809811
res) {

core/transactions/attempt_context_impl.hxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,12 @@ public:
357357
std::optional<transaction_get_multi_result>)>&& cb) override;
358358
auto get_multi(const std::vector<couchbase::transactions::transaction_get_multi_spec>& specs,
359359
const couchbase::transactions::transaction_get_multi_options& options)
360-
-> std::pair<error,
360+
-> std::pair<couchbase::error,
361361
std::optional<couchbase::transactions::transaction_get_multi_result>> override;
362362
void get_multi(
363363
const std::vector<couchbase::transactions::transaction_get_multi_spec>& specs,
364364
const couchbase::transactions::transaction_get_multi_options& options,
365-
std::function<void(error,
365+
std::function<void(couchbase::error,
366366
std::optional<couchbase::transactions::transaction_get_multi_result>)>&& cb)
367367
override;
368368

@@ -384,7 +384,7 @@ public:
384384
const couchbase::transactions::
385385
transaction_get_multi_replicas_from_preferred_server_group_options& options)
386386
-> std::pair<
387-
error,
387+
couchbase::error,
388388
std::optional<couchbase::transactions::
389389
transaction_get_multi_replicas_from_preferred_server_group_result>> override;
390390
void get_multi_replicas_from_preferred_server_group(
@@ -394,7 +394,7 @@ public:
394394
const couchbase::transactions::
395395
transaction_get_multi_replicas_from_preferred_server_group_options& options,
396396
std::function<void(
397-
error,
397+
couchbase::error,
398398
std::optional<couchbase::transactions::
399399
transaction_get_multi_replicas_from_preferred_server_group_result>)>&& cb)
400400
override;

core/transactions/transactions.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ transactions::run(const couchbase::transactions::transaction_options& config, lo
204204
auto
205205
transactions::run(couchbase::transactions::txn_logic&& code,
206206
const couchbase::transactions::transaction_options& config)
207-
-> std::pair<error, couchbase::transactions::transaction_result>
207+
-> std::pair<couchbase::error, couchbase::transactions::transaction_result>
208208
{
209209
try {
210210
return { {}, wrap_public_api_run(*this, config, max_attempts_, std::move(code)) };

0 commit comments

Comments
 (0)