diff --git a/examples/cli/HttpGet.roc b/examples/cli/HttpGet.roc index ada4abea06..93c60f30ec 100644 --- a/examples/cli/HttpGet.roc +++ b/examples/cli/HttpGet.roc @@ -9,8 +9,8 @@ main = url <- await Stdin.line - {} <- await (Stdout.line "The contents of \(url) are:\n") + result <- Task.attempt (Http.getUtf8 url) - contents <- await (Http.getUtf8 url) - - Stdout.line contents + when result is + Ok contents -> Stdout.line "The contents of \(url) are:\n\(contents)" + Err err -> Stdout.line "Error retrieving \(url) - error was: \(err)" diff --git a/examples/cli/platform/Http.roc b/examples/cli/platform/Http.roc index 2adf25bf5d..447c8e6c4d 100644 --- a/examples/cli/platform/Http.roc +++ b/examples/cli/platform/Http.roc @@ -2,5 +2,5 @@ interface Http exposes [ getUtf8 ] imports [ fx.Effect, Task ] # TODO FIXME Task.{ Task } -getUtf8 : Str -> Task.Task Str * -getUtf8 = \url -> Effect.map (Effect.httpGetUtf8 url) Ok +getUtf8 : Str -> Task.Task Str Str +getUtf8 = \url -> Effect.httpGetUtf8 url diff --git a/examples/cli/platform/Pkg-Config.roc b/examples/cli/platform/Pkg-Config.roc index c1f15a589f..1c6752712a 100644 --- a/examples/cli/platform/Pkg-Config.roc +++ b/examples/cli/platform/Pkg-Config.roc @@ -8,7 +8,7 @@ platform rtfeldman/roc-cli { putChar : I64 -> Effect {}, putLine : Str -> Effect {}, - httpGetUtf8 : Str -> Effect Str, + httpGetUtf8 : Str -> Effect (Result Str Str), getLine : Effect Str } diff --git a/examples/cli/platform/src/lib.rs b/examples/cli/platform/src/lib.rs index ad4fe80205..778563395a 100644 --- a/examples/cli/platform/src/lib.rs +++ b/examples/cli/platform/src/lib.rs @@ -45,16 +45,16 @@ pub fn roc_fx_putLine(line: RocStr) -> () { } #[no_mangle] -pub fn roc_fx_httpGetUtf8(url: RocStr) -> RocStr { - let body = ureq::get(unsafe { url.as_str() }) - .call() - .unwrap_or_else(|err| todo!("Report this HTTP error: {:?}", err)); - - let str = body - .into_string() - .unwrap_or_else(|err| todo!("Report this body.text() error: {:?}", err)); - - RocStr::from_slice(str.as_bytes()) +pub fn roc_fx_httpGetUtf8(url: RocStr) -> RocResult { + match ureq::get(unsafe { url.as_str() }).call() { + Ok(resp) => match resp.into_string() { + Ok(contents) => RocResult::Ok(RocStr::from_slice(contents.as_bytes())), // TODO make roc::Result! + // TODO turn this error into an enum! + Err(err) => RocResult::Err(RocStr::from_slice(format!("{:?}", err).as_bytes())), + }, + // TODO turn this error into an enum! + Err(err) => RocResult::Err(RocStr::from_slice(format!("{:?}", err).as_bytes())), + } } #[no_mangle]