开发者

Obtaining stack traces in ClojureScript

In my ClojureScript programs running in FireFox 5.0 on Ubuntu 10.04.1 LTS, I get a single cryptic line when an exception is thrown.

'Error: No protocol method ISeqable.-seq defined for type object: [object Object]' when calling method: [nsIDOMEventL开发者_如何学编程istener::handleEvent]

The "-seq" bit seems strange to me and I have searched the generated javascript files for it and not found it.

I hope I am not missing something entirely obvious, but how do I get a stack trace of the exception thrown? How are you debugging your scripts?


Unfortunately stack traces from errors rely on browser support. Most (all?) browsers will allow you to access a canned version of the stack trace (usually the top 10 elements, iirc) as a string by dereferencing the 'stack' field, so you could do something like this:

(try ...throws...
    (catch js/Error e
        (.log js/console (.-stack e))))

However, string stack traces aren't much fun, you can't click them to take you to the source. Better is printing the exception directory to the javascript console (if it's available) to print stack traces with clickable links. E.g.

(try ...throws...
    (catch js/Error e
        (.log js/console e)))

At least in chrome, this only works if the javascript console was open when the error was thrown. This is great for debugging, but less useful when the error was unexpected.

The javascript console objects provided by most browsers have lots of useful functions that you can use from clojurescript. If you want to get helpful line numbers though, you probably want to write a couple of macros to inject the code to print to the console, otherwise all your line numbers will point to your print function.


Looks like you are passing a Javascript object to a Clojurescript function which expects a Clojure sequence. Try (my-function (js->clj my-thing)) edit: or, I'm guessing you're using (.strobj) where you don't need to

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