From bf6f19dc97980f9d376dc4de48143fde1bec2438 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 6 May 2021 13:58:49 +0200 Subject: [PATCH] Tasks: don't print context on shell error --- src/babashka/impl/error_handler.clj | 84 +++++++++++++++-------------- src/babashka/impl/tasks.clj | 9 ++-- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/babashka/impl/error_handler.clj b/src/babashka/impl/error_handler.clj index 7ba19114..d0ad4750 100644 --- a/src/babashka/impl/error_handler.clj +++ b/src/babashka/impl/error_handler.clj @@ -79,8 +79,8 @@ (defn error-handler [^Exception e opts] (binding [*out* *err*] (let [d (ex-data e) - cause-exit (some-> e ex-cause ex-data :exit) - exit-code (or (:exit d) cause-exit) + cause-exit (some-> e ex-cause ex-data :babashka/exit) + exit-code (or (:babashka/exit d) cause-exit) sci-error? (isa? (:type d) :sci/error) ex-name (when sci-error? (some-> ^Throwable (ex-cause e) @@ -88,41 +88,45 @@ stacktrace (some-> d :sci.impl/callstack cs/stacktrace)] - (if exit-code [nil exit-code] - (do - (ruler "Error") - (println "Type: " (or - ex-name - (.. e getClass getName))) - (when-let [m (.getMessage e)] - (println (str "Message: " m))) - (let [{:keys [:file :line :column]} d] - (when line - (println (str "Location: " - (when file (str file ":")) - line ":" column"")))) - (when-let [phase (cs/phase e stacktrace)] - (println "Phase: " phase)) - (println) - (when-let [ec (when sci-error? - (error-context e opts))] - (ruler "Context") - (println ec) - (println)) - (when-let [locals (not-empty (:locals d))] - (ruler "Locals") - (print-locals locals) - (println)) - (when sci-error? - (when-let - [st (let [st (with-out-str - (when stacktrace - (print-stacktrace stacktrace opts)))] - (when-not (str/blank? st) st))] - (ruler "Stack trace") - (println st))) - (when (:debug opts) - (ruler "Exception") - (print-stack-trace e)) - (flush) - [nil 1]))))) + (if exit-code + (do + (when-let [m (.getMessage e)] + (println m)) + [nil exit-code]) + (do + (ruler "Error") + (println "Type: " (or + ex-name + (.. e getClass getName))) + (when-let [m (.getMessage e)] + (println (str "Message: " m))) + (let [{:keys [:file :line :column]} d] + (when line + (println (str "Location: " + (when file (str file ":")) + line ":" column"")))) + (when-let [phase (cs/phase e stacktrace)] + (println "Phase: " phase)) + (println) + (when-let [ec (when sci-error? + (error-context e opts))] + (ruler "Context") + (println ec) + (println)) + (when-let [locals (not-empty (:locals d))] + (ruler "Locals") + (print-locals locals) + (println)) + (when sci-error? + (when-let + [st (let [st (with-out-str + (when stacktrace + (print-stacktrace stacktrace opts)))] + (when-not (str/blank? st) st))] + (ruler "Stack trace") + (println st))) + (when (:debug opts) + (ruler "Exception") + (print-stack-trace e)) + (flush) + [nil 1]))))) diff --git a/src/babashka/impl/tasks.clj b/src/babashka/impl/tasks.clj index 882028cc..38c72b70 100644 --- a/src/babashka/impl/tasks.clj +++ b/src/babashka/impl/tasks.clj @@ -46,11 +46,10 @@ (continue proc)) zero-exit?)] (if continue? proc - (do (when-not zero-exit? - (binding [*out* *err*] - (println "Received non-zero exit code in task:" (:name @task)))) - (throw (ex-info (str "Error during task: " (:name @task)) - {:proc proc :task task :exit exit-code})))))))) + (throw (ex-info (str "Error while executing task: " (:name @task)) + {:proc proc + :task task + :babashka/exit exit-code}))))))) (def default-opts {:in :inherit