diff --git a/backend/src/app/http.clj b/backend/src/app/http.clj index c45c95c1c..672d1ec60 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 14f4cb223..8101db116 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 a6eabd9a4..f70e102ad 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