Fork me on GitHub
#babashka
<
2019-09-25
>
sogaiu02:09:31

as a cheap polling version, may be copy-modify / extend wait-for-it?

sogaiu02:09:25

with the following script:

(println "about to wait")
;; XXX: no try in babashka / sci?
(try
  (println (file/wait-for-path "/tmp/that" {:timeout 1000}))  
  (catch Exception e
    (println e)))
(println "finished waiting")
i get:
about to wait
clojure.lang.ExceptionInfo: Could not resolve symbol: try [at line 4, column 2]
{:type :sci/error, :row 4, :col 2}
 at sci.impl.utils$throw_error_with_location.invokeStatic (utils.cljc:48)
    sci.impl.utils$throw_error_with_location.invoke (utils.cljc:45)
    sci.impl.utils$throw_error_with_location.invokeStatic (utils.cljc:46)
    sci.impl.macros$resolve_symbol.invokeStatic (macros.cljc:76)
    sci.impl.macros$macroexpand_call.invokeStatic (macros.cljc:309)
    sci.impl.macros$macroexpand.invokeStatic (macros.cljc:335)
    sci.impl.interpreter$eval_do.invokeStatic (interpreter.cljc:58)
    sci.impl.interpreter$eval_string.invokeStatic (interpreter.cljc:281)
    sci.core$eval_string.invokeStatic (core.cljc:5)
...
is there some way to use try?

sogaiu06:09:43

lol, well, does it make sense for net/wait-for-it to throw then?

borkdude06:09:30

yes, your script will end with exit code other than 0. but I'll see what I can do with try catch finally, maybe on the plane to Helsinki today 😉

sogaiu06:09:57

looking forward to your presentation :)

borkdude06:09:04

are you going there?

sogaiu06:09:36

a bit out of my range - i meant virtually 🙂

borkdude06:09:44

ok 🙂

borkdude06:09:27

maybe it's better for wait-for-it to also not throw actually, but to return a map result

sogaiu06:09:42

that might be nicer at least atm, yes

borkdude11:09:29

@sogaiu I'm working on something in the try branch of sci.

borkdude11:09:05

This works: lein run '(try (/ 1 0) (catch java.lang.ArithmeticException _e 1) (finally (prn "dude")))'

borkdude11:09:26

but GraalVM has some problems with ClassForName, so maybe I have to just make it a static set of classes you can throw

borkdude11:09:48

yep, this works now:

$ ./sci '(try (/ 1 0) (catch java.lang.Exception _e 1) (finally (prn "dude")))'
"dude"
1

borkdude21:09:25

@sogaiu try/catch/finally now works in bb master. feel free to PR any Exceptions you might need here: https://github.com/borkdude/babashka/blob/master/src/babashka/impl/exceptions.clj

borkdude21:09:22

Maybe it makes sense to make a namespace called wait/... with wait-for-port and wait-for-path?

borkdude21:09:41

and then deprecate net/wait-for-it

borkdude21:09:12

also both wait/... functions could have a :default option that is returned when the waiting timed out

sogaiu23:09:40

@borkdude i tried try/catch from bb master -- when i run the new test for it (jvm or native), it seems to work fine:

$ BABASHKA_TEST_ENV=native lein test :only babashka.main-test/try-catch-test
==== Testing native version

lein test babashka.main-test

Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
and
$ lein test :only babashka.main-test/try-catch-test
==== Testing JVM version

lein test babashka.main-test

Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
but when i run this script:
(try
  (/ 1 0)
  (catch ArithmeticException _
    (println "caught an exception")))
i get:
$ ./bb examples/try-catch.clj
caught an exception
java.lang.Exception: Divide by zero [at line 1, column 1]
 at sci.impl.utils$re_throw_with_location_of_node.invokeStatic (utils.cljc:63)
    sci.impl.interpreter$eval_do$fn__8079.invoke (interpreter.cljc:60)
    sci.impl.interpreter$eval_do.invokeStatic (interpreter.cljc:60)
    sci.impl.interpreter$eval_string.invokeStatic (interpreter.cljc:301)
    sci.core$eval_string.invokeStatic (core.cljc:5)
    babashka.main$main$fn__8581.invoke (main.clj:231)
    babashka.main$main.invokeStatic (main.clj:224)
    babashka.main$main.doInvoke (main.clj:170)
    clojure.lang.RestFn.applyTo (RestFn.java:137)
    clojure.core$apply.invokeStatic (core.clj:665)
    babashka.main$_main.invokeStatic (main.clj:262)
    babashka.main$_main.doInvoke (main.clj:262)
    clojure.lang.RestFn.applyTo (RestFn.java:137)
    babashka.main.main (:-1)
am i doing something wrong?