Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

The errors thrown by neodoc npm module cause issues for ClojureScript (but not node).

$ java -cp cljs.jar cljs.main --repl-env node
ClojureScript 1.10.758
cljs.user=> (def neodoc (js/require "neodoc"))
#'cljs.user/neodoc
cljs.user=>  (.run neodoc "Usage:\n  foo" (clj->js {:argv []}))                    
#js {}

cljs.user=>  (.run neodoc "Usage:\n  foo" (clj->js {:argv ["bar"] :dontExit true}))
Execution error () at (<cljs repl>:1).
null

cljs.user=> (try (.run neodoc "Usage:\n  foo" (clj->js {:argv ["bar"] :dontExit true})) (catch :default e (prn :e e)))
Execution error (TypeError) at (<cljs repl>:1).
Function.prototype.toString requires that 'this' be a Function

This works in node (albeit the thrown object is strange):

Welcome to Node.js v16.13.2.
Type ".help" for more information.
> neodoc = require("neodoc")
{
  run: [Function: run],
  parse: [Function: parse],
  default: { run: [Function: run], parse: [Function: parse] }
}
> neodoc.run("Usage:\n  foo", {argv: ["bar"], dontExit: true})
Uncaught:
Function {
  message: 'foo: unknown command bar\n' +
    'Usage:\n' +
    '  foo\n' +
    'See foo -h/--help for more information',
  payload: {}
}
> try { neodoc.run("Usage:\n  foo", {argv: ["bar"], dontExit: true}) } catch (e) { console.log("e:", e) }
e: Function {
  message: 'foo: unknown command bar\n' +
    'Usage:\n' +
    '  foo\n' +
    'See foo -h/--help for more information',
  payload: {}
}
undefined

1 Answer

0 votes
by

I think they work in the exact same way. The difference you're seeing is that in CLJS, printing an function requires this while on NodeJS it doesnt. But both versions result in an exception. After all, your NodeJS version also triggered its catch block.

With that being said, you should be fine as long as you don't try to print that Function using the default facilities.

BTW, no need for try/catch. In CLJS REPL you can use *e to get the most recent exception. Similarly, in NodeJS REPL you can use _error.

by
Yes, sure enough. Interesting that there is no native way I can see to print this thing except calling out to js/console.log directly. Perhaps that's the real bug.

True about *e, except I discovered this in nbb where this causes the REPL to hang (but that's a separate issue).
...