Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teach error-api how to XML its output.
- Loading branch information
1 parent
453914d
commit 1bf2eec
Showing
1 changed file
with
16 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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})}))) |