|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_ERROR_METADATA_H |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_ERROR_METADATA_H |
| 17 | + |
| 18 | +#include "google/cloud/version.h" |
| 19 | +#include "absl/strings/string_view.h" |
| 20 | +#include <string> |
| 21 | +#include <utility> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace google { |
| 25 | +namespace cloud { |
| 26 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 27 | +namespace internal { |
| 28 | + |
| 29 | +/** |
| 30 | + * A (relatively) lightweight data structure to pass error metadata across |
| 31 | + * implementation functions. |
| 32 | + * |
| 33 | + * Sometimes we want to provide additional context about errors. The original |
| 34 | + * motivation is credential file parsing. These files can be fairly complex, |
| 35 | + * and parsing requires many functions that only need the *contents* of the |
| 36 | + * file to parse it, but may want to show the filename, the start of the |
| 37 | + * parsing call tree, and maybe some key intermediate callers. |
| 38 | + * |
| 39 | + * This class can be used to pass these additional parameters as needed, maybe |
| 40 | + * growing as parsing partially succeeds, and if there is an error the data |
| 41 | + * can be included as part of the message (or `google::cloud::ErrorInfo`). |
| 42 | + * |
| 43 | + * @par Example |
| 44 | + * @code |
| 45 | + * namespace google::cloud::internal { |
| 46 | + * StatusOr<Foo> ParseFooFile(std::string filename) { |
| 47 | + * ErrorContext ec{ |
| 48 | + * {"filename", filename}, |
| 49 | + * {"origin", __func__}, |
| 50 | + * }; |
| 51 | + * std::ifstream is(filename); |
| 52 | + * auto contents = std::string{std::istreambuf_iterator<char>{is.buf()},{ }}; |
| 53 | + * if (is.bad()) { |
| 54 | + * return Status( |
| 55 | + * StatusCode::kInvalidArgument, |
| 56 | + * Format("cannot read file", error_context)); |
| 57 | + * } |
| 58 | + * return ParseFooFileContents(std::move(contents), std::move(ec)); |
| 59 | + * } |
| 60 | + * |
| 61 | + * StatusOr<Foo> ParseFooFileContents(std::string contents, ErrorContext ec) { |
| 62 | + * // Do some stuff |
| 63 | + * if (has_bar()) return ParseFooFileContentsWithBar( |
| 64 | + * .., std::move(error_context)); |
| 65 | + * // do more stuff |
| 66 | + * if (bad()) return Status( |
| 67 | + * StatusCode::kInvalidArgument, |
| 68 | + * Format("badness parsing thingamajig", ec)); |
| 69 | + * // all good |
| 70 | + * return Foo{...}; |
| 71 | + * } |
| 72 | + * @endcode |
| 73 | + */ |
| 74 | +class ErrorContext { |
| 75 | + public: |
| 76 | + using Container = std::vector<std::pair<std::string, std::string>>; |
| 77 | + |
| 78 | + ErrorContext() = default; |
| 79 | + explicit ErrorContext(Container m) : metadata_(std::move(m)) {} |
| 80 | + |
| 81 | + ErrorContext(ErrorContext const&) = default; |
| 82 | + ErrorContext& operator=(ErrorContext const&) = default; |
| 83 | + ErrorContext(ErrorContext&&) = default; |
| 84 | + ErrorContext& operator=(ErrorContext&&) = default; |
| 85 | + |
| 86 | + template <typename... A> |
| 87 | + Container::reference emplace_back(A&&... a) { |
| 88 | + return metadata_.emplace_back(std::forward<A>(a)...); |
| 89 | + } |
| 90 | + |
| 91 | + void push_back(Container::value_type p) { |
| 92 | + return metadata_.push_back(std::move(p)); |
| 93 | + } |
| 94 | + |
| 95 | + bool empty() const { return metadata_.empty(); } |
| 96 | + |
| 97 | + Container::const_iterator begin() const { return metadata_.begin(); } |
| 98 | + |
| 99 | + Container::const_iterator end() const { return metadata_.end(); } |
| 100 | + |
| 101 | + private: |
| 102 | + Container metadata_; |
| 103 | +}; |
| 104 | + |
| 105 | +std::string Format(absl::string_view message, ErrorContext const& context); |
| 106 | + |
| 107 | +} // namespace internal |
| 108 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 109 | +} // namespace cloud |
| 110 | +} // namespace google |
| 111 | + |
| 112 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_ERROR_METADATA_H |
0 commit comments