java.lang.IllegalArgumentException: Key must be integer because of return value?

470 views
Skip to first unread message

Ivan Schuetz

unread,
Apr 7, 2014, 5:09:18 PM4/7/14
to comp...@googlegroups.com
I just started with Compojure / Clojure. Getting:

java.lang.IllegalArgumentException: Key must be integer

(No line numer from in my code).

Figured out, it's related with the return value of my create-product function, which I use as a handler of a POST request. When the last line is

    (dosync (commute products conj product-test))

I get the exception. When I put e.g. (println "blabla") as last line, it works.

Was looking in the documentation of Compojure and Ring what these functions are expected to return, but could't find anyting. Maybe I'm not searching well enough.

This is complete code:


(defn create-product [params]

  (println "post params: " params)


  (let [product-test (Product. "5" "Coffee"]

    (dosync (commute products conj product-test))

    ;; (println "blabla") if this line is not commented, it works
    )
  )


(defroutes app-routes

  (POST "/product/add" {params :params} (create-product params))
 
  (route/resources "/")
 
  (route/not-found "Not found"))



If someone can link me to documentation which explains this problem, it would be great. Thanks.

James Reeves

unread,
Apr 7, 2014, 5:18:24 PM4/7/14
to Compojure
A Ring handler takes in a request map, and returns a response map. Compojure routes are essentially Ring handlers, with one addition: if they return nil, Compojure considers the route to have failed, and moves onto the next route.

The println function returns nil, so when it is the last function called, your create-product function returns nil, telling Compojure the POST route didn't match. This will likely result the request cascading down to your final not-found route, and your application returns a 404.

The return value from a commute is the value the ref is set to, which judging by the "conj" is not a map. This is why you're getting an error; because you're returning your "products" data structure, when Ring expects a response map.

- James


--
You received this message because you are subscribed to the Google Groups "Compojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to compojure+...@googlegroups.com.
To post to this group, send email to comp...@googlegroups.com.
Visit this group at http://groups.google.com/group/compojure.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages