purescript-trout-client icon indicating copy to clipboard operation
purescript-trout-client copied to clipboard

Need to check for status code

Open martyall opened this issue 4 years ago • 0 comments

In the definition of HasMethodClients method r JSON (Aff (Either JSONClientError r)) (here) you are not checking the status code before deciding whether to parse the body of the request:

instance hasMethodClientMethodJson
  :: (DecodeJson r, IsSymbol method)
  => HasMethodClients method r JSON (Aff (Either JSONClientError r)) where
  getMethodClients method _ req = do
    toAffjaxRequest req
      # _ { method = toMethod method, responseFormat = AXResponseFormat.json }
      # request
      # map (bimap RequestError (_.body >>> decodeJson >>> lmap DecodeError) >>> join)

In the event that the networkl request succeeds but the server returns a non 200 response code, you are attempting to parse the body as valid a valid JSON for the type r, which is probably not what you want to do. IMO the correct implementation would define some kind of client error type in the event of non 200 similar to servant-client

martyall avatar Nov 19 '21 02:11 martyall