Question

I have a rather simple problem I thought. I have a luminus web project with clojurescript and cljs-ajax. What I do is an ajax request to my server and in my error-handler I want to parse a json string to display a specific error message. I just cannot get it working. This is my code:

Clojurescript:

(defn error-handler [response]
  (js/alert (aget response "message")))
;  (js/alert (:message response ))) neither works here

(defn test-connection []
  (POST "/url"
        {:params        (get-form-data)
         :response-format :json
         :handler       succ-handler
         :error-handler error-handler}))

And my serverside code:

(defn connect-jira []
      {:body {:message "No connection to JIRA. Please check your details."}}
      )

(defroutes jira-routes
           (POST "url" []
                 (connect-jira)))

What is the idiomatic way to return a custom error message and parse that with clojurescript?

Update I tracked my problem down some more. There is a difference between the success and the error handler of the POST request. Lets say my server side looks like this:

(defn connect-jira []
      {:status 200 :body {:message "No connection to JIRA. Please check your details."}})

And my success handler like this:

(defn succ-handler [{:keys [message]}]
  (.log js/console message))

Then its all fine, the correct messages is logged to the console. But if my server side looks like this (different statu):

(defn connect-jira []
      {:status 500 :body {:message "No connection to JIRA. Please check your details."}})

I call the error handler on cljs side

(defn err-handler [{:keys [message]}]
  (.log js/console message))

But the message is "null"

I also tried to overwrite the status-text on server side, but did not succeed. So, how do I pass a message to the error handler from server side?

Was it helpful?

Solution

Are you using this library for your AJAX calls?

https://github.com/yogthos/cljs-ajax

If so, your error handler would need to look something more like this:

(defn err-handler [{:keys [response]}]
  (.log js/console (:message response)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top