@@ -49,6 +49,7 @@ import Data.Foldable (foldMap, foldl')
49
49
import Data.List (find )
50
50
import Data.Maybe
51
51
import Data.Monoid
52
+ import Data.String.Conversions (cs )
52
53
import Data.Text (Text , unpack )
53
54
import Data.Text.Encoding (decodeUtf8 , encodeUtf8 )
54
55
import Data.Typeable
@@ -209,8 +210,11 @@ deriving instance Eq (MultipartResult tag) => Eq (FileData tag)
209
210
deriving instance Show (MultipartResult tag ) => Show (FileData tag )
210
211
211
212
-- | Lookup a file input with the given @name@ attribute.
212
- lookupFile :: Text -> MultipartData tag -> Maybe (FileData tag )
213
- lookupFile iname = find ((== iname) . fdInputName) . files
213
+ lookupFile :: Text -> MultipartData tag -> Either String (FileData tag )
214
+ lookupFile iname =
215
+ maybe (Left $ " File " <> cs iname <> " not found" ) Right
216
+ . find ((== iname) . fdInputName)
217
+ . files
214
218
215
219
-- | Representation for a textual input (any @\<input\>@ type but @file@).
216
220
--
@@ -221,8 +225,11 @@ data Input = Input
221
225
} deriving (Eq , Show )
222
226
223
227
-- | Lookup a textual input with the given @name@ attribute.
224
- lookupInput :: Text -> MultipartData tag -> Maybe Text
225
- lookupInput iname = fmap iValue . find ((== iname) . iName) . inputs
228
+ lookupInput :: Text -> MultipartData tag -> Either String Text
229
+ lookupInput iname =
230
+ maybe (Left $ " Field " <> cs iname <> " not found" ) (Right . iValue)
231
+ . find ((== iname) . iName)
232
+ . inputs
226
233
227
234
-- | 'MultipartData' is the type representing
228
235
-- @multipart/form-data@ form inputs. Sometimes
@@ -246,10 +253,10 @@ class FromMultipart tag a where
246
253
-- in a list of textual inputs and another list for
247
254
-- files, try to extract a value of type @a@. When
248
255
-- extraction fails, servant errors out with status code 400.
249
- fromMultipart :: MultipartData tag -> Maybe a
256
+ fromMultipart :: MultipartData tag -> Either String a
250
257
251
258
instance FromMultipart tag (MultipartData tag ) where
252
- fromMultipart = Just
259
+ fromMultipart = Right
253
260
254
261
-- | Allows you to tell servant how to turn a more structured type
255
262
-- into a 'MultipartData', which is what is actually sent by the
@@ -434,9 +441,9 @@ addMultipartHandling pTag opts subserver =
434
441
bodyCheck () = do
435
442
mpd <- check pTag opts :: DelayedIO (MultipartData tag )
436
443
case fromMultipart mpd of
437
- Nothing -> liftRouteResult $ FailFatal
438
- err400 { errBody = " fromMultipart returned Nothing " }
439
- Just x -> return x
444
+ Left msg -> liftRouteResult $ FailFatal
445
+ err400 { errBody = " Could not decode multipart mime body: " <> cs msg }
446
+ Right x -> return x
440
447
441
448
contentTypeH req = fromMaybe " application/octet-stream" $
442
449
lookup " Content-Type" (requestHeaders req)
0 commit comments