Skip to content

Commit cece99b

Browse files
committed
Improve error rendering, allow empty json
1 parent 747968b commit cece99b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/Network/HTTP/Affjax.purs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Control.Parallel (parOneOf)
2222
import Data.Argonaut.Core (Json)
2323
import Data.Argonaut.Core as J
2424
import Data.Argonaut.Parser (jsonParser)
25+
import Data.Array (intercalate)
2526
import Data.Array as Arr
2627
import Data.Either (Either(..), either)
2728
import Data.Foldable (any)
@@ -39,7 +40,7 @@ import Effect.Aff.Compat as AC
3940
import Effect.Class (liftEffect)
4041
import Effect.Exception (Error, error)
4142
import Effect.Ref as Ref
42-
import Foreign (F, Foreign, ForeignError(..), fail, unsafeReadTagged, unsafeToForeign)
43+
import Foreign (F, Foreign, ForeignError(..), fail, renderForeignError, unsafeReadTagged, unsafeToForeign)
4344
import Math as Math
4445
import Network.HTTP.Affjax.Request as Request
4546
import Network.HTTP.Affjax.Response as Response
@@ -213,8 +214,8 @@ affjax :: forall a. Response.Response a -> AffjaxRequest -> Affjax a
213214
affjax rt req = do
214215
res <- AC.fromEffectFnAff $ runFn2 _ajax responseHeader req'
215216
case runExcept (fromResponse' res.response) of
216-
Left err -> throwError $ error (show err)
217-
Right res' -> pure (res { response = res' })
217+
Left err -> throwError $ error $ intercalate "\n" (map renderForeignError err)
218+
Right res' -> pure (res { response = res' })
218219
where
219220

220221
req' :: AjaxRequest a
@@ -251,7 +252,9 @@ affjax rt req = do
251252
_ -> hs
252253

253254
parseJSON :: String -> F Json
254-
parseJSON = either (fail <<< ForeignError) pure <<< jsonParser
255+
parseJSON = case _ of
256+
"" -> pure J.jsonEmptyObject
257+
str -> either (fail <<< ForeignError) pure (jsonParser str)
255258

256259
fromResponse' :: Foreign -> F a
257260
fromResponse' = case rt of

0 commit comments

Comments
 (0)