Report errors on events.

This commit is contained in:
Andrey Antukh 2021-05-05 15:22:04 +02:00 committed by Andrés Moya
parent abb244c940
commit 3ce4769e8d
2 changed files with 66 additions and 38 deletions

View file

@ -76,6 +76,7 @@
{:status 500 {:status 500
:body {:type :server-error :body {:type :server-error
:code :assertion
:data (-> edata :data (-> edata
(assoc :explain (explain-error edata)) (assoc :explain (explain-error edata))
(dissoc :data))}})) (dissoc :data))}}))
@ -103,6 +104,7 @@
:cause error) :cause error)
{:status 500 {:status 500
:body {:type :server-error :body {:type :server-error
:code :unexpected
:hint (ex-message error) :hint (ex-message error)
:data edata}})))) :data edata}}))))
@ -132,7 +134,8 @@
:else :else
{:status 500 {:status 500
:body {:type :server-timeout :body {:type :server-error
:code :psql-exception
:hint (ex-message error) :hint (ex-message error)
:state state}}))) :state state}})))

View file

@ -226,8 +226,9 @@
[error] [error]
(ts/schedule (ts/schedule
(st/emitf (st/emitf
(dm/show {:content "Unexpected validation error (server side)." (dm/show {:content "Unexpected validation error."
:type :error}))) :type :error
:timeout 3000})))
;; Print to the console some debug info. ;; Print to the console some debug info.
(js/console.group "Validation Error") (js/console.group "Validation Error")
@ -243,58 +244,82 @@
;; assertion (assertion that is preserved on production builds). From ;; assertion (assertion that is preserved on production builds). From
;; the user perspective this should be treated as internal error. ;; the user perspective this should be treated as internal error.
(defmethod ptk/handle-error :assertion (defmethod ptk/handle-error :assertion
[{:keys [data stack message context] :as error}] [{:keys [data stack message hint context] :as error}]
(ts/schedule (let [message (or message hint)
(st/emitf (dm/show {:content "Internal error: assertion." context (str/fmt "ns: '%s'\nname: '%s'\nfile: '%s:%s'"
:type :error})))
;; Print to the console some debugging info
(js/console.group message)
(js/console.info (str/format "ns: '%s'\nname: '%s'\nfile: '%s:%s'"
(:ns context) (:ns context)
(:name context) (:name context)
(str cfg/public-uri "/js/cljs-runtime/" (:file context)) (str cfg/public-uri "/js/cljs-runtime/" (:file context))
(:line context))) (:line context))]
(ts/schedule
(st/emitf
(dm/show {:content "Internal error: assertion."
:type :error
:timeout 3000})
(ptk/event ::ev/event
{::ev/type "exception"
::ev/name "assertion-error"
:message message
:context context
:trace stack})))
;; Print to the console some debugging info
(js/console.group message)
(js/console.info context)
(js/console.groupCollapsed "Stack Trace") (js/console.groupCollapsed "Stack Trace")
(js/console.info stack) (js/console.info stack)
(js/console.groupEnd "Stack Trace") (js/console.groupEnd "Stack Trace")
(js/console.error (with-out-str (expound/printer data))) (js/console.error (with-out-str (expound/printer data)))
(js/console.groupEnd message)) (js/console.groupEnd message)))
;; This happens when the backed server fails to process the ;; This happens when the backed server fails to process the
;; request. This can be caused by an internal assertion or any other ;; request. This can be caused by an internal assertion or any other
;; uncontrolled error. ;; uncontrolled error.
(defmethod ptk/handle-error :server-error (defmethod ptk/handle-error :server-error
[{:keys [data] :as error}] [{:keys [data hint] :as error}]
(let [hint (or hint (:hint data) (:message data))
info (with-out-str (pprint (dissoc data :explain)))
expl (:explain data)]
(ts/schedule (ts/schedule
(st/emitf (dm/show (st/emitf
{:content "Something wrong has happened (on backend)." (dm/show {:content "Something wrong has happened (on backend)."
:type :error}))) :type :error
:timeout 3000})
(ptk/event ::ev/event
{::ev/type "exception"
::ev/name "server-error"
:hint hint
:info info
:explain expl})))
(js/console.group "Internal Server Error:") (js/console.group "Internal Server Error:")
(js/console.error "hint:" (or (:hint data) (:message data))) (js/console.error "hint:" hint)
(js/console.info (js/console.info info)
(with-out-str (when expl (js/console.error expl))
(pprint (dissoc data :explain)))) (js/console.groupEnd "Internal Server Error:")))
(when-let [explain (:explain data)]
(js/console.error explain))
(js/console.groupEnd "Internal Server Error:"))
(defmethod ptk/handle-error :default (defmethod ptk/handle-error :default
[error] [error]
(if (instance? ExceptionInfo error) (if (instance? ExceptionInfo error)
(ptk/handle-error (ex-data error)) (ptk/handle-error (ex-data error))
(do (let [stack (.-stack error)
hint (or (ex-message error)
(:hint error)
(:message error))]
(ts/schedule (ts/schedule
(st/emitf (dm/assign-exception error))) (st/emitf
(dm/assign-exception error)
(ptk/event ::ev/event
{::ev/type "exception"
::ev/name "unexpected-error"
:message hint
:trace (.-stack error)})))
(js/console.group "Internal error:") (js/console.group "Internal error:")
(js/console.log "hint:" (or (ex-message error) (js/console.log "hint:" hint)
(:hint error)
(:message error)))
(ex/ignoring (ex/ignoring
(js/console.error (clj->js error)) (js/console.error (clj->js error))
(js/console.error "stack:" (.-stack error))) (js/console.error "stack:" stack))
(js/console.groupEnd "Internal error:")))) (js/console.groupEnd "Internal error:"))))
(defonce uncaught-error-handler (defonce uncaught-error-handler