<commit_before>(ns shuppet.middleware
  (:require
   [environ.core :refer [env]]
   [clojure.string :refer [split]]
   [slingshot.slingshot :refer [try+ throw+]]
   [clojure.data.json :refer [write-str]]
   [ring.util.response :as ring-response]))

(defn wrap-shuppet-error
  [handler]
  (fn [req]
    (try+
     (handler req)
     (catch [:type :shuppet.validator/validator] e
       (->  (ring-response/response (write-str {:message "Validation Failure"
                                                :details (e :details)}))
            (ring-response/content-type "application/json")
            (ring-response/status 400)))
     (catch [:type :shuppet.git/git] e
       (->  (ring-response/response (write-str {:message (e :message)}))
            (ring-response/content-type "application/json")
            (ring-response/status (e :status))))
     (catch [:type :shuppet.util/aws] e
       (->  (ring-response/response (write-str e))
            (ring-response/content-type "application/json")
            (ring-response/status 409)))
     (catch [:type :_] e
       (->  (ring-response/response (write-str {:message (e :message)}))
            (ring-response/content-type "application/json")
            (ring-response/status (e :status))))
     (catch  clojure.lang.Compiler$CompilerException e
       (->  (ring-response/response (write-str {:message (.getMessage e)}))
            (ring-response/content-type "application/json")
            (ring-response/status 400)))
      (catch java.io.FileNotFoundException e
       (->  (ring-response/response (write-str {:message "Cannot find this one"}))
            (ring-response/content-type "application/json")
            (ring-response/status 404))))))

(defn- valid-env? [uri envs]
  (not-empty (filter identity (map
                               #(re-matches (re-pattern (str "/1.x/envs/" % ".*")) uri)
                               envs))))

(defn wrap-check-env
  [handler]
  (fn [{:keys [uri] :as req}]
    (if (re-matches #"/1.x/envs/.*" uri)
      (if (valid-env? uri (split (env :service-environments) #","))
        (handler req)
        (-> (ring-response/response (write-str {:message "Environment not allowed"}))
            (ring-response/content-type "application/json")
            (ring-response/status 403)))
      (handler req))))<commit_msg>catch compilation errors from clojail<commit_after>(ns shuppet.middleware
  (:require
   [environ.core :refer [env]]
   [clojure.string :refer [split]]
   [slingshot.slingshot :refer [try+ throw+]]
   [clojure.data.json :refer [write-str]]
   [ring.util.response :as ring-response]))

(defn wrap-shuppet-error
  [handler]
  (fn [req]
    (try+
     (handler req)
     (catch [:type :shuppet.validator/validator] e
       (->  (ring-response/response (write-str {:message "Validation Failure"
                                                :details (e :details)}))
            (ring-response/content-type "application/json")
            (ring-response/status 400)))
     (catch [:type :shuppet.git/git] e
       (->  (ring-response/response (write-str {:message (e :message)}))
            (ring-response/content-type "application/json")
            (ring-response/status (e :status))))
     (catch [:type :shuppet.util/aws] e
       (->  (ring-response/response (write-str e))
            (ring-response/content-type "application/json")
            (ring-response/status 409)))
     (catch [:type :_] e
       (->  (ring-response/response (write-str {:message (e :message)}))
            (ring-response/content-type "application/json")
            (ring-response/status (e :status))))
     (catch  java.util.concurrent.ExecutionException e
       (->  (ring-response/response (write-str {:message (.getMessage e)}))
            (ring-response/content-type "application/json")
            (ring-response/status 400)))
      (catch java.io.FileNotFoundException e
       (->  (ring-response/response (write-str {:message "Cannot find this one"}))
            (ring-response/content-type "application/json")
            (ring-response/status 404))))))

(defn- valid-env? [uri envs]
  (not-empty (filter identity (map
                               #(re-matches (re-pattern (str "/1.x/envs/" % ".*")) uri)
                               envs))))

(defn wrap-check-env
  [handler]
  (fn [{:keys [uri] :as req}]
    (if (re-matches #"/1.x/envs/.*" uri)
      (if (valid-env? uri (split (env :service-environments) #","))
        (handler req)
        (-> (ring-response/response (write-str {:message "Environment not allowed"}))
            (ring-response/content-type "application/json")
            (ring-response/status 403)))
      (handler req))))
{'fraction_non_alphanumeric': 0.16488222698072805, 'fraction_numerical': 0.007280513918629551, 'mean_word_length': 2.2987288135593222, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7028404', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1430, 'n_words': 311}