Skip to content

Commit 51fdbaa

Browse files
authored
Merge pull request #19 from SURFnet/ioexception
Handle IOException
2 parents cfad6ff + e6cdf4f commit 51fdbaa

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/nl/jomco/apie/report.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@
477477
(into [:dl])))
478478
[:dl [:dt "Collection is empty!"]]))
479479

480+
(defmethod issue-details "network-error"
481+
[_ _]
482+
nil)
483+
480484
;; this also works for non-json-schema issue types
481485
(defmethod issue-details :default
482486
[openapi {:keys [instance path schema-path] :as issue}]
@@ -527,6 +531,11 @@
527531
(list-summary (:ranges hints))
528532
", got " [:code instance]])
529533

534+
(defmethod issue-summary "network-error"
535+
[_ {:keys [hints instance path]}]
536+
[:span
537+
(str "Network error: " (:message hints))])
538+
530539
(defmethod issue-summary :default
531540
[_ {:keys [issue]}]
532541
[:span

src/nl/jomco/apie/spider.clj

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
[clojure.string :as string]
1111
[babashka.json :as json]
1212
[babashka.http-client :as http-client])
13-
(:import [java.net URI URL]))
13+
(:import [java.net URI URL]
14+
[java.io IOException]))
1415

1516
(defn fixup-request
1617
"Convert spider request to format expected by validator"
@@ -82,9 +83,12 @@
8283
(assoc :uri (select-keys req [:host :port :path :scheme]))
8384
(dissoc :host :port :path :scheme)))
8485

85-
(defn fixup-validator-request
86-
[req]
87-
(assoc req :uri (:path req)))
86+
(defn wrap-io-exception-handler [f]
87+
(fn [request]
88+
(try
89+
(f request)
90+
(catch IOException ex
91+
{:status 0, :body (.getMessage ex), :headers {}}))))
8892

8993
(defn wrap-client
9094
[f client]
@@ -195,6 +199,9 @@
195199
(defn mk-exec-request
196200
[{:keys [base-url headers basic-auth bearer-token]}]
197201
(cond-> http-client/request
202+
:always
203+
(wrap-io-exception-handler)
204+
198205
:always
199206
(wrap-client (http-client/client (assoc http-client/default-client-opts
200207
:follow-redirects :never)))
@@ -226,13 +233,21 @@
226233
'assoc assoc
227234
'dissoc dissoc))
228235

236+
(defn wrap-network-error [validate]
237+
(fn [{:keys [response] :as interaction} path]
238+
(if (= 0 (:status response))
239+
[{:issue "network-error"
240+
:hints {:message (:body response)}}]
241+
(validate interaction path))))
242+
229243
(defn spider-and-validate
230244
[openapi-spec
231245
{:keys [rules seeds] :as _profile}
232246
{:keys [max-requests-per-operation max-total-requests spider-timeout-millis] :as options}]
233247
(let [seeds (or (:seeds options) seeds)
234248
validate (-> (validator/validator-context openapi-spec {})
235-
(validator/interaction-validator))
249+
(validator/interaction-validator)
250+
(wrap-network-error))
236251
matcher (paths-matcher (keys (get openapi-spec "paths")))
237252
op-path (fn [request]
238253
(when-let [template (:template (matcher (path request)))]

0 commit comments

Comments
 (0)