Skip to content

Instantly share code, notes, and snippets.

@Zariel
Created September 25, 2013 13:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zariel/4875ba5dc34996e22d0e to your computer and use it in GitHub Desktop.
Save Zariel/4875ba5dc34996e22d0e to your computer and use it in GitHub Desktop.
(defn handle-query
[req]
(if-let [partner-name (get-in req [:query-params "partner"])]
(let [mongodb (get-in req [:config :mongodb])]
(if-let [partner (partners/get-by-name mongodb partner-name)]
(if-let [locale (get-in req [:query-params "locale"])]
(if-let [country (get-in req [:query-params "country"])]
(let [after-id (get-object-id (get-in req [:query-params "after"] "000000000000000000000000"))]
(json-response 200 (format-response (query/get-users-query mongodb (:id partner) locale country after-id)))
;(json-error-response 400 "invalid_after_pagination"))
)
(json-error-response 400 "invalid_country"))
(json-error-response 400 "invalid_locale"))
(json-error-response 400 "unrecognised_partner")))
(json-error-response 400 "partner_not_specified")))
@Chouser
Copy link

Chouser commented Sep 25, 2013

Maybe:

(defmacro let-errors [triples & body]
  (if (empty? triples)
    `(do ~@body)
    (let [[binding expr else & more] triples]
      `(if-let [~binding ~expr]
         (let-errors ~(vec more) ~@body)
         ~else))))

(let-errors [a (+ 1 2) "bad a"
             b (+ a 3) "bad b"
             c nil     "bad c"]
  (+ a b c))
;=> "bad c"

(let-errors [a (+ 1 2) "bad a"
             b (+ a 3) "bad b"
             c (+ a b) "bad c"]
  (+ a b c))
;=> 18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment