Skip to content

Commit 3852406

Browse files
puredangerstuarthalloway
authored andcommitted
CLJ-2504 More options to configure error reporting
Signed-off-by: Stuart Halloway <[email protected]>
1 parent 75f8bc9 commit 3852406

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

src/clj/clojure/main.clj

+36-35
Original file line numberDiff line numberDiff line change
@@ -582,38 +582,36 @@ java -cp clojure.jar clojure.main -i init.clj script.clj args...")
582582
(null-opt args (map vector (repeat "-i") inits))))
583583

584584
(defn report-error
585-
"Create and output an exception report for a Throwable to target,
586-
and optionally exit.
585+
"Create and output an exception report for a Throwable to target.
587586
588587
Options:
589-
:target - either :temp-file or :stderr, default to :temp-file
590-
:exit - if set will System/exit with this code on err, default to 1
591-
592-
If temp file is specified but cannot be completed, falls back to stderr."
593-
[^Throwable t & {:keys [target exit]
594-
:or {target :temp-file, exit 1} :as opts}]
595-
(let [trace (Throwable->map t)
596-
triage (ex-triage trace)
597-
message (ex-str triage)
598-
report (array-map :clojure.main/message message
599-
:clojure.main/triage triage
600-
:clojure.main/trace trace)
601-
report-str (with-out-str
602-
(binding [*print-namespace-maps* false]
603-
((requiring-resolve 'clojure.pprint/pprint) report)))
604-
err-path (when (= target :temp-file)
605-
(try
606-
(let [f (.toFile (Files/createTempFile "clojure-" ".edn" (into-array FileAttribute [])))]
607-
(with-open [w (BufferedWriter. (FileWriter. f))]
608-
(binding [*out* w] (println report-str)))
609-
(.getAbsolutePath f))
610-
(catch Throwable _)))] ;; ignore, fallback to stderr
611-
(binding [*out* *err*]
612-
(if err-path
613-
(println (str message (System/lineSeparator) "Full report at:" (System/lineSeparator) err-path))
614-
(println (str report-str (System/lineSeparator) message))))
615-
(when exit
616-
(System/exit exit))))
588+
:target - \"file\" (default), \"stderr\", \"none\"
589+
590+
If file is specified but cannot be written, falls back to stderr."
591+
[^Throwable t & {:keys [target]
592+
:or {target "file"} :as opts}]
593+
(when-not (= target "none")
594+
(let [trace (Throwable->map t)
595+
triage (ex-triage trace)
596+
message (ex-str triage)
597+
report (array-map
598+
:clojure.main/message message
599+
:clojure.main/triage triage
600+
:clojure.main/trace trace)
601+
report-str (with-out-str
602+
(binding [*print-namespace-maps* false]
603+
((requiring-resolve 'clojure.pprint/pprint) report)))
604+
err-path (when (= target "file")
605+
(try
606+
(let [f (.toFile (Files/createTempFile "clojure-" ".edn" (into-array FileAttribute [])))]
607+
(with-open [w (BufferedWriter. (FileWriter. f))]
608+
(binding [*out* w] (println report-str)))
609+
(.getAbsolutePath f))
610+
(catch Throwable _)))] ;; ignore, fallback to stderr
611+
(binding [*out* *err*]
612+
(if err-path
613+
(println (str message (System/lineSeparator) "Full report at:" (System/lineSeparator) err-path))
614+
(println (str report-str (System/lineSeparator) message)))))))
617615

618616
(defn main
619617
"Usage: java -cp clojure.jar clojure.main [init-opt*] [main-opt] [arg*]
@@ -623,7 +621,8 @@ java -cp clojure.jar clojure.main -i init.clj script.clj args...")
623621
init options:
624622
-i, --init path Load a file or resource
625623
-e, --eval string Evaluate expressions in string; print non-nil values
626-
--report-stderr Print uncaught exception report to stderr
624+
--report target Report uncaught exception to \"file\" (default), \"stderr\",
625+
or \"none\", overrides System property clojure.main.report
627626
628627
main options:
629628
-m, --main ns-name Call the -main function from a namespace with args
@@ -653,8 +652,8 @@ java -cp clojure.jar clojure.main -i init.clj script.clj args...")
653652
(loop [[opt arg & more :as args] args, inits [], flags nil]
654653
(cond
655654
;; flag
656-
(contains? #{"--report-stderr"} opt)
657-
(recur (rest args) inits (merge flags {(subs opt 2) true}))
655+
(contains? #{"--report"} opt)
656+
(recur more inits (merge flags {(subs opt 2) arg}))
658657

659658
;; init opt
660659
(init-dispatch opt)
@@ -664,11 +663,13 @@ java -cp clojure.jar clojure.main -i init.clj script.clj args...")
664663
(try
665664
((main-dispatch opt) args inits)
666665
(catch Throwable t
667-
(report-error t :target (if (contains? flags "report-stderr") :stderr :temp-file))))))
666+
(report-error t :target (get flags "report" (System/getProperty "clojure.main.report" "file")))
667+
(System/exit 1)))))
668668
(try
669669
(repl-opt nil nil)
670670
(catch Throwable t
671-
(report-error :target :temp-file))))
671+
(report-error t :target "file")
672+
(System/exit 1))))
672673
(finally
673674
(flush))))
674675

0 commit comments

Comments
 (0)