This document outlines the recommended usage and APIs for error handling in Cosmos SDK modules.
Registration
Modules should define and register their custom errors inx/{module}/errors.go
. Registration of errors is handled via the errors
package.
Example:
x/distribution/types/errors.go
- Must be greater than one, as a code value of one is reserved for internal errors.
- Must be unique within the module.
types/errors/errors.go
.
Wrapping
The custom module errors can be returned as their concrete type as they already fulfill theerror
interface. However, module errors can be wrapped to provide further context and meaning to failed execution.
Example:
x/bank/keeper/keeper.go
errors
package provides a function to determine if an error is of a particular kind via Is
.
ABCI
If a module error is registered, the Cosmos SDKerrors
package allows ABCI information to be extracted through the ABCIInfo
function. The package also provides ResponseCheckTx
and ResponseDeliverTx
as auxiliary functions to automatically get CheckTx
and DeliverTx
responses from an error.