示例#1
0
def raise_native(err: RpcError):
    """Convert the given gRPC error into a native exception and raise it."""
    status = err.code()
    if status == StatusCode.DEADLINE_EXCEEDED:
        raise TimeoutError(err.details())
    elif status == StatusCode.UNIMPLEMENTED:
        raise NotImplementedError(err.details())
    elif status == StatusCode.INVALID_ARGUMENT:
        raise ValueError(err.details())
    elif status == StatusCode.NOT_FOUND:
        raise LookupError(err.details())
    elif status == StatusCode.ALREADY_EXISTS:
        raise FileExistsError(err.details())
    else:
        raise RuntimeError(err.details())
示例#2
0
def test_client_grpc_retry_non_retryable(mocker):
    err = RpcError()
    err.code = MagicMock(return_value=StatusCode.INVALID_ARGUMENT)
    err.details = MagicMock(return_value='bad value')

    def get_foo():
        raise err

    stub = make_stub_with_func(get_foo)
    make_stub_methods_retryable(stub)

    with pytest.raises(RpcError) as exc_info:
        stub.GetFoo()

    # Should raise original error
    assert exc_info.value.code() == StatusCode.INVALID_ARGUMENT
    assert exc_info.value == err
示例#3
0
    def _handle_grpc_error(
        self,
        cancel_handler: CancelHandler,
        retry_handler: Optional[RetryHandler],
        grpc_error: grpc.RpcError,
        on_success: Callable[[X509Context], None],
        on_error: Callable[[Exception], None],
    ):
        grpc_error_code = grpc_error.code()

        if retry_handler and grpc_error_code not in _NON_RETRYABLE_CODES:
            _logger.error('Error connecting to the Workload API: {}'.format(
                str(grpc_error_code)))
            retry_handler.do_retry(
                self._call_watch_x509_context,
                [cancel_handler, retry_handler, on_success, on_error],
            )
        else:
            # don't retry, instead report error to user on the on_error callback
            error = FetchX509SvidError(str(grpc_error_code))
            on_error(error)
示例#4
0
def test_client_grpc_retry_fail(mocker):
    num_calls = 0

    err = RpcError()
    err.code = MagicMock(return_value=StatusCode.CANCELLED)
    err.details = MagicMock(return_value='Server broke')

    def get_foo():
        nonlocal num_calls
        num_calls += 1
        raise err

    stub = make_stub_with_func(get_foo)
    make_stub_methods_retryable(stub)

    with pytest.raises(RpcError) as exc_info:
        stub.GetFoo()

    # Should raise original error
    assert exc_info.value.code() == StatusCode.CANCELLED
    assert exc_info.value == err

    # num calls should be 1(orig call) + MAX_RETRIES
    assert num_calls == DEFAULT_MAX_RETRIES + 1
示例#5
0
def from_exception(e: grpc.RpcError):
    code, msg = e.code().value

    ctor = MAP.get(code, ClientError)
    return ctor(code, str(e))
示例#6
0
def _propagate_grpc_code_err(context: grpc.ServicerContext,
                             err: grpc.RpcError):
    logging.error(traceback.format_exc())
    context.set_code(err.code())
    context.set_details(str(err))
示例#7
0
文件: errors.py 项目: montag451/core
def show_grpc_error(e: grpc.RpcError, master, app: "Application"):
    title = [x.capitalize() for x in e.code().name.lower().split("_")]
    title = " ".join(title)
    title = f"GRPC {title}"
    dialog = ErrorDialog(master, app, title, e.details())
    dialog.show()