Merge pull request #4129 from penpot/niwinz-stagoing-debug-on-error

 Add the ability to download a report on internal error
This commit is contained in:
Alejandro 2024-02-12 16:02:05 +01:00 committed by GitHub
commit dd3040c56f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 160 additions and 98 deletions

View file

@ -149,9 +149,11 @@
(let [params (decode params)] (let [params (decode params)]
(if (validate params) (if (validate params)
(f cfg params) (f cfg params)
(ex/raise :type :validation
:code :params-validation (let [params (d/without-qualified params)]
::sm/explain (explain params)))))) (ex/raise :type :validation
:code :params-validation
::sm/explain (explain params)))))))
f)) f))
(defn- wrap-output-validation (defn- wrap-output-validation

View file

@ -42,30 +42,28 @@
(def ^:private (def ^:private
schema:update-file schema:update-file
(sm/define [:map {:title "update-file"}
[:map {:title "update-file"} [:id ::sm/uuid]
[:id ::sm/uuid] [:session-id ::sm/uuid]
[:session-id ::sm/uuid] [:revn {:min 0} :int]
[:revn {:min 0} :int] [:features {:optional true} ::cfeat/features]
[:features {:optional true} ::cfeat/features] [:changes {:optional true} [:vector ::cpc/change]]
[:changes {:optional true} [:vector ::cpc/change]] [:changes-with-metadata {:optional true}
[:changes-with-metadata {:optional true} [:vector [:map
[:vector [:map [:changes [:vector ::cpc/change]]
[:changes [:vector ::cpc/change]] [:hint-origin {:optional true} :keyword]
[:hint-origin {:optional true} :keyword] [:hint-events {:optional true} [:vector :string]]]]]
[:hint-events {:optional true} [:vector :string]]]]] [:skip-validate {:optional true} :boolean]])
[:skip-validate {:optional true} :boolean]]))
(def ^:private (def ^:private
schema:update-file-result schema:update-file-result
(sm/define [:vector {:title "update-file-result"}
[:vector {:title "update-file-result"} [:map
[:map [:changes [:vector ::cpc/change]]
[:changes [:vector ::cpc/change]] [:file-id ::sm/uuid]
[:file-id ::sm/uuid] [:id ::sm/uuid]
[:id ::sm/uuid] [:revn {:min 0} :int]
[:revn {:min 0} :int] [:session-id ::sm/uuid]]])
[:session-id ::sm/uuid]]]))
;; --- HELPERS ;; --- HELPERS
@ -73,14 +71,26 @@
;; to all clients using it. ;; to all clients using it.
(def ^:private library-change-types (def ^:private library-change-types
#{:add-color :mod-color :del-color #{:add-color
:add-media :mod-media :del-media :mod-color
:add-component :mod-component :del-component :restore-component :del-color
:add-typography :mod-typography :del-typography}) :add-media
:mod-media
:del-media
:add-component
:mod-component
:del-component
:restore-component
:add-typography
:mod-typography
:del-typography})
(def ^:private file-change-types (def ^:private file-change-types
#{:add-obj :mod-obj :del-obj #{:add-obj
:reg-objects :mov-objects}) :mod-obj
:del-obj
:reg-objects
:mov-objects})
(defn- library-change? (defn- library-change?
[{:keys [type] :as change}] [{:keys [type] :as change}]

View file

@ -16,8 +16,7 @@
(def ^:private (def ^:private
sql:delete-completed-tasks sql:delete-completed-tasks
"delete from task_completed "DELETE FROM task WHERE scheduled_at < now() - ?::interval")
where scheduled_at < now() - ?::interval")
(defmethod ig/pre-init-spec ::handler [_] (defmethod ig/pre-init-spec ::handler [_]
(s/keys :req [::db/pool])) (s/keys :req [::db/pool]))

View file

@ -242,7 +242,12 @@
([] ([]
(remove (comp qualified-keyword? key))) (remove (comp qualified-keyword? key)))
([data] ([data]
(into {} (without-qualified) data))) (reduce-kv (fn [data k _]
(if (qualified-keyword? k)
(dissoc data k)
data))
data
data)))
(defn without-keys (defn without-keys
"Return a map without the keys provided "Return a map without the keys provided

View file

@ -58,6 +58,7 @@
"application/zip" ".zip" "application/zip" ".zip"
"application/penpot" ".penpot" "application/penpot" ".penpot"
"application/pdf" ".pdf" "application/pdf" ".pdf"
"text/plain" ".txt"
nil)) nil))
(s/def ::id uuid?) (s/def ::id uuid?)

