diff --git a/backend/src/app/http.clj b/backend/src/app/http.clj index c45c95c1cd..672d1ec60f 100644 --- a/backend/src/app/http.clj +++ b/backend/src/app/http.clj @@ -150,8 +150,8 @@ [["" {:middleware [[mw/server-timing] [mw/params] [mw/format-response] - [mw/errors errors/handle] [mw/parse-request] + [mw/errors errors/handle] [session/soft-auth cfg] [actoken/soft-auth cfg] [mw/restrict-methods]]} diff --git a/backend/src/app/http/errors.clj b/backend/src/app/http/errors.clj index 14f4cb2233..8101db116c 100644 --- a/backend/src/app/http/errors.clj +++ b/backend/src/app/http/errors.clj @@ -14,32 +14,28 @@ [app.http :as-alias http] [app.http.access-token :as-alias actoken] [app.http.session :as-alias session] + [app.util.inet :as inet] [clojure.spec.alpha :as s] - [cuerdas.core :as str] [ring.request :as rreq] [ring.response :as rres])) -(defn- parse-client-ip - [request] - (or (some-> (rreq/get-header request "x-forwarded-for") (str/split ",") first) - (rreq/get-header request "x-real-ip") - (rreq/remote-addr request))) - (defn request->context "Extracts error report relevant context data from request." [request] (let [claims (-> {} (into (::session/token-claims request)) (into (::actoken/token-claims request)))] + {:request/path (:path request) :request/method (:method request) :request/params (:params request) :request/user-agent (rreq/get-header request "user-agent") - :request/ip-addr (parse-client-ip request) + :request/ip-addr (inet/parse-request request) :request/profile-id (:uid claims) :version/frontend (or (rreq/get-header request "x-frontend-version") "unknown") :version/backend (:full cf/version)})) + (defmulti handle-error (fn [cause _ _] (-> cause ex-data :type))) diff --git a/backend/src/app/http/middleware.clj b/backend/src/app/http/middleware.clj index a6eabd9a4f..f70e102adf 100644 --- a/backend/src/app/http/middleware.clj +++ b/backend/src/app/http/middleware.clj @@ -10,6 +10,7 @@ [app.common.logging :as l] [app.common.transit :as t] [app.config :as cf] + [app.http.errors :as errors] [clojure.data.json :as json] [cuerdas.core :as str] [ring.request :as rreq] @@ -70,12 +71,12 @@ :else request))) - (handle-error [cause] + (handle-error [cause request] (cond (instance? RuntimeException cause) (if-let [cause (ex-cause cause)] - (handle-error cause) - (throw cause)) + (handle-error cause request) + (errors/handle cause request)) (instance? RequestTooBigException cause) (ex/raise :type :validation @@ -89,14 +90,14 @@ :cause cause) :else - (throw cause)))] + (errors/handle cause request)))] (fn [request] (if (= (rreq/method request) :post) - (let [request (ex/try! (process-request request))] - (if (ex/exception? request) - (handle-error request) - (handler request))) + (try + (-> request process-request handler) + (catch Throwable cause + (handle-error cause request))) (handler request))))) (def parse-request diff --git a/frontend/src/app/main/data/persistence.cljs b/frontend/src/app/main/data/persistence.cljs index 2e917e7844..9a17eda3fc 100644 --- a/frontend/src/app/main/data/persistence.cljs +++ b/frontend/src/app/main/data/persistence.cljs @@ -12,7 +12,6 @@ [app.common.uuid :as uuid] [app.main.data.changes :as dch] [app.main.repo :as rp] - [app.util.router :as rt] [beicon.v2.core :as rx] [potok.v2.core :as ptk])) @@ -131,8 +130,7 @@ (rx/concat (if (= :authentication (:type cause)) (rx/empty) - (rx/of (rt/assign-exception cause) - (ptk/data-event ::error cause) + (rx/of (ptk/data-event ::error cause) (update-status :error))) (rx/of (discard-persistence-state)) (rx/throw cause))))))))))