Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor exception handling to close #23 #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Web application RPC library for Clojure/Script and Ring.

[](dependency)
```clojure
[hoplon/castra "3.0.0-alpha7"] ;; latest release
[hoplon/castra "3.0.0-alpha8"] ;; latest release
```
[](/dependency)

Expand Down
2 changes: 1 addition & 1 deletion build.boot
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(require
'[adzerk.bootlaces :refer :all])

(def +version+ "3.0.0-alpha7")
(def +version+ "3.0.0-alpha8")

(bootlaces! +version+)

Expand Down
4 changes: 2 additions & 2 deletions src/castra/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
[ex]
(if (ex? ex)
ex
(let [{:keys [status message data stack cause]} ex]
(doto (ex-info message (or data {}) cause)
(let [{:keys [status message stack cause]} ex]
(doto (ex-info message ex cause)
(aset "serverStack" stack)
(aset "status" status)))))

Expand Down
19 changes: 9 additions & 10 deletions src/castra/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[clojure.string :as string]
[ring.util.request :as q :refer [body-string]]
[clojure.set :as s :refer [intersection difference]]
[castra.core :as r :refer [ex ex? dfl-ex *pre* *request* *session* *validate-only*]]
[castra.core :as r :refer [*pre* *request* *session* *validate-only*]]
[clojure.stacktrace :as u :refer [print-cause-trace print-stack-trace]])
(:import
[java.io File]
Expand All @@ -21,16 +21,14 @@
(string/replace string ansi-pattern ""))

(defn- ex->clj [e]
(let [e (if (ex? e) e (dfl-ex e))]
{:message (.getMessage e)
:data (ex-data e)
:stack (strip-ansi
(with-out-str
(try (print-cause-trace e)
(->> (strip-ansi
(with-out-str
(try (print-cause-trace e)
(catch Throwable x
(try (print-stack-trace e)
(catch Throwable x
(try (print-stack-trace e)
(catch Throwable x
(printf "No stack trace: %s" (.getMessage x))))))))}))
(printf "No stack trace: %s" (.getMessage x))))))))
(assoc (ex-data e) :stack)))

(defn- csrf! []
(when-not (get-in *request* [:headers "x-castra-csrf"])
Expand Down Expand Up @@ -125,6 +123,7 @@
f #(do (csrf!) (do-rpc (vars) (expression body-keys req)))
d (try (response body-keys req {:result (f) :state (when state-fn (state-fn))})
(catch Throwable e
(.printStackTrace e)
(response body-keys req {:error (ex->clj e)})))]
{:status 200, :headers h, :body d, :session @*session*}))))))

Expand Down