aantron/dream

upload raises exception if Content-Type is not multipart/form-data

cemerick opened this issue · 0 comments

This check is done in a couple of places:

dream/src/server/upload.ml

Lines 102 to 115 in 2386083

let content_type = match Message.header request "Content-Type" with
| Some content_type ->
Result.to_option
(Multipart_form.Content_type.of_string (content_type ^ "\r\n"))
| None ->
None
in
match content_type with
| None ->
let message =
"The request does not have 'Content-Type: multipart/form_data; ...'" in
log.error (fun log -> log "%s" message);
failwith message

The problem is that the exception that is raised is an undifferentiated Failure. This is mostly okay when handling requests from live users (as they'll just see whatever error page produced by your installed error handler/template), but is much less useful in an API context, where one would much rather produce a 400 bad request response, rather than and (erroneous) 500 internal server error.

ISTM that the upload functions that raise this failure should either return a result carrying a potential array of informative error types, or a distinct exception type be raised so that callers can issue an appropriate response.