@@ -293,6 +293,9 @@ instance ToMultipart tag (MultipartData tag) where
293
293
instance ( FromMultipart tag a
294
294
, MultipartBackend tag
295
295
, LookupContext config (MultipartOptions tag )
296
+ #if MIN_VERSION_servant_server(0,18,0)
297
+ , LookupContext config ErrorFormatters
298
+ #endif
296
299
, SBoolI (FoldLenient mods)
297
300
, HasServer sublayout config )
298
301
=> HasServer (MultipartForm' mods tag a :> sublayout) config where
@@ -312,7 +315,7 @@ instance ( FromMultipart tag a
312
315
popts = Proxy :: Proxy (MultipartOptions tag )
313
316
multipartOpts = fromMaybe (defaultMultipartOptions pbak)
314
317
$ lookupContext popts config
315
- subserver' = addMultipartHandling @ tag @ a @ mods pbak multipartOpts subserver
318
+ subserver' = addMultipartHandling @ tag @ a @ mods @ config pbak multipartOpts config subserver
316
319
317
320
-- | Upon seeing @MultipartForm a :> ...@ in an API type,
318
321
-- servant-client will take a parameter of type @(LBS.ByteString, a)@,
@@ -433,29 +436,47 @@ check pTag tag = withRequest $ \request -> do
433
436
where parseOpts = generalOptions tag
434
437
435
438
-- Add multipart extraction support to a Delayed.
436
- addMultipartHandling :: forall tag multipart (mods :: [* ]) env a . (FromMultipart tag multipart , MultipartBackend tag )
439
+ addMultipartHandling :: forall tag multipart (mods :: [* ]) config env a .
440
+ ( FromMultipart tag multipart
441
+ , MultipartBackend tag
442
+ #if MIN_VERSION_servant_server(0,18,0)
443
+ , LookupContext config ErrorFormatters
444
+ #endif
445
+ )
437
446
=> SBoolI (FoldLenient mods)
438
447
=> Proxy tag
439
448
-> MultipartOptions tag
449
+ -> Context config
440
450
-> Delayed env (If (FoldLenient mods) (Either String multipart) multipart -> a)
441
451
-> Delayed env a
442
- addMultipartHandling pTag opts subserver =
452
+ addMultipartHandling pTag opts _config subserver =
443
453
addBodyCheck subserver contentCheck bodyCheck
444
454
where
445
455
contentCheck = withRequest $ \ request ->
446
456
fuzzyMultipartCTCheck (contentTypeH request)
447
457
448
- bodyCheck () = do
458
+ bodyCheck () = withRequest $ \ request -> do
449
459
mpd <- check pTag opts :: DelayedIO (MultipartData tag )
450
460
case (sbool :: SBool (FoldLenient mods ), fromMultipart @ tag @ multipart mpd ) of
451
- (SFalse , Left msg) -> liftRouteResult $ FailFatal
452
- err400 { errBody = " Could not decode multipart mime body: " <> cs msg }
461
+ (SFalse , Left msg) -> liftRouteResult $ FailFatal $ formatError request msg
453
462
(SFalse , Right x) -> return x
454
463
(STrue , res) -> return $ either (Left . cs) Right res
455
464
456
465
contentTypeH req = fromMaybe " application/octet-stream" $
457
466
lookup " Content-Type" (requestHeaders req)
458
467
468
+ defaultFormatError msg = err400 { errBody = " Could not decode multipart mime body: " <> cs msg }
469
+ #if MIN_VERSION_servant_server(0,18,0)
470
+ pFormatters = Proxy :: Proxy ErrorFormatters
471
+ rep = typeRep (Proxy :: Proxy MultipartForm' )
472
+ formatError request =
473
+ case lookupContext pFormatters _config of
474
+ Nothing -> defaultFormatError
475
+ Just fmts -> bodyParserErrorFormatter fmts rep request
476
+ #else
477
+ formatError _ = defaultFormatError
478
+ #endif
479
+
459
480
-- Check that the content type is one of:
460
481
-- - application/x-www-form-urlencoded
461
482
-- - multipart/form-data; boundary=something
0 commit comments