|
| 1 | +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2020-Present Couchbase, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <couchbase/key_value_error_context.hxx> |
| 19 | + |
| 20 | +#include <couchbase/fmt/cas.hxx> |
| 21 | +#include <couchbase/fmt/key_value_error_map_attribute.hxx> |
| 22 | +#include <couchbase/fmt/key_value_status_code.hxx> |
| 23 | +#include <couchbase/fmt/retry_reason.hxx> |
| 24 | + |
| 25 | +#include <tao/json/to_string.hpp> |
| 26 | + |
| 27 | +namespace couchbase |
| 28 | +{ |
| 29 | +auto |
| 30 | +key_value_error_context::to_json() const -> std::string |
| 31 | +{ |
| 32 | + tao::json::value json = { |
| 33 | + { |
| 34 | + "ec", |
| 35 | + tao::json::value{ |
| 36 | + { "value", ec().value() }, |
| 37 | + { "message", ec().message() }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { "operation_id", operation_id() }, |
| 41 | + { "id", id_ }, |
| 42 | + { "bucket", bucket_ }, |
| 43 | + { "scope", scope_ }, |
| 44 | + { "collection", collection_ }, |
| 45 | + }; |
| 46 | + |
| 47 | + if (auto val = retry_attempts(); val > 0) { |
| 48 | + json["retry_attempts"] = val; |
| 49 | + } |
| 50 | + if (opaque_ > 0) { |
| 51 | + json["opaque"] = opaque_; |
| 52 | + } |
| 53 | + |
| 54 | + if (!cas_.empty()) { |
| 55 | + json["cas"] = fmt::format("{}", cas_); |
| 56 | + } |
| 57 | + |
| 58 | + if (const auto& reasons = retry_reasons(); !reasons.empty()) { |
| 59 | + tao::json::value reasons_json = tao::json::empty_array; |
| 60 | + for (const auto& reason : reasons) { |
| 61 | + reasons_json.emplace_back(fmt::format("{}", reason)); |
| 62 | + } |
| 63 | + json["retry_reasons"] = reasons_json; |
| 64 | + } |
| 65 | + if (const auto& val = last_dispatched_from(); val.has_value()) { |
| 66 | + json["last_dispatched_from"] = val.value(); |
| 67 | + } |
| 68 | + if (const auto& val = last_dispatched_to(); val.has_value()) { |
| 69 | + json["last_dispatched_to"] = val.value(); |
| 70 | + } |
| 71 | + if (const auto& val = status_code_; val.has_value()) { |
| 72 | + json["status_code"] = fmt::format("{}", val.value()); |
| 73 | + } |
| 74 | + if (const auto& val = extended_error_info_; val.has_value()) { |
| 75 | + json["extended_error_info"] = tao::json::value{ |
| 76 | + { "context", val->context() }, |
| 77 | + { "reference", val->reference() }, |
| 78 | + }; |
| 79 | + } |
| 80 | + if (const auto& val = error_map_info_; val.has_value()) { |
| 81 | + tao::json::value info{ |
| 82 | + { "code", val->code() }, |
| 83 | + { "name", val->name() }, |
| 84 | + { "description", val->description() }, |
| 85 | + }; |
| 86 | + if (const auto& attributes = val->attributes(); !attributes.empty()) { |
| 87 | + tao::json::value attrs_json = tao::json::empty_array; |
| 88 | + for (const auto& attr : attributes) { |
| 89 | + attrs_json.emplace_back(fmt::format("{}", attr)); |
| 90 | + } |
| 91 | + info["attributes"] = attrs_json; |
| 92 | + } |
| 93 | + json["error_map_info"] = info; |
| 94 | + } |
| 95 | + |
| 96 | + return tao::json::to_string(json, 2); |
| 97 | +} |
| 98 | +} // namespace couchbase |
0 commit comments