Skip to content

Commit

Permalink
sci error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Apr 16, 2024
1 parent 39beb81 commit 56493af
Show file tree
Hide file tree
Showing 24 changed files with 160 additions and 106 deletions.
15 changes: 5 additions & 10 deletions demo/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
"resources"
"target/webly"]
:deps {org.pinkgorilla/goldly {:local/root ".." :deps/manifest :deps}
org.pinkgorilla/webly {:mvn/version "0.5.662"}
org.pinkgorilla/ui-repl {:mvn/version "0.1.100"}
org.pinkgorilla/webly {:mvn/version "0.5.666"}
org.pinkgorilla/ui-repl {:mvn/version "0.1.102"}
org.pinkgorilla/ui-site {:mvn/version "0.0.30"}
org.pinkgorilla/ui-binaryclock {:mvn/version "0.2.19"}
;org.pinkgorilla/ui-input {:mvn/version "0.2.44"} ; spaces
org.pinkgorilla/ui-tailwind {:mvn/version "0.0.5"}
org.pinkgorilla/ui-dialog-keybindings {:mvn/version "0.1.7"}
;org.pinkgorilla/devtools {:mvn/version "0.0.17"}
org.pinkgorilla/pinkie {:mvn/version "0.5.232"} ; higher version
org.pinkgorilla/ui-bidi {:mvn/version "0.0.30"} ; higher version
}

:aliases
Expand All @@ -19,14 +20,8 @@
:main-opts ["-m" "cljfmt.main"
"--indents" "cljfmt.edn"]}

:nrepl {:extra-deps {nrepl/nrepl {:mvn/version "1.0.0"}
cider/cider-nrepl {:mvn/version "0.28.3"}}
:main-opts ["-m" "nrepl.cmdline"]}

:tree {:extra-deps {org.clojure/tools.deps.alpha {:mvn/version "0.14.1178"}}
:exec-fn clojure.tools.cli.api/tree}

; goldy-test
; goldy-demo
; this is a complete deps.edn project used to test goldly.

; tests:
Expand Down
8 changes: 8 additions & 0 deletions demo/resources/demo/notebook/err_clj.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns demo.notebook.err-clj
(:require [bongistan]))

(blubb 1234)

; {:err {:class "java.io.FileNotFoundException",
; :message "Could not locate bongistan__init.class, bongistan.clj or bongistan.cljc on classpath.",
; :stacktrace [] }
7 changes: 7 additions & 0 deletions demo/resources/demo/notebook/err_cljs.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns demo.notebook.err-cljs
(:require [bongistan]))
; bongistan ns does not exist


(blubb 13 27)
;; blubb does not exist.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 6 additions & 1 deletion demo/resources/ext/pages.edn
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
"scicompile" demo.page.sci-compile/sci-compile-page
"lazy" demo.page.lazy/lazy-page
"kernel" demo.page.kernel/kernel-page
"error" demo.page.error/error-page}}
"error" demo.page.error/error-page}

:sci-cljs-ns [demo.cljs-libs.helper
]

}
19 changes: 11 additions & 8 deletions demo/resources/test-config.edn
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{:spa {:title "Goldly Demo"
{:build {:disabled-extensions #{"goog.string"
"clojure.walk"
"clojure.string"
"goog.object"}}

:spa {:title "Goldly Demo"
:start-user-app [:goldly/init]}

:reval {:rdocument {:storage-root "target/webly/public/rdocument/"
:url-root "/api/rdocument/file/"}
:collections {:demo [:clj "demo/notebook/"] ; embedded notebooks in jars.
:demo-cljs [:cljs "demo/notebook/"] ; embedded notebooks in jars.
}}
:keybindings [{:kb "alt-g k",

:keybindings [{:kb "alt-g k",
:handler [:palette/show],
:desc "Keybindings dialog"}
{:kb "esc",
Expand All @@ -16,21 +21,19 @@
{:kb "alt-g t",
:handler [:reframe10x-toggle],
:desc "10x visibility toggle"}

;{:kb "up" :handler [:arrow-up] :desc "Codemirror Key Up"} ; :scope :codemirror
;{:kb "down" :handler [:arrow-down] :desc "Codemirror Key Down"} :scope :codemirror
{:kb "shift-enter" :handler [:codemirror-active/completion-apply] :desc "applies auto completion"}
{:kb "ctrl-space" :handler [:codemirror/completion-get] :desc "Show possible auto-completions"}
; {:spec "enter" :handler [:completion/clear] :key ::clear}
; {:spec "ctrl" :handler [:completion/show-all false] :scope :global :key ::show-all-hide}
{:kb "alt-g c" :handler [:clojuredocs] :desc "Look up the symbol under the cursor in ClojureDocs"}

; eval
{:kb "alt-shift-enter" :handler [:notebook/evaluate-all] :desc "Evaluate all segments."}
{:kb "ctrl-shift-enter" :handler [:notebook/evaluate-all] :desc "Evaluate all segments"}
{:kb "ctrl-enter" :handler [:repl/eval-expression] :desc "Evaluate the highlighted segment"}]



;
}
4 changes: 2 additions & 2 deletions demo/src/demo/cljs_libs/helper.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
(defn wrap-layout [page]
(fn [route]
[layout/header-main
[test-header]
[page route]]))
[test-header]
[page route]]))
8 changes: 4 additions & 4 deletions demo/src/demo/cljs_libs/init.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns demo.cljs-libs.init
(:require
[taoensso.timbre :as timbre :refer [info warn]]
[re-frame.core :as rf]
[webly.module.build :refer [load-namespace]]))
(:require
[taoensso.timbre :as timbre :refer [info warn]]
[re-frame.core :as rf]
[webly.module.build :refer [load-namespace]]))

