Skip to content

Commit

Permalink
implant: render dirac info line when reporting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Aug 21, 2016
1 parent a55b916 commit 4fd4714
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ WebInspector.InspectorBackendExtensionMode.loadFromExtensionIfNeeded = function(
if (backendAPI) {
const decodedBackendAPI = decodeURIComponent(backendAPI);
const lines = decodedBackendAPI.split("\n");
WebInspector.BakedInspectorBackendMode = "external";
WebInspector.BakedInspectorBackendModeInfo = lines.length + " registrations";
if (dirac._DEBUG_BACKEND_API) {
console.log("BackendAPI: backend_api url parameter present (" + lines.length + " registrations).");
console.log("BackendAPI: backend_api url parameter present (" + WebInspector.BakedInspectorBackendModeInfo + ").");
}
evalAPI(lines);
WebInspector.BakedInspectorBackendMode = "external";
} else {
const lines = WebInspector.BakedInspectorBackendAPI.split("\n");
WebInspector.BakedInspectorBackendMode = "internal";
WebInspector.BakedInspectorBackendModeInfo = lines.length + " registrations";
if (dirac._DEBUG_BACKEND_API) {
console.log("BackendAPI: backend_api url parameter not present. Using pre-baked backend API (" + lines.length + " registrations).");
console.log("BackendAPI: backend_api url parameter not present. Using pre-baked backend API (" + WebInspector.BakedInspectorBackendModeInfo + ").");
}
evalAPI(lines);
WebInspector.BakedInspectorBackendMode = "internal";
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ WebInspector.SupportedCSSPropertiesExtensionMode.loadFromExtensionIfNeeded = fun
if (encodedBackendCSS) {
const backendCSS = decodeURIComponent(encodedBackendCSS);
const lines = backendCSS.split("\n");
WebInspector.BakedSupportedCSSPropertiesMode = "external";
WebInspector.BakedSupportedCSSPropertiesModeInfo = lines.length + " definitions";
if (dirac._DEBUG_BACKEND_CSS) {
console.log("BackendCSS: backend_css url parameter present (" + lines.length + " definitions).");
console.log("BackendCSS: backend_css url parameter present (" + WebInspector.BakedSupportedCSSPropertiesModeInfo + ").");
}
evalCSS(backendCSS);
WebInspector.BakedSupportedCSSPropertiesMode = "external";
} else {
const backendCSS = WebInspector.BakedSupportedCSSProperties;
const lines = backendCSS.split("\n");
WebInspector.BakedSupportedCSSPropertiesMode = "internal";
WebInspector.BakedSupportedCSSPropertiesModeInfo = lines.length + " definitions";
if (dirac._DEBUG_BACKEND_CSS) {
console.log("BackendCSS: backend_css url parameter not present. Using pre-baked backend CSS (" + lines.length + " definitions).");
console.log("BackendCSS: backend_css url parameter not present. Using pre-baked backend CSS (" + WebInspector.BakedSupportedCSSPropertiesModeInfo + ").");
}
evalCSS(backendCSS);
WebInspector.BakedSupportedCSSPropertiesMode = "internal";
}
};

Expand Down
11 changes: 8 additions & 3 deletions src/automation/dirac/automation/transcript_host.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,20 @@
(defn replace-gensyms [s]
(string/replace s #"--\d+" "--*gen-num*"))

(defn replace-internal-error-traces [s]
(string/replace s #"(Internal Dirac Error:.*?\n.*)(.|[\r\n])*" "$1\n<elided trace>"))
(def internal-error-re1 #"(Internal Dirac Error:.*?)\n(Dirac v.*?)\n(.*?)\n(.|[\r\n])*")
(def internal-error-re2 #"(Internal Dirac Error:.*?)\n(Dirac v.*?)\n(.*?)$")

(defn replace-internal-error [s]
(-> s
(string/replace internal-error-re1 "$1\n<elided info line>\n$3\n<elided body>")
(string/replace internal-error-re2 "$1\n<elided info line>\n$3")))

(defn transformer [console-output]
(-> console-output
replace-shortened-urls
replace-rel-url-params
replace-gensyms
replace-internal-error-traces))
replace-internal-error))

(defn process-default-state! [label text]
(cond
Expand Down
2 changes: 1 addition & 1 deletion src/implant/dirac/implant/eval.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
(info-fn msg)))

(defn display-user-error! [msg & more]
(error msg more)
(warn msg more)
(if-let [error-fn (pref :display-user-error-fn)]
(error-fn msg)))

Expand Down
2 changes: 1 addition & 1 deletion src/implant/dirac/implant/intercom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
(defn configure-eval! [client-config]
(eval/update-config! (assoc client-config
:display-user-info-fn eval/console-info!
:display-user-error-fn eval/console-error!)))
:display-user-error-fn eval/console-warn!)))

(defn init! []
(add-watch nrepl-tunnel-client/current-client ::client-observer on-client-change))
Expand Down
35 changes: 21 additions & 14 deletions src/implant/dirac/implant/reporter.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@
(:require [chromex.support :refer-macros [oget oset ocall oapply]]
[chromex.logging :refer-macros [log warn error group group-end]]
[dirac.implant.feedback :as feedback]
[dirac.implant.info :as info]
[dirac.utils :as utils]))

(defn report! [header body]
{:pre [(string? header)
(string? body)]}
(let [text (str header "\n"
(info/get-info-line) "\n"
body)]
(ocall (oget js/window "dirac") "addConsoleMessageToMainTarget" "error" text)
(feedback/post! text)))

; -- handling global exceptions ---------------------------------------------------------------------------------------------

(defn devtools-exception-handler! [_message _source _lineno _colno e]
(let [body (utils/format-error e)
text (str "Internal Dirac Error: DevTools code has thrown an unhandled exception\n" body)]
(ocall (oget js/window "dirac") "addConsoleMessageToMainTarget" "error" text)
(feedback/post! text)
(let [header "Internal Dirac Error: DevTools code has thrown an unhandled exception"
body (utils/format-error e)]
(report! header body)
false))

(defn register-global-exception-handler! []
Expand All @@ -19,11 +28,10 @@
; -- handling unhandled rejections in promises ------------------------------------------------------------------------------

(defn devtools-unhandled-rejection-handler! [event]
(let [reason (oget event "reason")
body (utils/format-error reason)
text (str "Internal Dirac Error: DevTools code has thrown an unhandled rejection (in promise)\n" body)]
(ocall (oget js/window "dirac") "addConsoleMessageToMainTarget" "error" text)
(feedback/post! text)))
(let [header "Internal Dirac Error: DevTools code has thrown an unhandled rejection (in promise)"
body (utils/format-error (oget event "reason"))]
(report! header body)
false))

(defn register-unhandled-rejection-handler! []
(.addEventListener js/window "unhandledrejection" devtools-unhandled-rejection-handler!))
Expand All @@ -34,11 +42,10 @@

(defn console-error-fn [& args]
(assert *original-console-error-fn*)
(let [result (.apply *original-console-error-fn* js/console (into-array args))]
(let [body (pr-str args)
text (str "Internal Dirac Error: an error was logged into the internal DevTools console\n" body)]
(ocall (oget js/window "dirac") "addConsoleMessageToMainTarget" "error" text)
(feedback/post! text))
(let [result (.apply *original-console-error-fn* js/console (into-array args))
header "Internal Dirac Error: an error was logged into the internal DevTools console"
body (pr-str args)]
(report! header body)
result))

(defn register-console-error-handler! []
Expand Down
3 changes: 3 additions & 0 deletions src/shared/dirac/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
(str stack)
(str e))))

(defn lines [text]
(cuerdas/lines text))

(defn line-count [text]
(count (cuerdas/lines text)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@
(testing "unhandled DevTools exceptions should be presented in target console as Internal Dirac Error"
(<!* a/switch-to-console-panel!)
(<!* a/trigger-internal-error!)
(let [error-content (<!* a/scrape :last-log-item-content "error")
first-line (utils/extract-first-line error-content)
second-line (utils/extract-line error-content 1)]
(let [error-content (utils/lines (<!* a/scrape :last-log-item-content "error"))
first-line (first error-content)
third-line (nth error-content 2 nil)]
(is (= first-line "Internal Dirac Error: DevTools code has thrown an unhandled exception"))
(is (= second-line "Error: :keyword is not ISeqable"))
(is (> (utils/line-count error-content) 4)))) ; assume it contains some stack trace
(is (= third-line "Error: :keyword is not ISeqable"))
(is (pos? (count (drop 3 error-content)))))) ; assume it contains some stack trace
(testing "async unhandled DevTools exceptions in promises should be presented in target console as Internal Dirac Error"
(<!* a/trigger-internal-error-in-promise!)
(let [error-content (<!* a/scrape :last-log-item-content "error")
first-line (utils/extract-first-line error-content)
second-line (utils/extract-line error-content 1)]
(let [error-content (utils/lines (<!* a/scrape :last-log-item-content "error"))
first-line (first error-content)
third-line (nth error-content 2 nil)]
(is (= first-line "Internal Dirac Error: DevTools code has thrown an unhandled rejection (in promise)"))
(is (= second-line "Error: fake async error in promise"))
(is (> (utils/line-count error-content) 4)))) ; assume it contains some stack trace
(is (= third-line "Error: fake async error in promise"))
(is (pos? (count (drop 3 error-content)))))) ; assume it contains some stack trace
(testing "DevTools console.error logs should be presented in target console as Internal Dirac Error"
(<!* a/trigger-internal-error-as-error-log!)
(let [error-content (<!* a/scrape :last-log-item-content "error")
first-line (utils/extract-first-line error-content)
second-line (utils/extract-line error-content 1)]
(let [error-content (utils/lines (<!* a/scrape :last-log-item-content "error"))
first-line (first error-content)
third-line (nth error-content 2 nil)]
(is (= first-line "Internal Dirac Error: an error was logged into the internal DevTools console"))
(is (= second-line "(\"a fake error log\" 1 2 3)"))
(is (= (utils/line-count error-content) 2)))))))
(is (= third-line "(\"a fake error log\" 1 2 3)"))
(is (zero? (count (drop 3 error-content))))))))) ; assume it contains no stack trace
8 changes: 5 additions & 3 deletions test/browser/transcripts/expected/suite01-error-feedback.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ devtools #1 displayWelcomeMessage
devtools #1 setCurrentPanel: console
automate #1 trigger-internal-error!
devtools #1 Internal Dirac Error: DevTools code has thrown an unhandled exception
<elided info line>
Error: :keyword is not ISeqable
<elided trace>
<elided body>
automate #1 scrape [:last-log-item-content "error"]
automate #1 trigger-internal-error-in-promise!
devtools #1 Internal Dirac Error: DevTools code has thrown an unhandled rejection (in promise)
<elided info line>
Error: fake async error in promise
<elided trace>
<elided body>
automate #1 scrape [:last-log-item-content "error"]
automate #1 trigger-internal-error-as-error-log!
devtools #1 Internal Dirac Error: an error was logged into the internal DevTools console
<elided info line>
("a fake error log" 1 2 3)
<elided trace>
automate #1 scrape [:last-log-item-content "error"]
automate #1 close-devtools!
extension handling command: close-dirac-devtools
Expand Down

0 comments on commit 4fd4714

Please sign in to comment.