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

Sample improve, exit when error happens instead of raise error #330

Closed
wants to merge 6 commits into from

Conversation

TingDaoK
Copy link
Contributor

@TingDaoK TingDaoK commented Jul 7, 2023

Issue #, if available:

Description of changes:

  • As for the sample, do raise error from callback, just exit with error print out.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@@ -142,7 +142,7 @@ static bool s_on_list_bucket_result_node_encountered(
}

if (ret_val && operation_data->on_object) {
ret_val |= operation_data->on_object(&fs_wrapper.fs_info, operation_data->user_data);
ret_val &= operation_data->on_object(&fs_wrapper.fs_info, operation_data->user_data);
Copy link
Contributor

Choose a reason for hiding this comment

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

trivial: rename ret_val -> keep_going or something, so we don't confuse it with an int again

Copy link
Contributor

@graebm graebm Jul 8, 2023

Choose a reason for hiding this comment

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

or just do keep_going = instead of this bitwise nonsense

return AWS_OP_ERR;
fprintf(
stderr,
"Failure when open file %s with error %s\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Failure when open file %s with error %s\n",
"Failure to open file %s, error %s\n",

@@ -142,7 +142,7 @@ static bool s_on_list_bucket_result_node_encountered(
}

if (ret_val && operation_data->on_object) {
ret_val |= operation_data->on_object(&fs_wrapper.fs_info, operation_data->user_data);
ret_val &= operation_data->on_object(&fs_wrapper.fs_info, operation_data->user_data);
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you looked into our other uses of the XML parser, in our real code, to see if we're "properly" using user_data to report errors?

@codecov-commenter
Copy link

Codecov Report

Merging #330 (b6ad74e) into main (7c34328) will decrease coverage by 0.13%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #330      +/-   ##
==========================================
- Coverage   88.87%   88.74%   -0.13%     
==========================================
  Files          17       17              
  Lines        4943     4932      -11     
==========================================
- Hits         4393     4377      -16     
- Misses        550      555       +5     
Impacted Files Coverage Δ
source/s3_list_objects.c 80.30% <100.00%> (ø)
source/s3_list_parts.c 74.80% <100.00%> (ø)

... and 2 files with indirect coverage changes

graebm added a commit to awslabs/aws-c-common that referenced this pull request Jul 18, 2023
**Issue:**
It's hard to report errors with the current API. Errors are being accidentally ignored, and some errors are never checked (perhaps because it was too much effort?).

**Diagnosis:**
The current callback returns `bool` of whether to continue parsing, rather than our typical `int/AWS_OP_SUCCESS/aws_raise_error()` [error handling](https://github.com/awslabs/aws-c-common/blob/b1ef77c1790a8776b58ae5ae57f90e9534b78991/README.md#error-handling).

This seems like a simple design. But the inconsistency in return type leads to errors being [mistakenly swallowed](awslabs/aws-c-s3#330 (comment)). And it makes it hard when you do want to "bubble up" an error from the callback. Callbacks needs to store a custom `error_code` in their `user_data` to report an error. Most callbacks never bothered to do this, maybe because it was extra work?

**Description of changes:**
- XML traversal callback returns `int` instead of `bool`.
    - If a callback fails, the whole parse() fails.
    - You can no longer stop parsing without causing failure. But in nearly all use-cases we were stopping due to error. I found 1 case where we stopped because we found what we were looking for. But in this case, it didn't really hurt to continue parsing. The reduced complexity seemed worth the change.
- Remove ~aws_xml_parser_new(), aws_xml_parser_destroy(), aws_xml_parser_parse()~, replace with  `aws_xml_parse()`.
    - The new() and destroy() calls were unnecessary. Removing them simplifies use.
- `aws_xml_node_get_name()` just returns `aws_byte_cursor()`, instead of ~int~
    - This can't fail. Changing the signature simplifies use.
- Raise `AWS_ERROR_INVALID_XML` instead of ~AWS_ERROR_MALFORMED_INPUT_STRING~
    - This change is more wishy-washy. It seemed useful to get this new, more specific, error code if it bubbling up from deep within some larger operation, like an S3 meta-request.

**API BREAK:**
We don't know any external uses of this API, so it seems safe to change. The API is only intended for internal use by the aws-c libraries, which are being fixed up now. This API was quickly written as private code in aws-c-auth (awslabs/aws-c-auth#40), then moved to public in aws-c-common (#674) when aws-c-s3 also needed to parse XML. The fact that it was originally private is why this API didn't get more scrutiny originally.
@TingDaoK TingDaoK closed this Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants