Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.