Skip to content

Commit 03ac5c2

Browse files
committed
Tweaks after review 6
- 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.
1 parent 3f988b4 commit 03ac5c2

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

ring-core/src/ring/middleware/multipart_params.clj

+13-9
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@
137137
{:multipart-params params}
138138
{:params params}))))
139139

140-
(defn default-invalid-filename-handler [request e]
141-
(response/bad-request (.getMessage e)))
140+
(defn default-invalid-filename-handler
141+
([request exception]
142+
(response/bad-request (.getMessage exception)))
143+
([request exception respond raise]
144+
(respond (default-invalid-filename-handler request exception))))
142145

143146
(defn wrap-multipart-params
144147
"Middleware to parse multipart parameters from a request. Adds the following
@@ -173,8 +176,9 @@
173176
174177
:invalid-filename-handler
175178
- A function that gets called when the file being uploaded has an invalid name.
176-
The function should expect two parameters: request and an exception of type
177-
InvalidFileNameException. It should return a ring response."
179+
The sync handler should expect two parameters: request and an exception of
180+
type InvalidFileNameException. The async version should expect four: request,
181+
exception, respond and raise."
178182
([handler]
179183
(wrap-multipart-params handler {}))
180184
([handler options]
@@ -183,14 +187,14 @@
183187
(fn ([request]
184188
(let [req-or-ex (try
185189
(multipart-params-request request options)
186-
(catch Exception ex ex))]
187-
(if (instance? Throwable req-or-ex)
190+
(catch InvalidFileNameException ex ex))]
191+
(if (instance? InvalidFileNameException req-or-ex)
188192
(invalid-filename-handler request req-or-ex)
189193
(handler req-or-ex))))
190194
([request respond raise]
191195
(let [req-or-ex (try
192196
(multipart-params-request request options)
193-
(catch Exception ex ex))]
194-
(if (instance? Throwable req-or-ex)
195-
(respond (invalid-filename-handler request req-or-ex))
197+
(catch InvalidFileNameException ex ex))]
198+
(if (instance? InvalidFileNameException req-or-ex)
199+
(invalid-filename-handler request req-or-ex respond raise)
196200
(handler req-or-ex respond raise))))))))

0 commit comments

Comments
 (0)