From ca4afe920cd5257fd5f9c2086ebcd057a7c4ed52 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 19 Mar 2016 12:49:14 +0200 Subject: [PATCH] Refactor the main repo impl. --- src/uxbox/repo/core.cljs | 45 +++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/uxbox/repo/core.cljs b/src/uxbox/repo/core.cljs index a18543139..9e3fe15b7 100644 --- a/src/uxbox/repo/core.cljs +++ b/src/uxbox/repo/core.cljs @@ -26,11 +26,25 @@ (assoc response :body (t/decode body)) response)) +(defrecord Ok [status payload]) +(defrecord ServerError [status paylpad]) +(defrecord ClientError [status payload]) +(defrecord NotFound [payload]) + (defn- handle-http-status - [response] - (if (http.status/success? response) - (p/resolved response) - (p/rejected response))) + [{:keys [body status] :as response}] + (cond + (http.status/success? response) + (rx/of (->Ok status body)) + + (http.status/client-error? response) + (rx/throw + (if (= status 404) + (->NotFound body) + (->ClientError status body))) + + (http.status/server-error? response) + (rx/throw (->ServerError status body)))) (def ^:private ^:const +headers+ {"content-type" "application/transit+json"}) @@ -48,23 +62,12 @@ (when auth (auth-headers))) request (merge (assoc request :headers headers) (when body {:body (t/encode body)}))] - (-> (http/send! request) - (p/catch (fn [err] - (println "[error]:" err) - (throw err))) - (p/then conditional-decode) - (p/then handle-http-status)))) - -(defn- req! - [request] - (let [on-success #(p/resolved (:body %)) - on-error #(if (map? %) - (p/rejected (:body %)) - (p/rejected %))] - - (-> (send! request) - (p/then on-success) - (p/catch on-error)))) + (->> (http/send! request) + (rx/from-promise) + (rx/map conditional-decode) + (rx/mapcat handle-http-status) + (rx/tap #(println "tt:" %))))) + ;; "tt: ")))) (defmulti -do (fn [type data] type))