(rf/reg-event-db
:goldly/init
Expand Down
4 changes: 2 additions & 2 deletions goldly-sci/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
funcool/promesa {:mvn/version "11.0.674"}
cljs-http/cljs-http {:mvn/version "0.1.46"} ; cljs-sci source loader
org.babashka/sci {:mvn/version "0.8.40"}
org.pinkgorilla/ui-dialog-keybindings {:mvn/version "0.1.6"}
org.pinkgorilla/timbre {:mvn/version "0.0.3"}}
org.pinkgorilla/ui-dialog-keybindings {:mvn/version "0.1.8"}
org.pinkgorilla/timbre {:mvn/version "0.0.4"}}

:aliases
{;; developer tooling
Expand Down
5 changes: 1 addition & 4 deletions goldly-sci/resources/ext/goldly.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
; build
:lazy true
:cljs-namespace [goldly.sci
goldly.sci.error
goldly.sci.eventhandler ; move to ui-repl or ui-gorilla
;goldly.page.no-page
;goldly.static.app ; not possible, creates cyclic dependency.
]
goldly.sci.error]
:cljs-ns-bindings {'goldly.sci {'require-async goldly.sci/require-async
'resolve-symbol goldly.sci/resolve-symbol
'requiring-resolve goldly.sci/requiring-resolve
Expand Down
59 changes: 59 additions & 0 deletions goldly-sci/src/goldly/build.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(ns goldly.build
(:require
[clojure.string]
[clojure.java.io :as io]
[babashka.fs :refer [create-dirs]]
[extension :refer [get-extensions-for write-service]]))

(defn- ensure-directory [path]
(when-not (.exists (io/file path))
(.mkdir (java.io.File. path))))

(defn ns->filename [ns]
(-> ns
(clojure.string/replace #"\." "/")
(clojure.string/replace #"\-" "_")))

(defn slurp-res [name-full]
(let [file-content (try
(slurp name-full)
(catch Exception _
nil))]
(if file-content
file-content
(try (let [r (io/resource name-full)]
(slurp r))
(catch Exception _
nil)))))

(defn export-ns [dir ns]
(let [filename (str (ns->filename ns) ".cljs")
content (slurp-res filename)
filename-out (str dir "/" filename)
idx (clojure.string/last-index-of filename-out "/")
dir-out (subs filename-out 0 idx)]
(println "exporting sci file " filename " to: " filename-out "dir-out: " dir-out)
(if (or (nil? content) (clojure.string/blank? content))
(println "no resource for: " filename)
(do (create-dirs dir-out)
(spit filename-out content)))))

(defn write-files [dir sci-files]
(doall (map #(export-ns dir %) sci-files)))

(defn export-sci-code [exts]
(let [sci-files (->> (get-extensions-for exts :sci-cljs-ns concat [] [])
(into []))
dir "target/webly/public/code"]
(write-service exts :sci-cljs-ns sci-files)
(ensure-directory dir)
(write-files dir sci-files)))

(comment
(def exts (extension/discover))
(export-sci-code exts)

;
)


30 changes: 17 additions & 13 deletions goldly-sci/src/goldly/sci.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[goldly.sci.clojure-core :refer [cljns] :as clojure-core]
; loading of cljs source-code
[goldly.sci.loader.async-load :refer [async-load-fn]]
))
[goldly.sci.error :refer [exception->error]]))

(declare ctx-repl) ; since we want to add compile-sci to the bindings, we have to declare the ctx later

Expand Down Expand Up @@ -97,19 +97,23 @@
; )
sci/ns @!last-ns]
(let [eval-p (scia/eval-string+ ctx-repl code)]
(.then eval-p (fn [res]
(let [{:keys [val ns]} res
result {:id nil
:code code
:value val
:out @output
:ns (str ns)}]
(reset! !last-ns ns)
(reset! output "")
(debug "sci-cljs compile result: " result)
result)))))
(-> eval-p
(p/then (fn [res]
(let [{:keys [val ns]} res
result {:id nil
:code code
:value val
:out @output
:ns (str ns)}]
(reset! !last-ns ns)
(reset! output "")
(debug "sci-cljs compile result: " result)
result)))
(p/catch (fn [e]
(timbre/error "sci compile-code-async error:" e)
(exception->error e))))))
(catch :default e
(timbre/error "sci compile-code-async --]" code "[-- ex: " e)
(timbre/error "sci compile-code-async2 --]" code "[-- ex: " e)
{:error {:root-ex (.-data e)
:err (.-message e)}})))

