Skip to content

Commit

Permalink
Add errors example (stellar#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored Oct 7, 2022
1 parent b084f9c commit 6cc2f11
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"timelock",
"token",
"logging",
"errors",
]

[profile.release-with-logs]
Expand Down
20 changes: 20 additions & 0 deletions errors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "soroban-errors-contract"
version = "0.0.0"
authors = ["Stellar Development Foundation <[email protected]>"]
license = "Apache-2.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib", "rlib"]
doctest = false

[features]
testutils = ["soroban-sdk/testutils"]

[dependencies]
soroban-sdk = "0.1.0"

[dev_dependencies]
soroban-sdk = { version = "0.1.0", features = ["testutils"] }
46 changes: 46 additions & 0 deletions errors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![no_std]
use soroban_sdk::{contracterror, contractimpl, log, symbol, Env, Symbol};

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
LimitReached = 1,
}

const COUNTER: Symbol = symbol!("COUNTER");
const MAX: u32 = 10;

pub struct IncrementContract;

#[contractimpl]
impl IncrementContract {
/// Increment increments an internal counter, and returns the value. Errors
/// if the value is attempted to be incremented past 10.
pub fn increment(env: Env) -> Result<u32, Error> {
// Get the current count.
let mut count: u32 = env
.data()
.get(COUNTER)
.unwrap_or(Ok(0)) // If no value set, assume 0.
.unwrap(); // Panic if the value of COUNTER is not u32.
log!(&env, "count: {}", count);

// Increment the count.
count += 1;

// Check if the count exceeds the max.
if count <= MAX {
// Save the count.
env.data().set(COUNTER, count);

// Return the count to the caller.
Ok(count)
} else {
// Return an error if the max is exceeded.
Err(Error::LimitReached)
}
}
}

mod test;
47 changes: 47 additions & 0 deletions errors/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![cfg(test)]

use super::*;
use soroban_sdk::{testutils::Logger, Env};

extern crate std;

#[test]
fn test() {
let env = Env::default();
let contract_id = env.register_contract(None, IncrementContract);
let client = IncrementContractClient::new(&env, &contract_id);

assert_eq!(client.try_increment(), Ok(Ok(1)));
assert_eq!(client.try_increment(), Ok(Ok(2)));
assert_eq!(client.try_increment(), Ok(Ok(3)));
assert_eq!(client.try_increment(), Ok(Ok(4)));
assert_eq!(client.try_increment(), Ok(Ok(5)));
assert_eq!(client.try_increment(), Ok(Ok(6)));
assert_eq!(client.try_increment(), Ok(Ok(7)));
assert_eq!(client.try_increment(), Ok(Ok(8)));
assert_eq!(client.try_increment(), Ok(Ok(9)));
assert_eq!(client.try_increment(), Ok(Ok(10)));
assert_eq!(client.try_increment(), Err(Ok(Error::LimitReached)));

std::println!("{}", env.logger().all().join("\n"));
}

#[test]
#[should_panic(expected = "Status(ContractError(1))")]
fn test_panic() {
let env = Env::default();
let contract_id = env.register_contract(None, IncrementContract);
let client = IncrementContractClient::new(&env, &contract_id);

assert_eq!(client.increment(), 1);
assert_eq!(client.increment(), 2);
assert_eq!(client.increment(), 3);
assert_eq!(client.increment(), 4);
assert_eq!(client.increment(), 5);
assert_eq!(client.increment(), 6);
assert_eq!(client.increment(), 7);
assert_eq!(client.increment(), 8);
assert_eq!(client.increment(), 9);
assert_eq!(client.increment(), 10);
client.increment();
}

0 comments on commit 6cc2f11

Please sign in to comment.