Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: GCS parse "error info" from JSON when available #7654

Merged
merged 4 commits into from
Nov 23, 2021

Conversation

devjgm
Copy link
Contributor

@devjgm devjgm commented Nov 23, 2021

Fixes: #7644

Follows https://cloud.google.com/apis/design/errors#http_mapping and parses the "error info" json object from responses when it's available, and uses it to set the fields of the returned g::c::Status.

Note to reviewers: I'm new to using nlohmann::json, so please review that code closely and tell me what I'm doing wrong. I may be doing things very inefficiently or not following the right best practices. Please let me know.


This change is Reviewable

Fixes: googleapis#7644

Please review this nlohmann code closely, because I've never used it and
I may be doing things the least optimal way.
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Nov 23, 2021
@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: 29d1ae7232ccfde7db8e1024582b4632c309ca05

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@codecov
Copy link

codecov bot commented Nov 23, 2021

Codecov Report

Merging #7654 (bdf3c1f) into main (e3493fe) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7654      +/-   ##
==========================================
- Coverage   95.28%   95.28%   -0.01%     
==========================================
  Files        1254     1254              
  Lines      113227   113257      +30     
==========================================
+ Hits       107893   107916      +23     
- Misses       5334     5341       +7     
Impacted Files Coverage Δ
google/cloud/storage/internal/http_response.cc 100.00% <100.00%> (ø)
...oogle/cloud/storage/internal/http_response_test.cc 100.00% <100.00%> (ø)
...ogle/cloud/pubsub/internal/subscription_session.cc 94.25% <0.00%> (-3.45%) ⬇️
google/cloud/bigtable/internal/common_client.cc 95.71% <0.00%> (-1.43%) ⬇️
google/cloud/pubsub/subscriber_connection_test.cc 97.18% <0.00%> (-0.71%) ⬇️
...ud/spanner/integration_tests/client_stress_test.cc 85.52% <0.00%> (-0.66%) ⬇️
...cloud/pubsub/internal/subscription_session_test.cc 97.75% <0.00%> (-0.25%) ⬇️
google/cloud/pubsub/samples/samples.cc 92.10% <0.00%> (-0.24%) ⬇️
...le/cloud/storage/internal/curl_download_request.cc 89.62% <0.00%> (+0.37%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e3493fe...bdf3c1f. Read the comment docs.

Copy link
Contributor

@coryan coryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code LGTM. There is more copying than needed, but this is not in the critical path.

// ]
// See also https://cloud.google.com/apis/design/errors#http_mapping
ErrorInfo MakeErrorInfo(nlohmann::json const& details) {
static auto const* const kErrorInfoType =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: auto constexpr kErrorInfoType = "..."; seems like enough?

if (v.value("@type", "") != kErrorInfoType) continue;
auto reason = v.value("reason", "");
auto domain = v.value("domain", "");
auto metadata_json = v.value("metadata", nlohmann::json::object());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I do not worry about copying here, this should be the uncommon path. But if one was worried about copying, there is a copy of something like a small std::map<>. You could do something like:

  auto domain = v.value("domain", "");
  if (!v.contains("metadata")) return ErrorInfo{std::move(reason), std::move(domain), {});
  auto metadata = std::unordered_map<std::string, std::string>{};
  for (auto const& m : v["metadata"].items()) {
    metadata[m.key()] = m.value();
  }
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Due to the likely minimal copying that would save, I think I'll leave the code as is, because I think it's possibly slightly easier to read as is.

@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: bdf3c1f4ab9648b0803bcb6991fa055f4389cbb6

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@devjgm devjgm marked this pull request as ready for review November 23, 2021 17:19
@devjgm devjgm requested a review from a team as a code owner November 23, 2021 17:19
@devjgm devjgm merged commit 2408d74 into googleapis:main Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ErrorInfo support to GCS
3 participants