Skip to content

Commit

Permalink
Tweaks after review 6
Browse files Browse the repository at this point in the history
- Only catch InvalidFileNameException.
- Test for InvalidFileNameException instead of Throwable.
- Add CPS style arities to default-invalid-filename-handler
- Extend docstring with information about overriding the default handler
  in the async case.
  • Loading branch information
expez committed Sep 26, 2018
1 parent 3f988b4 commit 03ac5c2
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions ring-core/src/ring/middleware/multipart_params.clj
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@
{:multipart-params params}
{:params params}))))

(defn default-invalid-filename-handler [request e]
(response/bad-request (.getMessage e)))
(defn default-invalid-filename-handler
([request exception]
(response/bad-request (.getMessage exception)))
([request exception respond raise]
(respond (default-invalid-filename-handler request exception))))

(defn wrap-multipart-params
"Middleware to parse multipart parameters from a request. Adds the following
Expand Down Expand Up @@ -173,8 +176,9 @@
:invalid-filename-handler
- A function that gets called when the file being uploaded has an invalid name.
The function should expect two parameters: request and an exception of type
InvalidFileNameException. It should return a ring response."
The sync handler should expect two parameters: request and an exception of
type InvalidFileNameException. The async version should expect four: request,
exception, respond and raise."
([handler]
(wrap-multipart-params handler {}))
([handler options]
Expand All @@ -183,14 +187,14 @@
(fn ([request]
(let [req-or-ex (try
(multipart-params-request request options)
(catch Exception ex ex))]
(if (instance? Throwable req-or-ex)
(catch InvalidFileNameException ex ex))]
(if (instance? InvalidFileNameException req-or-ex)
(invalid-filename-handler request req-or-ex)
(handler req-or-ex))))
([request respond raise]
(let [req-or-ex (try
(multipart-params-request request options)
(catch Exception ex ex))]
(if (instance? Throwable req-or-ex)
(respond (invalid-filename-handler request req-or-ex))
(catch InvalidFileNameException ex ex))]
(if (instance? InvalidFileNameException req-or-ex)
(invalid-filename-handler request req-or-ex respond raise)
(handler req-or-ex respond raise))))))))

0 comments on commit 03ac5c2

Please sign in to comment.