pw_status#
Exception-free error propagation for embedded
Stable C++17 C Python Java TypeScript Rust
Easy: Simple to understand, includes convenient macro
PW_TRY
Efficient: No memory allocation, no exceptions
Established: Just like
absl::Status
, deployed extensively at Google
pw::Status
is Pigweed’s error propagation primitive, enabling
exception-free error handling. The primary feature of pw_status
is the
pw::Status
class, a simple, zero-overhead status object that
wraps a status code, and the PW_TRY
macro. For example:
#include "pw_status/status.h"
pw::Status ImuEnable() {
if (!device_has_imu) {
return Status::FailedPrecondition();
}
PW_TRY(ImuSpiSendEnable()); // Propagates failure on non-OK status.
return pw::OkStatus();
}
void Initialize() {
if (auto status = ImuEnable(); status.ok()) {
PW_LOG_INFO("Imu initialized successfully")
} else {
if (status.IsFailedPrecondition()) {
PW_LOG_WARNING("No IMU present");
} else {
PW_LOG_ERROR("Unknown error initializing IMU: %d", status.code());
}
}
}
pw_status
provides an implementation of status in every supported
Pigweed language, including C, Rust, TypeScript, Java, and Python.
Pigweed’s status matches Google’s standard status codes (see the Google APIs repository). These codes are used extensively in Google projects including Abseil (status/status.h) and gRPC (doc/statuscodes.md).
Quick reference#
See Status codes for the precise semantics of each error, as well as how to spell the status in each of our supported languages. Click on the status names below to jump directly to that error’s reference.
Status |
Code |
Description |
---|---|---|
0 |
Operation succeeded |
|
1 |
Operation was cancelled, typically by the caller |
|
2 |
Unknown error occurred. Avoid this code when possible. |
|
3 |
Argument was malformed; e.g. invalid characters when parsing integer |
|
4 |
Deadline passed before operation completed |
|
5 |
The entity that the caller requested (e.g. file or directory) is not found |
|
6 |
The entity that the caller requested to create is already present |
|
7 |
Caller lacks permission to execute action |
|
8 |
Insufficient resources to complete operation; e.g. supplied buffer is too small |
|
9 |
System isn’t in the required state; e.g. deleting a non-empty directory |
|
10 |
Operation aborted due to e.g. concurrency issue or failed transaction |
|
11 |
Operation attempted out of range; e.g. seeking past end of file |
|
12 |
Operation isn’t implemented or supported |
|
13 |
Internal error occurred; e.g. system invariants were violated |
|
14 |
Requested operation can’t finish now, but may at a later time |
|
15 |
Unrecoverable data loss occurred while completing the requested operation |
|
16 |
Caller does not have valid authentication credentials for the operation |