       (handler request)
       (catch ExceptionInfo e
         (let [response (ex-data e)]
-          {:status  (:code response)
-           :headers {"Content-Type" "application/json"}
-           :body    {:error (:message response)}})))))
+          (json-response (:code response)
+                         (:message response)))))))
 
 (defn wrap-fallback-exception
   [handler]

      (catch Exception e
        {:status 500 :body e}))))

(def app
  (-> handler
      wrap-client-exception
      ring-json/wrap-json-response
      ring-json/wrap-json-params

(ns swarmpit.server
  (:import (clojure.lang ExceptionInfo))
  (:use [org.httpkit.server :only [run-server]])
  (:require [ring.middleware.json :as ring-json]
            [cheshire.core :refer [parse-string]]
            [swarmpit.handler :refer [handler]]
            [swarmpit.utils :refer [in?]]))

(def unsecure ["/login"])

(defn- secure?
  [request]
  (let [uri (:uri request)]
    (not (in? unsecure uri))))

(defn- json-response
  [status response]
  {:status  status
   :headers {"Content-Type" "application/json"}
   :body    {:error response}})

(defn wrap-client-exception
  [handler]
