Repository navigation
v0.2.0: open error codes, the protocontract crate, and a worked example
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 thatprotoc-gen-buffagenerates 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 anenumnext to itsservice. protocontract::Fault, the trait of a failure (code,retryable,
backtrace), andprotocontract::Failure, the ready-made fault (code,
message,with_retryable,with_source, a captured backtrace). Any
Faultconverts intoprotocontract::Errorwith?.Error::is,code_as,retryable,stack,backtrace,
downcast_ref, andcontext; theprotocontract::Contextextension adds a
stack frame to the error of aResult(.context("load"),
.with_context(|| …)).- The plugin finds a service's error-code enum by name
(GreeterErrorCode, thenGreeterServiceErrorCode, in the package of
GreeterService), emits the aliasGreeterServiceErrorCodenext 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
(Blockinginside a current-thread runtime),CANCELLED(a blocking
task dropped by its runtime).
Changed
- Breaking. The runtime crate
contractis nowprotocontract
(directoryrust/protocontract, Bazel target//rust/protocontract).
Depend onprotocontractand replacecontract::paths; the plugin's
runtime=default is::protocontract. A crate that mounts the traits
may now name their modulecontract. - Breaking.
contract::Statusis nowprotocontract::Error: every
generated method returnsResult<_, protocontract::Error>. Build one with
Error::new(code, message), wherecodeis a value of the contract's
error enum, or from aFailureor aFaultof your own. - Breaking. The bridges report
RuntimeCodevalues instead of
INTERNALandFAILED_PRECONDITION. - The runtime depends on
buffa(forErrorCodeon 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 ofrequestandrequests. Implementations
are unaffected: Rust does not bind parameter names.
Removed
- Breaking. The fixed
google.rpc.Codeset (contract::Codeas an
enum) and the per-code constructors (Status::invalid_argument, …):
codes are open (see Added). - Breaking.
Status::with_details,details, andinto_parts: the
calls stay in one process, so the wire payloads ofgoogle.rpc.Status
have no use. - Breaking.
PartialEqon the error: a fault cannot be compared.
Compare codes (error.is(code)) instead.
Upgrading from 0.1.0
- Depend on
protocontractinstead ofcontract, from tagv0.2.0,
and replacecontract::paths withprotocontract::. A plugin run
withruntime=set keeps its path; without it the generated code now
names::protocontract. - Rename
StatustoErrorin implementations and callers. - Declare the codes of each service as a proto enum named
<Service>ErrorCodenext to it (forservice Greeter,
enum GreeterErrorCode), and replaceStatus::invalid_argument(msg)
and the other per-code constructors with
Error::new(GreeterErrorCode::…, msg); useFailurefor a retryable
failure or one with a source. - Branch with
error.is(code)orerror.code_as::<GreeterErrorCode>()
instead of comparingerror.code()withCode::…; compare codes, not
errors, in tests. - Code that matched
INTERNALorFAILED_PRECONDITIONfromOffloador
BlockingmatchesRuntimeCode::Panickedor
RuntimeCode::CannotBlock.