Skip to content

Update wrap_methods to optionally call wrap_errors if it is a gRPC callable #661

Open
@ohmayr

Description

@ohmayr

The following wrap_method function is a common code path for both REST and gRPC which incorrectly tries to wrap errors for all callables via wrap_errors but it's is meant to be used for only gRPC callables:

def wrap_method(
    func,
    default_retry=None,
    default_timeout=None,
    default_compression=None,
    client_info=client_info.DEFAULT_CLIENT_INFO,
):

    func = grpc_helpers_async.wrap_errors(func)

    metadata = [client_info.to_grpc_metadata()] if client_info is not None else None

    return functools.wraps(func)(
        _GapicCallable(
            func,
            default_retry,
            default_timeout,
            default_compression,
            metadata=metadata,
        )
    )

Proposed fix:

try:
    import grpc
    from google.api_core import grpc_helpers_async
    _STREAM_WRAP_CLASSES = (grpc.aio.UnaryUnaryMultiCallable, grpc.aio.UnaryStreamMultiCallable, grpc.aio.StreamUnaryMultiCallable, grpc.aio.StreamStreamMultiCallable)
except ImportError:
    grpc = None


if grpc and isinstance(func, _STREAM_WRAP_CLASSES):
        func = grpc_helpers_async.wrap_errors(func)

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions