Skip to content

Commit d3e5029

Browse files
committed
Fix rust template
1 parent a4cb0f9 commit d3e5029

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

templates/rust/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ restate-sdk = "0.3"
88
tokio = { version = "1", features = ["full"] }
99
tracing-subscriber = "0.3"
1010
rand = "0.8.5"
11+
anyhow = "1.0.86"

templates/rust/src/main.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@ impl Greeter for GreeterImpl {
1515
async fn greet(&self, mut ctx: Context<'_>, name: String) -> HandlerResult<String> {
1616
// Durably execute a set of steps; resilient against failures
1717
let greeting_id = ctx.rand_uuid().to_string();
18-
ctx.run(|| async {
19-
send_notification(&greeting_id, &name);
20-
Ok(())
21-
}).await?;
18+
ctx.run(|| send_notification(&greeting_id, &name)).await?;
2219
ctx.sleep(Duration::from_millis(1000)).await?;
23-
ctx.run(|| async {
24-
send_reminder(&greeting_id);
25-
Ok(())
26-
}).await?;
20+
ctx.run(|| send_reminder(&greeting_id)).await?;
2721

2822
// Respond to caller
2923
Ok(format!("Greetings {name}"))

templates/rust/src/utils.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
use rand::random;
2+
use anyhow::{anyhow, Result};
3+
use restate_sdk::errors::HandlerError;
24

3-
pub fn send_notification(greeting_id: &str, name: &str) {
5+
pub async fn send_notification(greeting_id: &str, name: &str) -> Result<(), HandlerError> {
46
if random::<f32>() < 0.5 {
57
println!("👻 Failed to send notification: {} - {}", greeting_id, name);
6-
panic!("Failed to send notification: {} - {}", greeting_id, name);
8+
return Err(HandlerError::from(anyhow!("Failed to send notification: {} - {}", greeting_id, name)));
79
}
810
println!("Notification sent: {} - {}", greeting_id, name);
11+
Ok(())
912
}
1013

11-
pub fn send_reminder(greeting_id: &str) {
14+
pub async fn send_reminder(greeting_id: &str)-> Result<(), HandlerError> {
1215
if random::<f32>() < 0.5 {
1316
println!("👻 Failed to send reminder: - {}", greeting_id);
14-
panic!("Failed to send reminder: - {}", greeting_id);
17+
return Err(HandlerError::from(anyhow!("Failed to send reminder: {}", greeting_id)));
1518
}
1619
println!("Reminder sent: {}", greeting_id);
20+
Ok(())
1721
}

0 commit comments

Comments
 (0)