Add HTTP error handling to CLI example

This commit is contained in:
Richard Feldman 2021-04-06 22:45:04 -04:00
parent dba526bb3e
commit a3c7dc494b
4 changed files with 17 additions and 17 deletions

View file

@ -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<RocStr, RocStr> {
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]