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

Fix exception handling in ApiMethodWrapper.handle_neptune_http_errors #1469

Merged
merged 6 commits into from Sep 13, 2023

Conversation

AleksanderWWW
Copy link
Contributor

@AleksanderWWW AleksanderWWW commented Sep 13, 2023

…tune_http_errors

Before submitting checklist

  • Did you update the CHANGELOG? (not for test updates, internal changes/refactors or CI/CD setup)
  • Did you ask the docs owner to review all the user-facing changes?

@AleksanderWWW AleksanderWWW changed the title fix bug with passing None as exception to ApiMethodWrapper.handle_nep… Fix exception handling in ApiMethodWrapper.handle_neptune_http_errors Sep 13, 2023
@codecov
Copy link

codecov bot commented Sep 13, 2023

Codecov Report

Patch coverage is 85.71% of modified lines.

Files Changed Coverage
...eptune/internal/backends/hosted_file_operations.py ø
src/neptune/common/backends/utils.py 50.00%
...eptune/internal/backends/swagger_client_wrapper.py 91.66%

📢 Thoughts on this report? Let us know!.

raise default_exception from source_exception
raise source_exception

if isinstance(source_exception, BaseException):
Copy link
Contributor

Choose a reason for hiding this comment

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

Just make source_exception an Optional (source_exception: Optional[Exception]) and check if it's not None.

except AttributeError:
pass

if exception is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

This if seems to be unnecessary.


if exception is None:
# raise generic HTTPError with response info
raise HTTPError(
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't expose bravado exceptions. ;)

How about using NeptuneApiException?

@@ -35,8 +35,8 @@ def handle_json_errors(
if error_processor:
raise error_processor(content) from source_exception

if default_exception and isinstance(source_exception, BaseException):
if default_exception and source_exception is not None:
Copy link
Contributor

Choose a reason for hiding this comment

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

We still may want to raise a default_exception. Just without a cause (from).

@AleksanderWWW
Copy link
Contributor Author

AleksanderWWW commented Sep 13, 2023

@aniezurawski
BTW
Shouldn't we switch places of those two if statements?

    if response.status_code >= 300:
        ApiMethodWrapper.handle_neptune_http_errors(response)
    if response.status_code in (
        HTTPUnprocessableEntity.status_code,
        HTTPPaymentRequired.status_code,
    ):
        raise NeptuneLimitExceedException(reason=response.json().get("title", "Unknown reason"))

They have 422 and 402 codes.

message=f"{response.status_code} Error: {response.reason} for {response.url}", response=response
)
# raise generic NeptuneApiException with response info
raise NeptuneApiException(f"{response.status_code} Error: {response.reason} for {response.url}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Again let's wrap it in exept AttributeError and just stringify all arguments if somthing goes wrong.

@aniezurawski
Copy link
Contributor

@aniezurawski BTW Shouldn't we switch places of those two if statements?

    if response.status_code >= 300:
        ApiMethodWrapper.handle_neptune_http_errors(response)
    if response.status_code in (
        HTTPUnprocessableEntity.status_code,
        HTTPPaymentRequired.status_code,
    ):
        raise NeptuneLimitExceedException(reason=response.json().get("title", "Unknown reason"))

They have 422 and 402 codes.

Huh. Good point. In this case it look like handle_neptune_http_errors was meant to not raise anything if there is no source or default excpetion. Now I see the bug was introduced in 1dfa30a. In that case we should just restore the old behaviour.

@AleksanderWWW
Copy link
Contributor Author

@aniezurawski BTW Shouldn't we switch places of those two if statements?

    if response.status_code >= 300:
        ApiMethodWrapper.handle_neptune_http_errors(response)
    if response.status_code in (
        HTTPUnprocessableEntity.status_code,
        HTTPPaymentRequired.status_code,
    ):
        raise NeptuneLimitExceedException(reason=response.json().get("title", "Unknown reason"))

They have 422 and 402 codes.

Huh. Good point. In this case it look like handle_neptune_http_errors was meant to not raise anything if there is no source or default excpetion. Now I see the bug was introduced in 1dfa30a. In that case we should just restore the old behaviour.

I don't follow. What's then the point of calling this with None as exception if in that case it's not supposed to do anything? Or am I missing something?

@AleksanderWWW AleksanderWWW merged commit 8ed3918 into master Sep 13, 2023
4 checks passed
@AleksanderWWW AleksanderWWW deleted the aw/fix-handling_http_errors branch September 13, 2023 12:51
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.

None yet

2 participants