View file

@ -16,7 +16,6 @@
[app.main.features :as features] [app.main.features :as features]
[app.main.repo :as rp] [app.main.repo :as rp]
[app.main.store :as st] [app.main.store :as st]
[app.util.router :as rt]
[app.util.time :as dt] [app.util.time :as dt]
[beicon.v2.core :as rx] [beicon.v2.core :as rx]
[okulary.core :as l] [okulary.core :as l]
@ -177,19 +176,11 @@
(rx/of (shapes-changes-persisted-finished)))))) (rx/of (shapes-changes-persisted-finished))))))
(rx/catch (fn [cause] (rx/catch (fn [cause]
(cond (if (instance? js/TypeError cause)
(= :authentication (:type cause))
(rx/throw cause)
(instance? js/TypeError cause)
(->> (rx/timer 2000) (->> (rx/timer 2000)
(rx/map (fn [_] (rx/map (fn [_]
(persist-changes file-id file-revn changes pending-commits)))) (persist-changes file-id file-revn changes pending-commits))))
(rx/throw cause)))))))))
:else
(rx/concat
(rx/of (rt/assign-exception cause))
(rx/throw cause))))))))))
;; Event to be thrown after the changes have been persisted ;; Event to be thrown after the changes have been persisted
(defn shapes-changes-persisted-finished (defn shapes-changes-persisted-finished

View file

@ -116,7 +116,6 @@
(defmethod ptk/handle-error :validation (defmethod ptk/handle-error :validation
[{:keys [code] :as error}] [{:keys [code] :as error}]
(print-group! "Validation Error" (print-group! "Validation Error"
(fn [] (fn []
(print-data! error) (print-data! error)
@ -130,12 +129,7 @@
:timeout 3000}))) :timeout 3000})))
:else :else
(let [message (tr "errors.generic-validation")] (st/async-emit! (rt/assign-exception error))))
(st/async-emit!
(msg/show {:content message
:type :error
:timeout 3000})))))
;; This is a pure frontend error that can be caused by an active ;; This is a pure frontend error that can be caused by an active
@ -256,12 +250,7 @@
(defmethod ptk/handle-error :server-error (defmethod ptk/handle-error :server-error
[error] [error]
(ts/schedule (st/async-emit! (rt/assign-exception error))
#(st/emit!
(msg/show {:content "Something wrong has happened (on backend)."
:type :error
:timeout 3000})))
(print-group! "Server Error" (print-group! "Server Error"
(fn [] (fn []
(print-data! (dissoc error :data)) (print-data! (dissoc error :data))

View file

@ -23,28 +23,31 @@
(rx/of nil) (rx/of nil)
(= 502 status) (= 502 status)
(rx/throw {:type :bad-gateway}) (rx/throw (ex-info "http error" {:type :bad-gateway}))
(= 503 status) (= 503 status)
(rx/throw {:type :service-unavailable}) (rx/throw (ex-info "http error" {:type :service-unavailable}))
(= 0 (:status response)) (= 0 (:status response))
(rx/throw {:type :offline}) (rx/throw (ex-info "http error" {:type :offline}))
(= 200 status) (= 200 status)
(rx/of body) (rx/of body)
(= 413 status) (= 413 status)
(rx/throw {:type :validation (rx/throw (ex-info "http error"
:code :request-body-too-large}) {:type :validation
:code :request-body-too-large}))
(and (>= status 400) (map? body)) (and (>= status 400) (map? body))
(rx/throw body) (rx/throw (ex-info "http error" body))
:else :else
(rx/throw {:type :unexpected-error (rx/throw
(ex-info "http error"
{:type :unexpected-error
:status status :status status
:data body}))) :data body}))))
(def default-options (def default-options
{:update-file {:query-params [:id]} {:update-file {:query-params [:id]}

View file

@ -58,22 +58,22 @@
(defonce last-events (defonce last-events
(let [buffer (atom []) (let [buffer (atom [])
allowed #{:app.main.data.workspace/initialize-page omitset #{:potok.v2.core/undefined
:app.main.data.workspace/finalize-page :app.main.data.workspace.persistence/update-persistence-status
:app.main.data.workspace/initialize-file :app.main.data.websocket/send-message
:app.main.data.workspace/finalize-file}] :app.main.data.workspace.notifications/handle-pointer-send
:app.util.router/assign-exception}]
(->> (rx/merge (->> (rx/merge
(->> stream (->> stream
(rx/filter (ptk/type? :app.main.data.workspace.changes/commit-changes)) (rx/filter (ptk/type? :app.main.data.workspace.changes/commit-changes))
(rx/map #(-> % deref :hint-origin str)) (rx/map #(-> % deref :hint-origin)))
(rx/pipe (rxo/distinct-contiguous))) (rx/map ptk/type stream))
(->> stream (rx/filter #(not (contains? omitset %)))
(rx/map ptk/type) (rx/map str)
(rx/filter #(contains? allowed %)) (rx/pipe (rxo/distinct-contiguous))
(rx/map str)))
(rx/scan (fn [buffer event] (rx/scan (fn [buffer event]
(cond-> (conj buffer event) (cond-> (conj buffer event)
(> (count buffer) 20) (> (count buffer) 50)
(pop))) (pop)))
#queue []) #queue [])
(rx/subs! #(reset! buffer (vec %)))) (rx/subs! #(reset! buffer (vec %))))

View file

@ -128,12 +128,11 @@
{:keys [file-id]} path-params] {:keys [file-id]} path-params]
[:? {} [:? {}
(if (:token query-params) (if (:token query-params)
[:> static/static-header {} [:> static/error-container {}
[:div.image i/unchain] [:div.image i/unchain]
[:div.main-message (tr "viewer.breaking-change.message")] [:div.main-message (tr "viewer.breaking-change.message")]
[:div.desc-message (tr "viewer.breaking-change.description")]] [:div.desc-message (tr "viewer.breaking-change.description")]]
[:& viewer-page [:& viewer-page
{:page-id page-id {:page-id page-id
:file-id file-id :file-id file-id

View file

@ -7,19 +7,21 @@
(ns app.main.ui.static (ns app.main.ui.static
(:require-macros [app.main.style :as stl]) (:require-macros [app.main.style :as stl])
(:require (:require
[app.common.data :as d]
[app.common.pprint :as pp]
[app.main.store :as st] [app.main.store :as st]
[app.main.ui.icons :as i] [app.main.ui.icons :as i]
[app.util.dom :as dom]
[app.util.globals :as globals] [app.util.globals :as globals]
[app.util.i18n :refer [tr]] [app.util.i18n :refer [tr]]
[app.util.object :as obj]
[app.util.router :as rt] [app.util.router :as rt]
[app.util.webapi :as wapi]
[rumext.v2 :as mf])) [rumext.v2 :as mf]))
(mf/defc static-header (mf/defc error-container
{::mf/wrap-props false} {::mf/wrap-props false}
[props] [{:keys [children]}]
(let [children (obj/get props "children") (let [on-click (mf/use-callback #(set! (.-href globals/location) "/"))]
on-click (mf/use-callback #(set! (.-href globals/location) "/"))]
[:section {:class (stl/css :exception-layout)} [:section {:class (stl/css :exception-layout)}
[:button [:button
{:class (stl/css :exception-header) {:class (stl/css :exception-header)
@ -34,13 +36,13 @@
(mf/defc invalid-token (mf/defc invalid-token
[] []
[:> static-header {} [:> error-container {}
[:div {:class (stl/css :main-message)} (tr "errors.invite-invalid")] [:div {:class (stl/css :main-message)} (tr "errors.invite-invalid")]
[:div {:class (stl/css :desc-message)} (tr "errors.invite-invalid.info")]]) [:div {:class (stl/css :desc-message)} (tr "errors.invite-invalid.info")]])
(mf/defc not-found (mf/defc not-found
[] []
[:> static-header {} [:> error-container {}
[:div {:class (stl/css :main-message)} (tr "labels.not-found.main-message")] [:div {:class (stl/css :main-message)} (tr "labels.not-found.main-message")]
[:div {:class (stl/css :desc-message)} (tr "labels.not-found.desc-message")]]) [:div {:class (stl/css :desc-message)} (tr "labels.not-found.desc-message")]])
@ -49,7 +51,7 @@
(let [handle-retry (let [handle-retry
(mf/use-callback (mf/use-callback
(fn [] (st/emit! (rt/assign-exception nil))))] (fn [] (st/emit! (rt/assign-exception nil))))]
[:> static-header {} [:> error-container {}
[:div {:class (stl/css :main-message)} (tr "labels.bad-gateway.main-message")] [:div {:class (stl/css :main-message)} (tr "labels.bad-gateway.main-message")]
[:div {:class (stl/css :desc-message)} (tr "labels.bad-gateway.desc-message")] [:div {:class (stl/css :desc-message)} (tr "labels.bad-gateway.desc-message")]
[:div {:class (stl/css :sign-info)} [:div {:class (stl/css :sign-info)}
@ -57,27 +59,88 @@
(mf/defc service-unavailable (mf/defc service-unavailable
[] []
(let [handle-retry (let [on-click (mf/use-fn #(st/emit! (rt/assign-exception nil)))]
(mf/use-callback [:> error-container {}
(fn [] (st/emit! (rt/assign-exception nil))))]
[:> static-header {}
[:div {:class (stl/css :main-message)} (tr "labels.service-unavailable.main-message")] [:div {:class (stl/css :main-message)} (tr "labels.service-unavailable.main-message")]
[:div {:class (stl/css :desc-message)} (tr "labels.service-unavailable.desc-message")] [:div {:class (stl/css :desc-message)} (tr "labels.service-unavailable.desc-message")]
[:div {:class (stl/css :sign-info)} [:div {:class (stl/css :sign-info)}
[:button {:on-click handle-retry} (tr "labels.retry")]]])) [:button {:on-click on-click} (tr "labels.retry")]]]))
(defn generate-report
[data]
(let [team-id (:current-team-id @st/state)
profile-id (:profile-id @st/state)
trace (:app.main.errors/trace data)
instance (:app.main.errors/instance data)
content (with-out-str
(println "Hint: " (or (:hint data) (ex-message instance) "--"))
(println "Prof ID:" (str (or profile-id "--")))
(println "Team ID:" (str (or team-id "--")))
(when-let [file-id (:file-id data)]
(println "File ID:" (str file-id)))
(println)
(println "Data:")
(loop [data data]
(-> (d/without-qualified data)
(dissoc :explain)
(d/update-when :data (constantly "(...)"))
(pp/pprint {:level 8 :length 10}))
(println)
(when-let [explain (:explain data)]
(print explain))
(when (and (= :server-error (:type data))
(contains? data :data))
(recur (:data data))))
(println "Trace:")
(println trace)
(println)
(println "Last events:")
(pp/pprint @st/last-events {:length 200})
(println))]
(wapi/create-blob content "text/plain")))
(mf/defc internal-error (mf/defc internal-error
[] {::mf/props :obj}
(let [handle-retry [{:keys [data]}]
(mf/use-callback (let [on-click (mf/use-fn #(st/emit! (rt/assign-exception nil)))
(fn [] (st/emit! (rt/assign-exception nil))))] report-uri (mf/use-ref nil)
[:> static-header {}
on-download
(mf/use-fn
(fn [event]
(dom/prevent-default event)
(when-let [uri (mf/ref-val report-uri)]
(dom/trigger-download-uri "report" "text/plain" uri))))]
(mf/with-effect [data]
(let [report (generate-report data)
uri (wapi/create-uri report)]
(mf/set-ref-val! report-uri uri)
(fn []
(wapi/revoke-uri uri))))
[:> error-container {}
[:div {:class (stl/css :main-message)} (tr "labels.internal-error.main-message")] [:div {:class (stl/css :main-message)} (tr "labels.internal-error.main-message")]
[:div {:class (stl/css :desc-message)} (tr "labels.internal-error.desc-message")] [:div {:class (stl/css :desc-message)} (tr "labels.internal-error.desc-message")]
[:a {:on-click on-download} "Download report.txt"]
[:div {:class (stl/css :sign-info)} [:div {:class (stl/css :sign-info)}
[:button {:on-click handle-retry} (tr "labels.retry")]]])) [:button {:on-click on-click} (tr "labels.retry")]]]))
(mf/defc exception-page (mf/defc exception-page
{::mf/props :obj}
[{:keys [data] :as props}] [{:keys [data] :as props}]
(case (:type data) (case (:type data)
:not-found :not-found
@ -89,4 +152,4 @@
:service-unavailable :service-unavailable
[:& service-unavailable] [:& service-unavailable]
[:& internal-error])) [:> internal-error props]))