Fork me on GitHub
#beginners
<
2022-07-28
>
Lycheese08:07:26

I am trying to catch a socket timeout on app startup with:

(require '[conman.core :as conman])

(try
    (conman/connect! {:jdbc-url
                      ""})
    (catch java.net.SocketTimeoutException _
      "Connection Timed Out"))
deps.edn
{:deps {conman/conman {:mvn/version "0.9.1"}}
It works when just using Exception but I would like to be more specific in what I catch. I tried (:import [ SocketTimeoutExeption]) but no dice. Stacktrace in thread.

Lycheese10:07:11

Wait, is it because the top-level exception is com.zaxxer.hikari.pool.HikariPool$PoolInitializationException and catch never gets to see the java.net.SocketTimeoutException?

Lycheese10:07:44

Using the hikari exception in catch works. Not really as specific as I'd like though. Is there a way to go down the exceptions stack in catch?

kennytilton11:07:27

Dunno about the implementation of the Hikari code throwing the exception, but looking at the inheritance....

java.lang.Object
  java.lang.Throwable
    java.lang.Exception
       java.lang.RuntimeException
          com.zaxxer.hikari.pool.PoolInitializationException
...it would seem you cannot get more specific. Perhaps check the Hikari doc for verbose options available during development?

Ben Lieberman14:07:59

I am trying to understand this code example from the docs for reducers/fold :

(defn count-occurrences [words]
  (r/fold
    (r/monoid #(merge-with + %1 %2) (constantly {}))
    (fn [m [k cnt]] (assoc m k (+ cnt (get m k 0))))
    (r/map #(vector % 1) words)))
I think I've got it mostly, except for the r/monoid expression. Do %1 and %2 correspond to the two functions below, or something else? Secondly, is constantly providing a new map on each iteration that is then being folded into the previous state?

delaguardo14:07:43

%1, %2 etc are a syntax for anonymous function. They stand for argument 1, argument 2 etc. There more information in the guides - https://clojure.org/guides/learn/functions#_anonymous_function_syntax

delaguardo14:07:44

so #(merge-with + %1 %2) is a function of two arguments in position of operation for r/monoid

Ben Lieberman14:07:36

I know about the positional meaning here, but I'm unsure of which positions. In my limited experience, those positions are only meaningful inside the expression they are used with. It seems like %1 would correspond to the output of constantly {} which is a new map. But what about %2 ? Or am I mistaken?

dpsutton14:07:44

In mathematics, a monoid is a set of values which has an operation of how to combine two values and get a new value in the set, and an identity element. The identity element e satisfies x * e = e * x = x for all values in the set. * here is that “operation of how to combine two values and get a new value”. So with that understanding, you are defining a monoid and giving the two properties that make a monoid: • how do you combine two values: you take two maps whos values are numbers and combine the values of the same keys with +. ie (combine {:a 1 :c 3} {:a 5}) -> {:a 6 :c 3} which is a map whose keys are numbers and still in the set of values • the identity element: {} (and r/monoid seems to want a function to return the identity element rather than just the element. And to verify that {} is indeed an identity element, (combine {:a 1} {}) will merge the keys, there are no keys in the empty map so it will be just the value of the first map. Similarly if the identity element is first (combine {} {:a 1}) will yield back the second argument.

🙏 1
Ben Lieberman14:07:36

thanks! that's really helpful

👍 1
Benjamin14:07:22

Is there an IFn protocol? The reason I ask is because I want to try to extend "ifn" to clr reflection methods (I am running on clr right now)

Benjamin14:07:45

yea. And "extend" only is for protocols right

Benjamin15:07:07

(import [clojure.lang IFn])

  ((let [m method]
     (reify IFn
       (invoke [_ arg0]
         (.Invoke method nil (into-array System.Object [arg0])))))
   10)
this works (method is Console.WriteLine) I am not sure if it will end up being useful but it is cute

Ed15:07:29

deftype will let you implement and interface, so you could use that to create something that's callable

dpsutton16:07:13

Sean’s user-manager example repo has an https://github.com/seancorfield/usermanager-example/blob/develop/src/usermanager/model/user_manager.clj#L89 where a defrecord is callable to return the db connection. Example use case is here in the https://github.com/seancorfield/usermanager-example/blob/develop/src/usermanager/model/user_manager.clj#L133 function where it calls (db) which is invoking an instance of that record

sarna19:07:14

hey, how do I make clojure print longer stacktraces when I run stuff via lein run? for now all the info I get is

Execution error (FileAlreadyExistsException) at sun.nio.fs.UnixCopyFile/move (UnixCopyFile.java:449).

Full report at:
/var/folders/xs/qz1q94cn4snc4xmt05l3rhmr0000gn/T/clojure-10476363638687002965.edn
there's not even a line number (except the one deep in java code)

phronmophobic19:07:48

I have the following in my shell profile for convenience:

function clj-error(){
    ls -tr "$TMPDIR"/clojure-* | tail -1 | xargs less
}
which I can use by just executing $ clj-error in my terminal

sarna20:07:31

ah cool, ty

hiredman19:07:30

The full report file contains the complete stacktrace

hiredman19:07:00

There is a system property (java property) that controls that behavior

sarna20:07:56

aha, so that was the change, in 1.10.1. that explains it, thanks!

Ben21:07:31

I'm having a bit of trouble getting started in windows. Specifically, WSL 2. I have WSL 2 working well with C/C++ to build Marlin firmware via VSCode & PlatformI/O. Which is to say, I'm relatively confident that it's working correctly. Installed clojure & lein (`sudo apt install clojure leiningen`) and that worked OK for lein new app my-app and lein repl with a couple packages from clojars. When I added my profiles.clj in ~/.lein, I get continual errors:

Retrieving cider/cider-nrepl/0.28.3/cider-nrepl-0.28.3.jar from clojars
Error loading cider.nrepl: Syntax error compiling at (cider/nrepl.clj:1:1).
My profiles.clj looks like this:
> cat ~/.lein/profiles.clj
{:repl {:plugins [[cider/cider-nrepl "0.28.3"]
                  [mx.cider/enrich-classpath "1.9.0"]]}}
I had tried with version 0.28.5 first, since that looked latest, but rolled back to .3, because that's working on my Mac. Any thoughts? What step have I missed?

hiredman21:07:16

the lein version from apt is likely ancient

hiredman22:07:12

there should be more from the above error message as well, which would be helpful in determining what the issue is

Ben14:07:16

My version:

lein -v
Leiningen 2.9.1 on Java 11.0.15 OpenJDK 64-Bit Server VM
And the full error:
Error loading cider.nrepl: Syntax error compiling at (cider/nrepl.clj:1:1).
Error loading cider.nrepl: Syntax error compiling at (cider/nrepl.clj:1:1).
Error loading cider.nrepl: Syntax error compiling at (cider/nrepl.clj:1:1).
Error loading cider.nrepl: Syntax error compiling at (cider/nrepl.clj:1:1).
Error loading cider.nrepl: Syntax error compiling at (cider/nrepl.clj:1:1).
Error loading cider.nrepl: Syntax error compiling at (cider/nrepl.clj:1:1).
Error loading cider.nrepl: Syntax error compiling at (cider/nrepl.clj:1:1).
Exception in thread "main" Syntax error compiling var at (/tmp/form-init13430373027611414889.clj:1:9257).
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:7114)
        at clojure.lang.Compiler.analyze(Compiler.java:6789)
        at clojure.lang.Compiler.analyze(Compiler.java:6745)
        at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3888)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:7108)
        at clojure.lang.Compiler.analyze(Compiler.java:6789)
        at clojure.lang.Compiler.analyze(Compiler.java:6745)
        at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3888)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:7108)
        at clojure.lang.Compiler.analyze(Compiler.java:6789)
        at clojure.lang.Compiler.access$300(Compiler.java:38)
        at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:6384)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:7106)
        at clojure.lang.Compiler.analyze(Compiler.java:6789)
        at clojure.lang.Compiler.analyze(Compiler.java:6745)
        at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:6120)
        at clojure.lang.Compiler$FnMethod.parse(Compiler.java:5467)
        at clojure.lang.Compiler$FnExpr.parse(Compiler.java:4029)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:7104)
        at clojure.lang.Compiler.analyze(Compiler.java:6789)
        at clojure.lang.Compiler.eval(Compiler.java:7173)
        at clojure.lang.Compiler.eval(Compiler.java:7166)
        at clojure.lang.Compiler.eval(Compiler.java:7166)
        at clojure.lang.Compiler.eval(Compiler.java:7166)
        at clojure.lang.Compiler.load(Compiler.java:7635)
        at clojure.lang.Compiler.loadFile(Compiler.java:7573)
        at clojure.main$load_script.invokeStatic(main.clj:452)
        at clojure.main$init_opt.invokeStatic(main.clj:454)
        at clojure.main$init_opt.invoke(main.clj:454)
        at clojure.main$initialize.invokeStatic(main.clj:485)
        at clojure.main$null_opt.invokeStatic(main.clj:519)
        at clojure.main$null_opt.invoke(main.clj:516)
        at clojure.main$main.invokeStatic(main.clj:598)
        at clojure.main$main.doInvoke(main.clj:561)
        at clojure.lang.RestFn.applyTo(RestFn.java:137)
        at clojure.lang.Var.applyTo(Var.java:705)
        at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: Unable to resolve var: cider.nrepl/wrap-apropos in this context
        at clojure.lang.Util.runtimeException(Util.java:221)
        at clojure.lang.Compiler$TheVarExpr$Parser.parse(Compiler.java:720)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:7106)
        ... 36 more
REPL server launch timed out.

Ben14:07:36

Well, I uninstalled all the apt tools and reinstalled with homebrew (:man-shrugging:) and everything works again. Thanks for looking!

seancorfield22:07:39

Please remember to use threads folks!

thanks3 1
Bernard Staniforth14:07:34

Yes see bubble icon with two lines in it!