Skip to content
This repository has been archived by the owner on Mar 20, 2018. It is now read-only.

Replacing ValueError with InvalidArgumentError in google.gax.errors.create_error(). #196

Merged
merged 1 commit into from Jul 31, 2017
Merged

Replacing ValueError with InvalidArgumentError in google.gax.errors.create_error(). #196

merged 1 commit into from Jul 31, 2017

Conversation

dhermes
Copy link
Contributor

@dhermes dhermes commented Jul 18, 2017

This new class inherits from both GaxError and ValueError to keep from breaking existing clients.

By wrapping an INVALID_ARGUMENT in a ValueError, the impl. was losing information by "stringify"-ing the wrapped exception.

See #132 for change that introduced the ValueError behavior.

…reate_error().

This new class inherits from both GaxError and ValueError to keep
from breaking existing clients.

By wrapping an `INVALID_ARGUMENT` in a `ValueError`, the impl. was losing
information by "stringify"-ing the wrapped exception.

See #132 for change that introduced the `ValueError` behavior.
@dhermes dhermes changed the title Removing google.gax.errors.create_error(). Replacing ValueError with InvalidArgumentError in google.gax.errors.create_error(). Jul 18, 2017
@dhermes
Copy link
Contributor Author

dhermes commented Jul 18, 2017

@lukesneeringer PTAL

@lukesneeringer
Copy link
Contributor

Waiting on CI, then happy to merge.

status_code = config.exc_to_code(cause)
status_name = config.NAME_STATUS_CODES.get(status_code)
if status_name == 'INVALID_ARGUMENT':
return InvalidArgumentError(msg, cause=cause)
Copy link
Contributor Author

@dhermes dhermes Jul 18, 2017

Choose a reason for hiding this comment

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

@lukesneeringer @jonparrott It's worth noting here that, str(exc) has still changed. We went from:

>>> msg = 'RPC failed'
>>> exc = ValueError('{}: {}'.format(msg, cause))
>>> str(exc)
'RPC failed: <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Resource name invalid.)>'
>>> exc.args
('RPC failed: <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Resource name invalid.)>',)
>>> raise exc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: RPC failed: <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Resource name invalid.)>

to:

>>> exc = InvalidArgumentError(msg, cause=cause)
>>> str(exc)
'InvalidArgumentError(RPC failed, caused by <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Resource name invalid.)>)'
>>> exc.args
('RPC failed',)
>>> raise exc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
google.gax.errors.InvalidArgumentError: InvalidArgumentError(RPC failed, caused by <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Resource name invalid.)>)

Aside, the setup is:

>>> import grpc
>>> from grpc import _channel
>>> 
>>> status_code = grpc.StatusCode.INVALID_ARGUMENT
>>> details = 'Resource name invalid.'
>>> exc_state = _channel._RPCState((), None, None, status_code, details)
>>> cause = _channel._Rendezvous(exc_state, None, None, None)

Copy link
Contributor

Choose a reason for hiding this comment

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

One potential bad thing about this is that there is nothing to tell the user to check cause.

Perhaps it would be worth defining a __str__ that gives a more useful message? (It does not have to be the same message.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lukesneeringer Are you proposing a change to GaxError.__str__ or something else?

Copy link
Contributor

Choose a reason for hiding this comment

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

I was proposing InvalidArgumentError.__str__ (but I am not married to that). If GaxError has a cause attribute (I think it does) than it is the better choice. Then you just have to make sure that GaxError is before ValueError in your MRO (so, flip what you have now).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MERP.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lukesneeringer Actually, un-MERP. Double check my comment above. It has the "correct" representation:

>>> str(exc)
'InvalidArgumentError(RPC failed, caused by <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Resource name invalid.)>)'

Copy link
Contributor

Choose a reason for hiding this comment

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

I am no longer tracking the state of this. Is there an outstanding question/issue or should I merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am just unsure what you actually wanted.

@dhermes
Copy link
Contributor Author

dhermes commented Jul 18, 2017

@lukesneeringer CI is green, but LMK what you think about the str/repr/stacktrace changes

@dhermes
Copy link
Contributor Author

dhermes commented Jul 21, 2017

@lukesneeringer Can we merge / resolve?

@dhermes
Copy link
Contributor Author

dhermes commented Jul 31, 2017

@lukesneeringer @tseaver @jonparrott Let's get this thing to the finish line?

@dhermes
Copy link
Contributor Author

dhermes commented Jul 31, 2017

I don't have merge privileges here.

@theacodes theacodes merged commit 58e8c2f into googleapis:master Jul 31, 2017
@dhermes dhermes deleted the standard-exc-wrapping branch July 31, 2017 19:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants