Skip to content

Commit

Permalink
Merge pull request penpot#4926 from penpot/niwinz-fix-error-report
Browse files Browse the repository at this point in the history
🐛 Fix regression on error reporting
  • Loading branch information
superalex authored Jul 26, 2024
2 parents 0bd3d80 + a261a57 commit 4b6d354
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion backend/src/app/http.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]]}
Expand Down
12 changes: 4 additions & 8 deletions backend/src/app/http/errors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
17 changes: 9 additions & 8 deletions backend/src/app/http/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/app/main/data/persistence.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down Expand Up @@ -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))))))))))
Expand Down

0 comments on commit 4b6d354

Please sign in to comment.