Module ibc_relayer_cli::conclude
source · Expand description
Custom-made solution to output a JSON return message and ensure a return code from a CLI command. The main use-case for this module is to provide a consistent output for queries and transactions.
The examples below rely on crate-private methods (for this reason, doctests are ignored).
They are intended for contributors to crate relayer-cli
, and not for users of this binary.
§Examples on how to use the quick-access constructors:
- Exit from a query/tx with a
String
error:
ⓘ
let e = String::from("error message");
Output::error(e).exit();
// or as an alternative:
Output::error(json!("error occurred")).exit();
- Exit from a query/tx with an error of type
anomaly
: In the case where the error is a complex type such as anomaly (including backtraces), it is better to simplify the output and only write out the chain of error sources, which we can achieve withformat!("{}", e)
. The complete solution is as follows:
ⓘ
let e: Error = Kind::Query.into();
Output::error(format!("{}", e)).exit();
§Note:
The resulting output that this approach generates is determined by the ‘error’ annotation given
to the error object Kind::Query
. If this error object comprises any positional arguments,
e.g. as achieved by Query(String, String)
, then it is important to cover these arguments
in the error
annotation, for instance:
ⓘ
#[derive(Debug, Error)]
pub enum Kind {
#[error("failed with underlying causes: {0}, {1}")]
Query(String, String),
// ...
}
- Exit from a query/tx with success:
ⓘ
let cs = ChannelEnd::default();
Output::success(cs).exit();
- Exit from a query/tx with success and multiple objects in the result:
ⓘ
let h = Height::default();
let end = ConnectionEnd::default();
Output::success(h).with_result(end).exit();
Structs§
- A CLI output with support for JSON serialization. The only mandatory field is the
status
, which typically signals a success (UNIX process return code0
) or an error (code1
). An optionalresult
can be added to an output.
Enums§
- The result to display before quitting, can either be a JSON value, some plain text, a value to print with its Debug instance, or nothing.
- Represents the exit status of any CLI command
Functions§
- Functional-style method to exit a program.
- Exits the program. Useful when a type produces an error which can no longer be propagated, and the program must exit instead.
- Returns true if the application global json flag
--json
is enabled. Returns false otherwise.