Skip to content

v0.2.0: open error codes, the protocontract crate, and a worked example

Choose a tag to compare

@github-actions github-actions released this 07 Oct 10:13
· 33 commits to main since this release
v0.2.0
7ee0ca7

One error type for every contract, with codes the contract declares itself, a renamed runtime crate, and an example of a provider and a consumer that know only the generated API.

Added

  • examples/glossary: a contract in use. A proto with a service and its
    error codes, the crate generated from it, a provider that implements the
    traits, a consumer that calls them without knowing the provider, and the
    application that wires the two (cargo run -p glossary-app).
  • Open error codes: protocontract::ErrorCode, implemented by every protobuf
    enum that protoc-gen-buffa generates and by hand for a Rust enum, and
    protocontract::Code, a code with its type erased. A contract declares the
    codes its callers may branch on as an enum next to its service.
  • protocontract::Fault, the trait of a failure (code, retryable,
    backtrace), and protocontract::Failure, the ready-made fault (code,
    message, with_retryable, with_source, a captured backtrace). Any
    Fault converts into protocontract::Error with ?.
  • Error::is, code_as, retryable, stack, backtrace,
    downcast_ref, and context; the protocontract::Context extension adds a
    stack frame to the error of a Result (.context("load"),
    .with_context(|| …)).
  • The plugin finds a service's error-code enum by name
    (GreeterErrorCode, then GreeterServiceErrorCode, in the package of
    GreeterService), emits the alias GreeterServiceErrorCode next to the
    traits, and names it in their rustdoc. A service without such an enum is
    generated as before; a message or enum already named like the alias
    fails the run.
  • protocontract::RuntimeCode: PANICKED (Offload), CANNOT_BLOCK
    (Blocking inside a current-thread runtime), CANCELLED (a blocking
    task dropped by its runtime).

Changed

  • Breaking. The runtime crate contract is now protocontract
    (directory rust/protocontract, Bazel target //rust/protocontract).
    Depend on protocontract and replace contract:: paths; the plugin's
    runtime= default is ::protocontract. A crate that mounts the traits
    may now name their module contract.
  • Breaking. contract::Status is now protocontract::Error: every
    generated method returns Result<_, protocontract::Error>. Build one with
    Error::new(code, message), where code is a value of the contract's
    error enum, or from a Failure or a Fault of your own.
  • Breaking. The bridges report RuntimeCode values instead of
    INTERNAL and FAILED_PRECONDITION.
  • The runtime depends on buffa (for ErrorCode on its enums) instead of
    buffa-types.
  • Each method's parameter is named after its message in snake case
    (word: Word, greet_request: GreetRequest), plural for an inbound
    stream (items), instead of request and requests. Implementations
    are unaffected: Rust does not bind parameter names.

Removed

  • Breaking. The fixed google.rpc.Code set (contract::Code as an
    enum) and the per-code constructors (Status::invalid_argument, …):
    codes are open (see Added).
  • Breaking. Status::with_details, details, and into_parts: the
    calls stay in one process, so the wire payloads of google.rpc.Status
    have no use.
  • Breaking. PartialEq on the error: a fault cannot be compared.
    Compare codes (error.is(code)) instead.

Upgrading from 0.1.0

  1. Depend on protocontract instead of contract, from tag v0.2.0,
    and replace contract:: paths with protocontract::. A plugin run
    with runtime= set keeps its path; without it the generated code now
    names ::protocontract.
  2. Rename Status to Error in implementations and callers.
  3. Declare the codes of each service as a proto enum named
    <Service>ErrorCode next to it (for service Greeter,
    enum GreeterErrorCode), and replace Status::invalid_argument(msg)
    and the other per-code constructors with
    Error::new(GreeterErrorCode::…, msg); use Failure for a retryable
    failure or one with a source.
  4. Branch with error.is(code) or error.code_as::<GreeterErrorCode>()
    instead of comparing error.code() with Code::…; compare codes, not
    errors, in tests.
  5. Code that matched INTERNAL or FAILED_PRECONDITION from Offload or
    Blocking matches RuntimeCode::Panicked or
    RuntimeCode::CannotBlock.