Pretty stack traces
It could be nice to have pretty stack traces for error pages, at least as an option. This would make it a bit easier to see where the real problem lies, and it could maybe colourise them and format them to be a bit more readable.
This technically works, but everything is upside down, alignment is off, and it includes the ascii terminal codes.
(defmethod yada.body/render-error "text/html"
[status ^Throwable error representation {:keys [id options resource] :as ctx}]
(html
[:head
[:title "Error"]
[:style {:type "text/css"} (slurp (io/resource "style.css"))]]
[:body
[:h1 (format "%d: %s" status (yada.body/get-error-message status))]
(when-let [description (yada.body/get-error-description status)]
[:p description])
;; Only
(when yada.body/*output-errors*
[:div
(when (and (:show-stack-traces? resource) yada.body/*output-stack-traces*)
(let [baos (new java.io.ByteArrayOutputStream)
pw (new java.io.PrintWriter (new java.io.OutputStreamWriter baos))]
(io.aviso.exception/write-exception pw error)
#_(.printStackTrace error pw)
(.flush pw)
(let [s (String. (.toByteArray baos))]
[:pre s])))])
[:div
[:p.footer
[:a {:href "https://deps.co"} "Deps"]]]]))
This is not too bad:
(defmethod yada.body/render-error "text/html"
[status ^Throwable error representation {:keys [id options resource] :as ctx}]
(html
[:head
[:title "Error"]
[:style {:type "text/css"} (slurp (io/resource "style.css"))]]
[:body
{:style {:max-width "reset"
:margin "40px"}}
[:h1 (format "%d: %s" status (yada.body/get-error-message status))]
(when-let [description (yada.body/get-error-description status)]
[:p description])
;; Only
(when yada.body/*output-errors*
[:div
(when (and (:show-stack-traces? resource) yada.body/*output-stack-traces*)
(let [baos (new java.io.ByteArrayOutputStream)
pw (new java.io.PrintWriter (new java.io.OutputStreamWriter baos))]
(binding [io.aviso.exception/*traditional* true
io.aviso.exception/*fonts* nil]
(io.aviso.exception/write-exception pw error))
#_(.printStackTrace error pw)
(.flush pw)
(let [s (String. (.toByteArray baos))]
[:pre s])))])
[:div
[:p.footer
[:a {:href "https://deps.co"} "Deps"]]]]))
Just a tip @danielcompton : Have you tried Pyro? Might be of interest. (Personally I'm not using it at the moment.) Not sure how it would plug into HTML, but I see that it's using glow which can output to HTML as well.
Thanks! I was aware of Pyro but hadn’t thought about using it for HTML.