day8 / re-frame-http-fx

A re-frame "effects handler" for performing Ajax tasks (via cljs-ajax)
MIT License
259 stars 27 forks source link

What counts as success? #14

Closed deadghost closed 7 years ago

deadghost commented 7 years ago

Some situations where I'd expect success but receive failure:

(reg-event-fx
 :cms/test-200
 (fn [{:keys [db]} [_]]
   {:http-xhrio {:method :post
                 :uri "https://httpbin.org/status/200"
                 :timeout 5000
                 :params {}
                 :format (ajax/json-request-format)
                 :response-format (ajax/json-response-format {:keyword? true})
                 :on-success [:success-handler]
                 :on-failure [:failure-handler]}}))

(reg-event-fx
 :cms/test-201
 (fn [{:keys [db]} [_]]
   {:http-xhrio {:method :post
                 :uri "https://httpbin.org/status/201"
                 :timeout 5000
                 :params {}
                 :format (ajax/json-request-format)
                 :response-format (ajax/json-response-format {:keyword? true})
                 :on-success [:success-handler]
                 :on-failure [:failure-handler]}}))

Seems like just a 200 or 201 response is not enough to be considered a success.

danielcompton commented 7 years ago

We're using the definition of success provided by cljs-ajax. I can't see anything obviously wrong with the code above, what is it actually returning in the HTTP request?

deadghost commented 7 years ago
Status Code: 200 OK

Response Headers
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://mysite.dev
Connection: keep-alive
Content-Length: 0

-------------------------------------------

Status Code: 201 CREATED

Response Headers
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://mysite.dev
Connection: keep-alive
Content-Length: 0

:on-failure handler gets triggered. https://httpbin.org/post works fine; sends a json body instead of empty. I'd guess there are some :body requirements lurking.

danielcompton commented 7 years ago

Sure, what does Chrome Devtools show about the requests? What is the error being returned in the :on-failure?

deadghost commented 7 years ago

{:response nil, :last-method "POST", :original-text "", :last-error "", :failure :parse, :status-text "Invalid JSON string: Format should have been JSON", :status 201, :uri "https://httpbin.org/status/201", :debug-message "No Error", :last-error-code 0}

Looks like it can't take an empty body. I don't know if I quite agree an empty body should count as a failure but I'll consider this issue resolved.

danielcompton commented 7 years ago

I don't know if I quite agree an empty body should count as a failure

Ok sure. You can raise an issue with cljs-ajax about it if you'd like, as they make the decision about success/failure.