Explorar o código

fix: silent failures on commands

Also add user friendly error handling when api server isn't turned on
Gabriel Horner hai 2 meses
pai
achega
d5c46433b2

+ 9 - 4
deps/cli/src/logseq/cli.cljs

@@ -6,7 +6,7 @@
             [clojure.string :as string]
             [logseq.cli.common.graph :as cli-common-graph]
             [logseq.cli.spec :as cli-spec]
-            [nbb.error :as error]
+            [nbb.error]
             [promesa.core :as p]))
 
 (defn- format-commands [{:keys [table]}]
@@ -53,8 +53,13 @@
   "Lazy load fn to speed up start time. After nbb requires ~30 namespaces, start time gets close to 1s"
   [fn-sym]
   (fn [& args]
-    (p/let [_ (require (symbol (namespace fn-sym)))]
-      (apply (resolve fn-sym) args))))
+    (-> (p/let [_ (require (symbol (namespace fn-sym)))]
+          (apply (resolve fn-sym) args))
+        (p/catch (fn [err]
+                   (if (= :sci/error (:type (ex-data err)))
+                     (nbb.error/print-error-report err)
+                     (js/console.error "Error:" err))
+                   (js/process.exit 1))))))
 
 (def ^:private table
   [{:cmds ["list"] :desc "List graphs"
@@ -100,6 +105,6 @@
                                  (throw (ex-info msg data)))
                                (js/process.exit 1))})
     (catch ^:sci/error js/Error e
-      (error/print-error-report e))))
+      (nbb.error/print-error-report e))))
 
 #js {:main -main}

+ 1 - 3
deps/cli/src/logseq/cli/commands/search.cljs

@@ -40,6 +40,4 @@
                                            (map #(string/replace % "\n" "\\\\n"))
                                            (map highlight-content-query)))))))
           (cli-util/api-handle-error-response resp)))
-      (p/catch (fn [err]
-                 (js/console.error "Error:" err)
-                 (js/process.exit 1)))))
+      (p/catch cli-util/command-catch-handler)))

+ 16 - 1
deps/cli/src/logseq/cli/util.cljs

@@ -1,8 +1,10 @@
 (ns ^:node-only logseq.cli.util
   "Util fns"
   (:require ["path" :as node-path]
+            [clojure.string :as string]
             [logseq.cli.common.graph :as cli-common-graph]
-            [logseq.db.common.sqlite :as common-sqlite]))
+            [logseq.db.common.sqlite :as common-sqlite]
+            [nbb.error]))
 
 (defn get-graph-dir
   [graph]
@@ -27,4 +29,17 @@
   [resp]
   (js/console.error "Error: API Server responded with status" (.-status resp)
                     (when (.-statusText resp) (str "and body " (pr-str (.-statusText resp)))))
+  (js/process.exit 1))
+
+(defn command-catch-handler
+  "Default p/catch handler for commands which handles sci errors and HTTP API Server connections gracefully"
+  [err]
+  (cond
+    (= :sci/error (:type (ex-data err)))
+    (nbb.error/print-error-report err)
+    (string/includes? (some->> err .-cause .-message str) "ECONNREFUSED")
+    (do (js/console.error "Error: Failed to connect to HTTP API Server with error" (pr-str (.-message err)))
+        (js/console.log "Make sure the HTTP API Server is turned on."))
+    :else
+    (js/console.error "Error:" err))
   (js/process.exit 1))