clojureliberator

How to return 401 code from post clause in clojure liberator?


I have such a code:

(defresource errors []
         :authorized? (fn [ctx] (authorized? ctx))
         :allowed-methods [:post :options]
         :available-media-types media-types
         :post!      (fn [ctx] (-> ctx
                                   parse-json
                                   errors/insert-error)))

authorized? function checks user token, but I have some other rules, which I want to check inside post! function. How to do it? I can throw an Exception from the post! function, but I would like to return 401 status code.


Solution

  • It's not clear from your question what you're wanting to check in the :post! function, but in the normal case, a failure in the :post! function wouldn't return a 401 error.

    If you want to return a 401 then you should probably be doing your checking of the request in the :authorized? function, and returning true or false from that.

    If you can elaborate on what rules you want to check then my answer might be able to be more specific.

    You probably have already done this, but make sure you understand the Liberator decision graph and how your request flows through it.