From 0198c436e9da945e691280be8fbff261606df645 Mon Sep 17 00:00:00 2001 From: sarahschwartz <58856580+sarahschwartz@users.noreply.github.com> Date: Mon, 6 Feb 2023 15:20:40 -0700 Subject: [PATCH] docs: add call response error handling section (#824) add error handling section for the Call response page in the docs --- docs/src/calling-contracts/call-response.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/src/calling-contracts/call-response.md b/docs/src/calling-contracts/call-response.md index 62c90c386e..6de71b6dba 100644 --- a/docs/src/calling-contracts/call-response.md +++ b/docs/src/calling-contracts/call-response.md @@ -16,3 +16,21 @@ Where `value` will hold the value returned by its respective contract method, re - `receipts` will hold all [receipts](https://github.com/FuelLabs/fuel-specs/blob/master/specs/protocol/abi.md#receipt) generated by that specific contract call. - `gas_used` is the amount of gas it consumed by the contract call. + +## Error handling + +You can use the `is_ok` and `is_err` methods to check if a contract call `Result` is ok or contains an error. These methods will return either `true` or `false`. + +```rust, ignore +let is_ok = response.is_ok(); +let is_error = response.is_err(); +``` + +If `is_err` returns `true`, you can use the `unwrap_err` method to unwrap the error message. + +```rust, ignore +if response.is_err() { + let err = response.unwrap_err(); + println!("ERROR: {:?}", err); +}; +```