improve error messages

This commit is contained in:
jem 2020-06-17 18:23:42 +02:00
parent 3dd7083d7b
commit c564c525f4
2 changed files with 26 additions and 15 deletions

View file

@ -1,6 +1,7 @@
{:source-paths ["src/main" {:source-paths ["src/main"
"src/test"] "src/test"]
:dependencies [[orchestra "2018.12.06-2"]] :dependencies [[orchestra "2019.02.06-1"]
[expound "0.8.4"]]
:builds {:dev {:target :node-library :builds {:dev {:target :node-library
:output-to "target/node-lib.js" :output-to "target/node-lib.js"
:exports {:infra mastodon-bot.core/main} :exports {:infra mastodon-bot.core/main}

View file

@ -4,12 +4,15 @@
[clojure.spec.test.alpha :as st] [clojure.spec.test.alpha :as st]
[clojure.string :as cs] [clojure.string :as cs]
[orchestra.core :refer-macros [defn-spec]] [orchestra.core :refer-macros [defn-spec]]
[expound.alpha :as expound]
[mastodon-bot.infra :as infra] [mastodon-bot.infra :as infra]
[mastodon-bot.transform :as transform] [mastodon-bot.transform :as transform]
[mastodon-bot.mastodon-api :as masto] [mastodon-bot.mastodon-api :as masto]
[mastodon-bot.twitter-api :as twitter] [mastodon-bot.twitter-api :as twitter]
[mastodon-bot.tumblr-api :as tumblr])) [mastodon-bot.tumblr-api :as tumblr]))
(set! s/*explain-out* expound/printer)
(s/def ::mastodon masto/mastodon-auth?) (s/def ::mastodon masto/mastodon-auth?)
(s/def ::twitter twitter/twitter-auth?) (s/def ::twitter twitter/twitter-auth?)
(s/def ::tumblr tumblr/tumblr-auth?) (s/def ::tumblr tumblr/tumblr-auth?)
@ -40,9 +43,9 @@
[config config?] [config config?]
(:transform config)) (:transform config))
(defn transform! [config-location] (defn-spec transform! any?
(let [config (infra/load-config config-location) [config config?]
mastodon-auth (mastodon-auth config)] (let [mastodon-auth (mastodon-auth config)]
(masto/get-mastodon-timeline (masto/get-mastodon-timeline
mastodon-auth mastodon-auth
(fn [timeline] (fn [timeline]
@ -80,24 +83,31 @@
)))) ))))
))))) )))))
(def usage
"usage:
node target/mastodon-bot.js [-h] /path/to/config.edn
or
npm start [-h] /path/to/config.edn
")
(defn main [& args] (defn main [& args]
(let [parsed-args (s/conform ::args args)] (let [parsed-args (s/conform ::args args)]
(if (= ::s/invalid parsed-args) (if (= ::s/invalid parsed-args)
(do (s/explain ::args args) (do (s/explain ::args args)
(infra/exit-with-error "Bad commandline arguments")) (infra/exit-with-error (str "Bad commandline arguments\n" usage)))
(let [{:keys [options config-location]} parsed-args] (let [{:keys [options config-location]} parsed-args]
(cond (cond
(some #(= "-h" %) options) (some #(= "-h" %) options)
(print "usage: (print usage)
:default
node target/mastodon-bot.js [-h] /path/to/config.edn (let [config (infra/load-config config-location)]
(when (not (s/valid? config? config))
or (s/explain config? config)
(infra/exit-with-error "Bad configuration"))
npm start [-h] /path/to/config.edn (transform! config)))))))
")
:default
(transform! config-location))))))
(st/instrument 'mastodon-auth) (st/instrument 'mastodon-auth)
(st/instrument 'twitter-auth) (st/instrument 'twitter-auth)