Skip to content

Instantly share code, notes, and snippets.

@solussd
Created April 25, 2012 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solussd/422e9bcefc4102527b96 to your computer and use it in GitHub Desktop.
Save solussd/422e9bcefc4102527b96 to your computer and use it in GitHub Desktop.
passing one or both of the optional key args results in the error: No value supplied for key
passing one or both of the optional key args results in the error: No value supplied for key, if I don't pass the optional map-destructured args all works swell. :)
(defmacro def-rest-resource
"takes an unamed route and params as expected by defpage and a single body form,
optionally a map of the form {:nil-handler nil-handler-fn, :exception-handler exception-handler-fn}
where nil-handler-fn takes no arguments and exception-handler-fn takes an Exception as an argument"
[route params body & {:keys [nil-handler exception-handler]
:or {nil-handler #(response/status 404 {:error "no results"})
exception-handler #(response/status 500 {:error (format "An error has occurred: %s" (.getMessage %))})}}]
`(let [response-format-fn# response/json] ; make this dependent on request accept-type in the future, json for now
(defpage ~route ~params
(try
(let [result# ~body]
(response-format-fn# (if (nil? result#)
(~nil-handler)
result#)))
(catch Exception exception#
((~exception-handler exception#)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment