This document outlines the recommended usage and APIs for error handling in Cosmos SDK modules.
Modules are encouraged to define and register their own errors to provide better context on failed message or handler execution. Typically, these errors should be common or general errors which can be further wrapped to provide additional specific execution context.
Modules should define and register their custom errors in
Registration of errors is handled via the
errors package (opens new window).
Each custom module error must provide the codespace, which is typically the module name (e.g. "distribution") and is unique per module, and a uint32 code. Together, the codespace and code provide a globally unique Cosmos SDK error. Typically, the code is monotonically increasing but does not necessarily have to be. The only restrictions on error codes are the following:
- Must be greater than one, as a code value of one is reserved for internal errors.
- Must be unique within the module.
Note, the Cosmos SDK provides a core set of common errors. These errors are defined in
types/errors/errors.go (opens new window).
The custom module errors can be returned as their concrete type as they already fulfill the
interface. However, module errors can be wrapped to provide further context and meaning to failed
Regardless if an error is wrapped or not, the Cosmos SDK's
errors package provides a function to determine if
an error is of a particular kind via
If a module error is registered, the Cosmos SDK
errors package allows ABCI information to be extracted
ABCIInfo function. The package also provides
auxiliary functions to automatically get
DeliverTx responses from an error.