From 43e5e6876e4b6f59d93cc602024180cf7c938079 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sat, 31 Aug 2024 15:22:26 +0100 Subject: [PATCH] improve tangc error messages --- pkgs/tangc/tangc.fnl | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkgs/tangc/tangc.fnl b/pkgs/tangc/tangc.fnl index 6b01f61..1276b4b 100644 --- a/pkgs/tangc/tangc.fnl +++ b/pkgs/tangc/tangc.fnl @@ -90,11 +90,20 @@ })) (fn http-post [url body] - (json.decode + (match (http.request "POST" url "" 0 "application/x-www-form-urlencoded" - body))) + body) + s (json.decode s) + (nil err) (error err))) + + +(fn http-get [url body] + (match + (http.fetch url) + s (json.decode s) + (nil code msg) (error (.. "Error: " code ": " msg)))) (fn decrypt [] (let [b64 (base64 :url) @@ -141,13 +150,15 @@ (json.encode {:url "http://tang.local"}))) (print (%% "tangc encrypt %q < plaintext > filename.enc # encrypt" (json.encode {:thp "idGFpbiBhIHByZWJ1aWx0IGRhdGFiYXNlIGZyb20gaH" - :url "http://tang.local"})))) + :url "http://tang.local"}))) + (os.exit 1)) (fn encrypt [cfg] (let [{ : url : thp : adv } cfg + _ (or url (usage)) b64 (base64 :url) - adv (or adv (json.decode (http.fetch (.. url "/adv/" (or thp "")))))] + adv (or adv (http-get (.. url "/adv/" (or thp ""))))] (assert adv.payload "advertisement is malformed") (let [jwks (json.decode (b64:decode adv.payload)) ver (jose! [:jwk :use "-i-" "-r" "-u" "verify" "-o-"] @@ -165,6 +176,6 @@ (case arg ["decrypt"] (decrypt) ["encrypt" cfg] (encrypt (json.decode cfg)) - _ (error "usage: tangc [decrypt] | [encrypt cfg]"))) + _ (usage))) { : run }