Skip to content

Commit

Permalink
Teach error-api how to XML its output.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy authored and tobias committed Apr 25, 2017
1 parent 453914d commit 1bf2eec
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/clojars/web/error_api.clj
@@ -1,13 +1,22 @@
(ns clojars.web.error-api
(:require [ring.util.response :refer [response status content-type]]
[cheshire.core :as json]))
[cheshire.core :as json]
[clojure.xml :as xml]))

(defn error-api-response [error-options error-id]
(let [defaults {:status 500}
options (merge defaults error-options)]
{:status (:status options)
:headers {"Content-Type" "application/json; charset=UTF-8"
"Access-Control-Allow-Origin" "*"}
:body (json/generate-string
{:error (:error-message options)
:error-id error-id})}))
(if (= :xml (:format options))
{:status (:status options)
:headers {"Content-Type" "text/xml; charset=UTF-8"
"Access-Control-Allow-Origin" "*"}
:body (with-out-str
(xml/emit {:tag :error
:attrs {:message (:error-message options)
:id error-id}}))}
{:status (:status options)
:headers {"Content-Type" "application/json; charset=UTF-8"
"Access-Control-Allow-Origin" "*"}
:body (json/generate-string
{:error (:error-message options)
:error-id error-id})})))

0 comments on commit 1bf2eec

Please sign in to comment.