yada icon indicating copy to clipboard operation
yada copied to clipboard

Pretty stack traces

Open danielcompton opened this issue 8 years ago • 4 comments

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.

danielcompton avatar Oct 01 '17 07:10 danielcompton

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"]]]]))

danielcompton avatar Oct 02 '17 13:10 danielcompton

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"]]]]))

danielcompton avatar Oct 02 '17 13:10 danielcompton

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.

ivarref avatar Dec 19 '18 10:12 ivarref

Thanks! I was aware of Pyro but hadn’t thought about using it for HTML.

danielcompton avatar Dec 19 '18 10:12 danielcompton