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

Add wrappers for gRPC errors #19

Closed
llucax opened this issue Feb 15, 2024 · 1 comment
Closed

Add wrappers for gRPC errors #19

llucax opened this issue Feb 15, 2024 · 1 comment
Labels
type:enhancement New feature or enhancement visitble to users
Milestone

Comments

@llucax
Copy link
Contributor

llucax commented Feb 15, 2024

What's needed?

We need to make sure gRPC errors are properly wrapped so we don't leak the use of grpc/protobuf to client users.

From frequenz-floss/frequenz-api-dispatch#144:

gRPC errors in Python are raised as grpc.RpcError exceptions. Here's how to handle them:

import grpc

try:
    response = await self._stub.GetMicrogridDispatch(request)
    # Process the successful response here

except grpc.RpcError as error:
    status_code = error.code()  # Get the gRPC status code
    details = error.details()   # Optional error details as a string 

    # Example of handling based on status code
    if status_code == grpc.StatusCode.NOT_FOUND:
        print("Resource not found!")
    elif status_code == grpc.StatusCode.PERMISSION_DENIED:
        print("Insufficient permissions")
    else: 
        print(f"Error occurred: {status_code} - {details}")

Proposed solution

Add wrapper error classes for the relevant gRPC errors raised by clients.

Some of these errors should inherit from specific Python built-in errors, like INVALID_ARGUMENT should inherit from ValueError, NOT_FOUND and ALREADY_EXISTS should probably inherit from KeyError, etc.

For errors that should never occur (because the client is doing some sanity checks to prevent them from happening), we should probably still have a wrapper in the client, like UnexpectedError, because a server could always misbehave for some reason.

When raising these errors in the client, we can still attach the original gRPC error using the raise OurClientError(...) from grpc_error syntax, which could be very useful for debugging purposes.

Additional context

@llucax llucax added the type:enhancement New feature or enhancement visitble to users label Feb 15, 2024
@llucax llucax added this to the v0.2.0 milestone Feb 15, 2024
@llucax llucax modified the milestones: v0.2.0, v0.3.0 Mar 1, 2024
@llucax llucax modified the milestones: v0.3.0, Untriaged May 8, 2024
@llucax
Copy link
Contributor Author

llucax commented Jul 26, 2024

Done in:

@llucax llucax closed this as completed Jul 26, 2024
@llucax llucax modified the milestones: Untriaged, v0.5.0 Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement New feature or enhancement visitble to users
Projects
None yet
Development

No branches or pull requests

1 participant