summaryrefslogtreecommitdiff
path: root/curl.ha
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2024-02-27 03:03:58 +0100
committerJulian Hurst <ark@mansus.space>2024-02-27 03:03:58 +0100
commit69712258bafda1258454e6336faabf527f207e3b (patch)
treeab564a979de88d0dd9c6208b51791ddad3567482 /curl.ha
parentd1cb4dc83bd60e7cc8e4013d5f33911278ee8b6e (diff)
downloadhacurl-69712258bafda1258454e6336faabf527f207e3b.tar.gz
Improve errors
Diffstat (limited to 'curl.ha')
-rw-r--r--curl.ha25
1 files changed, 18 insertions, 7 deletions
diff --git a/curl.ha b/curl.ha
index 3725d56..4c0c56d 100644
--- a/curl.ha
+++ b/curl.ha
@@ -26,23 +26,30 @@ export @symbol("curl_easy_getinfo") fn curl_easy_getinfo(
code: nullable *c::long
) CURLcode;
-type curlerror = !(str, int);
+type curlerror = !(setopterr | performerr | getinfoerr);
-fn get(url: str) (void | curlerror) = {
+type setopterr = !int;
+type performerr = !int;
+type getinfoerr = !int;
+
+fn get(url: const str) (void | curlerror) = {
let c = curl_easy_init();
defer curl_easy_cleanup(c);
let c_url = c::fromstr(url);
defer free(c_url);
let res = curl_easy_setopt(c, CURLOPT_URL, c_url);
if (res != 0) {
- return ("setopt", res): curlerror;
+ return res: setopterr;
};
res = curl_easy_perform(c);
if (res != 0) {
- return ("perform", res): curlerror;
+ return res: performerr;
};
let rc: c::long = 0;
- curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &rc);
+ res = curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &rc);
+ if (res != 0) {
+ return res: getinfoerr;
+ };
fmt::println(rc)!;
};
@@ -53,7 +60,11 @@ export fn main() void = {
match (get(os::args[1])) {
case void =>
yield;
- case let err: curlerror =>
- fmt::fatalf("{} returned {} instead of 0", err.0, err.1);
+ case let err: setopterr =>
+ fmt::fatalf("setopt returned {} instead of 0", err: int);
+ case let err: performerr =>
+ fmt::fatalf("perform returned {} instead of 0", err: int);
+ case let err: getinfoerr =>
+ fmt::fatalf("getinfo returned {} instead of 0", err: int);
};
};