Error reporting#
pw_rpc2: Next gen, zero-copy async RPC with end-to-end backpressure
In pw_rpc2, RPC status only reports transport and framework failures.
Application errors belong in your response protobufs. Most services use one of
three approaches:
No error field: For methods that cannot fail, or when firmware logs details separately via pw_log_rpc.
Per-method custom error detail: An
enumand optional context fields tailored to a single RPC.Common error message: A shared message combining a canonical status code with an optional tokenized or plain-text error string.
These protobuf patterns also apply to pw_rpc (RPC1). See pw_rpc error reporting for RPC1-specific guidance.
Comparison#
Approach |
Schema |
Embedded cost |
Good fit for |
|---|---|---|---|
No error field |
None (log via pw_log_rpc) |
Existing log budget |
Infallible RPCs; firmware debugging |
Per-method custom error detail |
Method-specific |
Minimal (varints) |
Programmatic handling, retries, and metrics |
Common error message (code only) |
|
Minimal (1 varint) |
HALs, storage, and status-forwarding proxies |
Common error message (with text) |
Status code + tokenized |
~4+ bytes (tokenized) or fixed string size |
Host daemons, CLIs, and OTA flows that log or return error text |
Per-method custom error detail#
Define a method-specific enum and any extra context fields the caller needs:
syntax = "proto3";
package my_project.proto;
message CalibrateResponse {
enum Result {
RESULT_UNSPECIFIED = 0;
RESULT_OK = 1;
RESULT_SENSOR_NOT_FOUND = 2;
RESULT_BUS_TIMEOUT = 3;
RESULT_OUT_OF_RANGE = 4;
}
Result result = 1;
// Optional context for host logging or metrics.
uint32 sensor_id = 2;
int32 measured_millidegrees = 3;
}
This avoids string formatting in firmware while giving callers typed failure cases they can branch on or format into log messages.
Common error message#
Define a reusable error message in a service or shared .proto file, then
embed it in your responses. pw.protobuf.StatusCode gives you canonical codes
matching pw::Status, absl::StatusCode, and google.rpc.Code 1:1.
Add a tokenized bytes field or a bounded string field if callers need
human-readable text:
syntax = "proto3";
package my_project.proto;
import "pw_protobuf_protos/status.proto";
import "pw_tokenizer_proto/options.proto";
message ErrorDetail {
// OK (0) indicates success; non-OK codes map 1:1 to pw::Status and
// absl::StatusCode.
pw.protobuf.StatusCode code = 1;
// Option A: Tokenized message (compact on flash and wire; supports printf
// arguments; requires the firmware token database on the caller).
bytes tokenized_message = 2
[(pw.tokenizer.format) = TOKENIZATION_OPTIONAL];
// Option B: Plain text (no token database needed; set max_size in
// .pwpb_options / .options, such as 64 or 128 bytes).
// string message = 2;
}
message WriteConfigResponse {
ErrorDetail error = 1;
uint32 applied_revision = 2;
}
Option A (tokenized
bytes): Compact on flash and wire when the caller has the firmware token database. See Tokenized fields in protocol buffers and pw_software_update/bundled_update.proto.Option B (bounded
string): Standalone when the caller does not manage token databases and flash/RAM budgets fit a small fixed-capacity string.
If you need compatibility with an existing cloud or gRPC schema, you can embed
google.rpc.Status instead, at the cost of google.protobuf.Any overhead
on embedded targets.
Streaming RPCs#
Aborting a streaming call with an RPC error never carries a payload. To report an application error from a server- or bidirectional-streaming RPC, send a stream message with the error details before finishing normally.
Language-specific error handling#
Framework-level RPC errors are documented per language:
Each language can map application errors from response protobufs onto its native error type:
#include "pw_tokenizer/tokenize.h"
void SetTokenizedError(WriteConfigResponse::Message& response,
pw::Status status,
uint32_t sector_id) {
response.error.code = static_cast<pw::protobuf::StatusCode>(status.code());
size_t size = response.error.tokenized_message.max_size();
response.error.tokenized_message.resize(size);
PW_TOKENIZE_TO_BUFFER(response.error.tokenized_message.data(),
&size,
"Config write failed on sector %u",
sector_id);
response.error.tokenized_message.resize(size);
}
#include "absl/status/status.h"
#include "pw_tokenizer/detokenize.h"
absl::Status ToAbslStatus(const ErrorDetail& err,
const pw::tokenizer::Detokenizer& detokenizer) {
auto code = static_cast<absl::StatusCode>(err.code());
if (code == absl::StatusCode::kOk) {
return absl::OkStatus();
}
std::string message =
detokenizer.Detokenize(err.tokenized_message()).BestString();
return absl::Status(code, message);
}
import pw_tokenizer.proto
def check_response(response, detokenizer) -> None:
pw_tokenizer.proto.detokenize_fields(detokenizer, response.error)
if response.error.code != StatusCode.OK:
detail = response.error.tokenized_message.decode(
'utf-8', errors='replace'
)
raise RuntimeError(f'{response.error.code}: {detail}')
fn into_result(resp: CalibrateResponse) -> Result<i32, CalibrateError> {
match resp.result {
CalibrateResult::Ok => Ok(resp.measured_millidegrees),
CalibrateResult::SensorNotFound => {
Err(CalibrateError::SensorNotFound(resp.sensor_id))
}
CalibrateResult::BusTimeout => Err(CalibrateError::BusTimeout),
_ => Err(CalibrateError::OutOfRange(resp.measured_millidegrees)),
}
}