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,15 +83,8 @@
)))) ))))
))))) )))))
(defn main [& args] (def usage
(let [parsed-args (s/conform ::args args)] "usage:
(if (= ::s/invalid parsed-args)
(do (s/explain ::args args)
(infra/exit-with-error "Bad commandline arguments"))
(let [{:keys [options config-location]} parsed-args]
(cond
(some #(= "-h" %) options)
(print "usage:
node target/mastodon-bot.js [-h] /path/to/config.edn node target/mastodon-bot.js [-h] /path/to/config.edn
@ -96,8 +92,22 @@
npm start [-h] /path/to/config.edn npm start [-h] /path/to/config.edn
") ")
(defn main [& args]
(let [parsed-args (s/conform ::args args)]
(if (= ::s/invalid parsed-args)
(do (s/explain ::args args)
(infra/exit-with-error (str "Bad commandline arguments\n" usage)))
(let [{:keys [options config-location]} parsed-args]
(cond
(some #(= "-h" %) options)
(print usage)
:default :default
(transform! config-location)))))) (let [config (infra/load-config config-location)]
(when (not (s/valid? config? config))
(s/explain config? config)
(infra/exit-with-error "Bad configuration"))
(transform! config)))))))
(st/instrument 'mastodon-auth) (st/instrument 'mastodon-auth)
(st/instrument 'twitter-auth) (st/instrument 'twitter-auth)