How to handle validation errors from server after post request

Hello all,

I am using post method to register a new user to the server and my command look like this

createPostCommand : User -> Cmd Msg                                                                      
createPostCommand user =                                                                                 
     Http.post                                                                                            
     { url = "http://localhost:4000/users"                                                                
     , body = Http.jsonBody (userEncoder user)                                                            
     , expect = Http.expectJson GotResult userDecoder                                                     
     }      

Me message type for the response is defined like
GotResult (Result Http.Error String)

But my server upon HTTP 400 returns a list with all the validation errors like
[ “user_name” : “can’t be blank”]

How can I set to the post method that upon 201 response I want it to use userDecoder
and decode the User Returned but when 400 comes buck to use some userErrorDecoder
and give me back the validation errors the server responded with ?

and consequently in my update method receive the appropriate response record ?

any help or direction towards an online resource, tutorial or even book would be appreciated.

Cheers

Have a look there to build your own expect function:

https://package.elm-lang.org/packages/elm/http/latest/Http#expectStringResponse

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.