Skip to content

Commit

Permalink
Don't return 502 on non 200 gateway response (#6)
Browse files Browse the repository at this point in the history
* Don't return 502 on non 200 gateway response

* Review feedback
  • Loading branch information
mdemare authored Sep 13, 2024
1 parent cc3e729 commit 4a9b1ac
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:paths ["src" "test" "resources" "classes"]
:deps {ch.qos.logback.contrib/logback-json-classic {:mvn/version "0.1.5"}
:deps {ch.qos.logback.contrib/logback-jackson {:mvn/version "0.1.5"}
ch.qos.logback.contrib/logback-json-classic {:mvn/version "0.1.5"}
ch.qos.logback/logback-classic {:mvn/version "1.5.8"}
cheshire/cheshire {:mvn/version "5.13.0"}
com.fasterxml.jackson.core/jackson-databind {:mvn/version "2.17.2"}
Expand Down
14 changes: 8 additions & 6 deletions src/nl/surf/eduhub/validator/service/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
{:headers {"x-route" (str "endpoint=" endpoint-id)
"accept" "application/json; version=5"
"x-envelope-response" "false"}
:basic-auth (:gateway-basic-auth config)})]
:basic-auth (:gateway-basic-auth config)
:throws false})]
(if (= (:status response) 200)
{:valid true}
{:valid false :message (str "Endpoint validation failed with status: " (:status response))}))
(catch Throwable e
{:valid false :message (str "Error during validation: " (.getMessage e))})))
(log/error e "Exception in validate-endpoint")
{:valid false :error true :message (str "Error during validation " (class e) ":" (ex-message e))})))

(defroutes app-routes
(GET "/endpoints/:endpoint-id/config" [endpoint-id]
Expand All @@ -53,11 +55,11 @@
(fn [req]
(let [{:keys [validator endpoint-id] :as resp} (app req)]
(if validator
(let [{:keys [valid] :as body} (validate-endpoint endpoint-id config)]
(let [{:keys [error] :as body} (validate-endpoint endpoint-id config)]
{:body body
:status (if valid
http-status/ok
http-status/bad-gateway)})
:status (if error
http-status/internal-server-error
http-status/ok)})
resp))))

(def opt-specs
Expand Down
2 changes: 1 addition & 1 deletion test/nl/surf/eduhub/validator/service/main_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@

(deftest test-validate-fails
(with-redefs [http/request (fn [_] {:status 401 :body "mocked response"})]
(is (= {:status 502
(is (= {:status 200
:body {:message "Endpoint validation failed with status: 401", :valid false}}
(app {:uri "/endpoints/google.com/config" :request-method :get})))))

0 comments on commit 4a9b1ac

Please sign in to comment.