Re: ClojureScript: catching all javascript exceptions

1,400 views
Skip to first unread message

AtKaaZ

unread,
Oct 31, 2012, 5:19:09 AM10/31/12
to clo...@googlegroups.com
https://github.com/clojure/clojurescript/blob/master/src/cljs/clojure/reflect.cljs#L7
https://github.com/clojure/clojurescript/blob/master/src/cljs/clojure/browser/repl.cljs#L30

Looks implemented and it's same as in clojure ...
What do you think?

On Wed, Oct 31, 2012 at 9:31 AM, xiefei <heliu...@gmail.com> wrote:
followed strategy #1 explained here to write (try ... (catch e ...)) and (try ... (catch _ ...)) , no luck. 
The compiler says "unsupported bind form".  Maybe this construct just not implemented now.

在 2012年9月27日星期四UTC+8下午11时08分22秒,Dima B写道:
Hi,

I came to the point where I need to be able to catch all javascript exceptions, log them down and swallow. I've been trying to achieve this by

(try ... (catch Exception e ...))
(try ... (catch nil e ...))
(try ... (catch js/object e ...))

and nothing seems to do the trick.

Could you please help me find the syntax which allows me to catch and swallow all exceptions in clojurescript?
I'm using all latest (clojurescript via cljsbuild).

Thank you,
Dima

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en



--
I may be wrong or incomplete.
Please express any corrections / additions,
they are encouraged and appreciated.
At least one entity is bound to be transformed if you do ;)

AtKaaZ

unread,
Oct 31, 2012, 5:22:59 AM10/31/12
to clo...@googlegroups.com
seems to be working here: https://himera.herokuapp.com/index.html

cljs.user> (try (+ 1 2) (catch js/Error e e))
3
cljs.user> (try (throw (js/Error. "err1")) (catch js/Error e e))
#<Error: err1>

Alexander Solovyov

unread,
Oct 31, 2012, 6:24:00 AM10/31/12
to clo...@googlegroups.com
On Wed, Oct 31, 2012 at 11:22 AM, AtKaaZ <atk...@gmail.com> wrote:
> seems to be working here: https://himera.herokuapp.com/index.html
>
> cljs.user> (try (+ 1 2) (catch js/Error e e))
> 3
> cljs.user> (try (throw (js/Error. "err1")) (catch js/Error e e))
> #<Error: err1>

This is not working:

(try (throw "err1") (catch js/Error e e))

So it's better to use js/Object there:

(try (throw "err1") (catch js/Object e e))
(try (throw 1) (catch js/Object e e))
(try (throw (js/Error. "err1")) (catch js/Object e e))

All of those cases work.

--
Alexander

Frank Siebenlist

unread,
Oct 31, 2012, 10:59:33 AM10/31/12
to clo...@googlegroups.com, clo...@googlegroups.com
Very useful example - thanks.

This should be explained in the official clojurescript doc pages in the exceptions section.

-FS.

Herwig Hochleitner

unread,
Oct 31, 2012, 2:13:18 PM10/31/12
to clo...@googlegroups.com
try in clojurescript is actually a macro that uses the builtin try* and adds type dispatch.
So to catch everything, just use (try* ... (catch e ...)). This maps directly to javascript's try.

This question seems to come up a lot: Maybe it should be documented where it's visible to people looking for it. Maybe try's docstring?
Where did you first look for it, Dima?

Steve Buikhuizen

unread,
Oct 31, 2012, 8:28:40 PM10/31/12
to clo...@googlegroups.com
Take a look at http://closure-library.googlecode.com/svn-history/r9/trunk/closure/goog/docs/class_goog_debug_ErrorReporter.html

Since all Google Closure is available to clojurescript (in web clients) you can use the static "install" method to log all errors in the client back to the server. 

This has been working well for me.

Frank Siebenlist

unread,
Nov 1, 2012, 2:49:32 AM11/1/12
to clo...@googlegroups.com, Frank Siebenlist
Hi Steve,

That sounds very intriguing, but with my limited javascript&goog knowledge it's difficult to see how you would go about it.

Could you please elaborate on what you did and how you get those js-errors reported back to a web server as some form of logging-service (?).

Thanks, FrankS.

Steve Buikhuizen

unread,
Nov 1, 2012, 6:06:13 AM11/1/12
to clo...@googlegroups.com, Frank Siebenlist
No problem. On the client (cljs) you should:
  1. require [goog.debug.ErrorReporter :as reporter]
  2. (reporter/install "/er")

On the server (I'm using Noir which supplies defpage)

(defpage [:post "/er"] {:keys [error line script trace]}

  (service/record-client-error error line script trace)

  (str "ACK"))

where record-client-error is a function you create that does whatever you want it to do e.g. save to a database, log using log4j or send to an aggregator e.g. Loggly

I know this is still pretty brief. If you want more detail, just say which part is not clear - I'll happily elaborate

Hubert Iwaniuk

unread,
Nov 1, 2012, 7:11:18 AM11/1/12
to clo...@googlegroups.com
In past I used (goog.debug.ErrorReporter/install "logError")
It worked great.

HTH,
Hubert.

Steve Buikhuizen wrote:
--

Frank Siebenlist

unread,
Nov 1, 2012, 12:17:16 PM11/1/12
to Steve Buikhuizen, Frank Siebenlist, clo...@googlegroups.com
Thanks Steve - exactly what I needed - cool stuff.
Reply all
Reply to author
Forward
0 new messages