Procházet zdrojové kódy

fix(api): unexpected failures in api should 500

Previously we were getting 200s which makes handling more complicated
(check status and async response) and was already resulting in buggy
error handling
Gabriel Horner před 4 měsíci
rodič
revize
a8e2b173ce

+ 1 - 5
deps/cli/src/logseq/cli/commands/append.cljs

@@ -9,10 +9,6 @@
   (let [text (string/join " " args)]
     (-> (p/let [resp (cli-util/api-fetch api-server-token "logseq.app.append_block_in_page" [text nil nil])]
           (if (= 200 (.-status resp))
-            (p/let [body (.json resp)]
-              (if (.-error body)
-                (cli-util/error (str "Failed to append.\nAPI Error: "
-                                     (string/replace (.-error body) "\n" "\\\\n")))
-                (println "Success!")))
+            (println "Success!")
             (cli-util/api-handle-error-response resp)))
         (p/catch cli-util/command-catch-handler))))

+ 11 - 4
deps/cli/src/logseq/cli/util.cljs

@@ -4,6 +4,7 @@
             [clojure.string :as string]
             [logseq.cli.common.graph :as cli-common-graph]
             [logseq.db.common.sqlite :as common-sqlite]
+            [promesa.core :as p]
             [nbb.error]))
 
 (defn get-graph-dir
@@ -26,11 +27,17 @@
                                        :args args}))})))
 
 (defn api-handle-error-response
-  "Handles a non 200 response"
+  "Handles a non 200 response. For 500 return full response to provide more detail"
   [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))
+  (if (= 500 (.-status resp))
+    (p/let [body (.text resp)]
+      (js/console.error "Error: API Server responded with status" (.-status resp)
+                        "\nAPI Response:" (pr-str body))
+      (js/process.exit 1))
+    (do
+      (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"

+ 12 - 9
src/electron/electron/server.cljs

@@ -1,17 +1,17 @@
 (ns electron.server
-  (:require ["fastify" :as Fastify]
-            ["@fastify/cors" :as FastifyCORS]
+  (:require ["@fastify/cors" :as FastifyCORS]
             ["electron" :refer [ipcMain]]
+            ["fastify" :as Fastify]
             ["fs-extra" :as fs-extra]
             ["path" :as node-path]
-            [clojure.string :as string]
-            [promesa.core :as p]
-            [cljs-bean.core :as bean]
-            [electron.utils :as utils]
             [camel-snake-kebab.core :as csk]
-            [electron.logger :as logger]
+            [cljs-bean.core :as bean]
+            [clojure.string :as string]
             [electron.configs :as cfgs]
-            [electron.window :as window]))
+            [electron.logger :as logger]
+            [electron.utils :as utils]
+            [electron.window :as window]
+            [promesa.core :as p]))
 
 (defonce ^:private *win (atom nil))
 (defonce ^:private *server (atom nil))
@@ -106,7 +106,10 @@
   (if-let [^js body (.-body req)]
     (if-let [method (resolve-real-api-method (.-method body))]
       (-> (invoke-logseq-api! method (.-args body))
-          (p/then #(.send rep %))
+          (p/then #(do
+                     ;; Responses with an :error key are unexpected failures from electron.listener
+                     (when (aget % "error") (.code rep 500))
+                     (.send rep %)))
           (p/catch #(.send rep %)))
       (-> rep
           (.code 400)