Expand Down
24 changes: 17 additions & 7 deletions goldly-sci/src/goldly/sci/error.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@
(timbre/error "compilation failed: " filename error)
(show-notification :error (error-view filename error) 0))

(defn load-error? [e]
(:load-error e))

(defn load-error->error [e]
{:err {:class "load-error"
:message (:load-error e)}})

(defn exception->error [e]
; #error {:message "Could not resolve symbol: call-bad-fn",
; :data {:type :sci/error, :line nil, :column nil, :file nil, :phase "analysis"}}
; :data {:type :sci/error, :line nil, :column nil, :file nil, :phase "analysis"}}
; not working:
; error-message (:error/message err)
; error-data (:error/data err)
(let [data (ex-data e)]
(when-let [message (or (:message data) (.-message e))]
(let [data (or (:data data) (.-data e))]
(error "sci error-message:" message " error-data:" data)
{:err message
:root-ex data}))))
(if (load-error? e)
(load-error->error e)
(let [data (ex-data e)]
(when-let [message (or (:message data) (.-message e))]
(let [data (or (:data data) (.-data e))]
(error "sci error-message:" message " error-data:" data)
{:err {:message message
:class ""
:root-ex data}})))))
8 changes: 4 additions & 4 deletions goldly-sci/src/goldly/sci/loader/async_load.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
[d]
(let [r-p (p/deferred)
shadow-p (add-shadow-module d)]
(-> shadow-p
(-> shadow-p
(p/then (fn [r]
(p/resolve! r-p r)))
(p/catch (fn [err]
(let [source-p (add-sci-cljs-source d)]
(-> source-p
(p/then (fn [res] (p/resolve! r-p res)))
(p/catch (fn [err] (p/reject! r-p err))))))))
(p/then (fn [res] (p/resolve! r-p res)))
(p/catch (fn [err] (p/reject! r-p err))))))))

;(= libname "some_js_lib")
;(load-module-test ctx libname ns opts)
r-p))
8 changes: 4 additions & 4 deletions goldly-sci/src/goldly/sci/loader/cljs_source_add.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(p/then (fn [code]
(scia/eval-string+ ctx code)))
(p/catch (fn [err]
(p/reject! r {:error (str "no code received err: " err)}))))]
(p/reject! r {:load-error (str "no sci-code for ns: " libname " err: " err)}))))]
(-> eval-p
(p/then (fn [res]
(if (valid-code? res)
Expand All @@ -31,10 +31,10 @@
(warn "registering as: " as "in ns: " ns " to:" (symbol libname))
(sci/add-import! ctx ns (symbol libname) (:as opts)))
(p/resolve! r {:handled false}))
(do (error "no sci-code received for " libname)
(p/reject! r {:error "no sci-code received!"})))))
(do (error "no sci-code received for ns: " libname)
(p/reject! r {:load-error (str "no sci-code for ns: " libname)})))))
(p/catch (fn [e]
(error "compile error for: " libname " error: " e)
(p/reject! r (str "compile error for: " libname)))))
r))

3 changes: 1 addition & 2 deletions goldly-sci/src/goldly/sci/loader/cljs_source_load.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
(info "loading sci-cljs source file from url: " url)
url))


(defn load-sci-cljs-code [libname]
; libname: bongo.trott ; the ns that gets compiled
(info "load-sci-cljs-code" "libname:" libname )
(info "load-sci-cljs-code" "libname:" libname)
(let [url (-> libname str ns->url)]
(GET url)))
17 changes: 7 additions & 10 deletions goldly-sci/src/goldly/sci/loader/shadow_add.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@
[sci.core :as sci]
[goldly.sci.loader.shadow-load :refer [load-shadow-ns]]))


(defn add-shadow-module [{:keys [ctx libname ns opts sci-mod]}]
(info "load-shadow-module: ns: " libname)
(let [r-p (p/deferred)
shadow-p (load-shadow-ns libname)
shadow-p (if (p/promise? shadow-p)
shadow-p
shadow-p
(p/resolved shadow-p))]
(-> shadow-p
(p/then (fn [res]
(info "received shadow-module for libname: " libname "ns: " ns)
(sci/add-namespace! ctx libname res)
(info "received shadow-module for libname: " libname "ns: " ns)
(sci/add-namespace! ctx libname res)
;; empty map return value, SCI will still process `:as` and `:refer`
(p/resolve! r-p {})))
(p/resolve! r-p {})))
(p/catch (fn [err]
(error "no shadow-module received for ns: " libname)
(p/reject! r-p err)
))
)
(error "no shadow-module received for ns: " libname)
(p/reject! r-p err))))

r-p))

2 changes: 1 addition & 1 deletion goldly-sci/src/goldly/sci/loader/shadow_load.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
[webly.module.build :refer [load-namespace]]))

(defn load-shadow-ns [libname]
(info "webly-shadow-ns load: " libname " type: " (type libname))
(info "webly-shadow-ns load: " libname " type: " (type libname))
(load-namespace libname))
Loading

0 comments on commit 56493af

Please sign in to comment.