l3nz / cli-matic

Compact, hands-free [sub]command line parsing library for Clojure.
Eclipse Public License 2.0
361 stars 29 forks source link

[Ftr] Managed exceptions to terminate script with an error #102

Closed l3nz closed 4 years ago

l3nz commented 4 years ago

Is your feature request related to a problem?

While an exception is/should be a technical error, sometimes it is useful isn a script to throw an exception to abort it. This works, but like in this case, it's ugly because a full stack trace is printed.

JVM Exception: #error {
 :cause (("instance not found"))
 :via
 [{:type java.lang.IllegalArgumentException
   :message (("instance not found"))
   :at [tools.utils$abort invokeStatic utils.clj 12]}]
 :trace
 [[tools.utils$abort invokeStatic utils.clj 12]
  [tools.utils$abort doInvoke utils.clj 10]
  [clojure.lang.RestFn invoke RestFn.java 408]
  [tools.utils$abortNil invokeStatic utils.clj 14]
  [tools.utils$abortNil doInvoke utils.clj 14]
  [clojure.lang.RestFn invoke RestFn.java 423]
  [tools.qmlive$getInstance invokeStatic qmlive.clj 49]
  [tools.qmlive$getInstance invoke qmlive.clj 45]
  [eml$send_email_live_generic invokeStatic eml.clj 106]
  [eml$send_email_live_generic invoke eml.clj 102]
  [eml$send_email_live invokeStatic eml.clj 113]
  [eml$send_email_live invoke eml.clj 111]
  [cli_matic.core$invoke_subcmd invokeStatic core.cljc 505]
  [cli_matic.core$invoke_subcmd invoke core.cljc 484]
  [cli_matic.core$run_cmd_STAR_ invokeStatic core.cljc 546]
  [cli_matic.core$run_cmd_STAR_ invoke core.cljc 520]
  [cli_matic.core$run_cmd invokeStatic core.cljc 569]
  [cli_matic.core$run_cmd invoke core.cljc 559]
  [eml$_main invokeStatic eml.clj 237]
  [eml$_main doInvoke eml.clj 234]
  [clojure.lang.RestFn applyTo RestFn.java 137]
  [clojure.lang.Var applyTo Var.java 702]
  [clojure.core$apply invokeStatic core.clj 657]
  [clojure.main$main_opt invokeStatic main.clj 317]
  [clojure.main$main_opt invoke main.clj 313]
  [clojure.main$main invokeStatic main.clj 424]
  [clojure.main$main doInvoke main.clj 387]
  [clojure.lang.RestFn applyTo RestFn.java 137]
  [clojure.lang.Var applyTo Var.java 702]
  [clojure.main main main.java 37]]}

In a case like this, aborting the script printing "instance not found" would be what you expect, not to go through a 37-line exception.

Describe the solution you'd like

We could find a way to flag some exceptions as "cli-matic's own", so it would only print a message and exit with status code you specify. Other exceptions are "technical" so the stack trace is OK. Maybe CLI-matic could offer an abort function to raise such exceptions.

Of course this should work both on CLJ and CLJS where exceptions differ significantly.