Catching exceptions

272 views
Skip to first unread message

Aaron Bedra

unread,
Jan 5, 2010, 10:32:49 AM1/5/10
to Compojure
Where would I hook into to have a catch all exception handler? I would like to make sure that all unexpected exceptions are logged properly.

Cheers,

Aaron

James Reeves

unread,
Jan 5, 2010, 5:05:38 PM1/5/10
to Compojure
On Jan 5, 3:32 pm, Aaron Bedra <aaron.be...@gmail.com> wrote:
> Where would I hook into to have a catch all exception handler?  I would like to make sure that all unexpected exceptions are logged properly.

You'd want some middleware like this:

(defn with-error-catching [handler]
(fn [request]
(try
(handler request)
(catch Exception e ;; handle the exception
...))))

And then you'd use the decorate macro to add it onto your routes:

(decorate your-routes with-error-catching)

- James

Aaron Bedra

unread,
Jan 5, 2010, 5:30:49 PM1/5/10
to comp...@googlegroups.com
Thanks!

> --
> You received this message because you are subscribed to the Google Groups "Compojure" group.
> To post to this group, send email to comp...@googlegroups.com.
> To unsubscribe from this group, send email to compojure+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/compojure?hl=en.
>
>

Aaron Bedra

unread,
Jan 7, 2010, 10:40:32 AM1/7/10
to comp...@googlegroups.com
The solution ends up looking something like this:

(defn error-handling


[handler]
(fn [request]
(try
(handler request)
(catch Exception e

(if (= "development" environment)
(handler request)
(do
(log :error e)
[500 "Application Error"]))))))

I have setup environments so that production doesn't produce a stack trace straight to the browser but development will.

Cheers,

Aaron

On Jan 5, 2010, at 5:05 PM, James Reeves wrote:

Wei Hsu

unread,
Feb 27, 2014, 5:34:21 AM2/27/14
to comp...@googlegroups.com
I just implemented similar error-handling recently and am having trouble getting it to work.

Made a reference app to demonstrate the issue: https://github.com/yayitswei/exceptiontest What am I doing wrong here?
Reply all
Reply to author
Forward
0 new messages