Check out github.com/ryb73/ppx_decco , it's a JSON codec PPX. It should work something like this:
[@decco] type error = {message: string}; [@decco] type response = { // other response fields, error: option(error), };
Now it can decode a Js.Json.t that you got from Fetch.Response.json:
Js.Json.t
Fetch.Response.json
|> then_(response => switch (response_decode(response)) { | Belt.Result.Ok({error: Some({message})}) => reject(PostError(message)) | response => resolve(response))
This will resolve with a Js.Promise.t(Belt.Result.t(response, error)) meaning the service didn't return an error message but the JSON may or may not have parsed successfully, or reject with your exception containing the service error message.
Js.Promise.t(Belt.Result.t(response, error))
This syntax looks much cleaner ๐คฉ. I will give it a try soon with the Request module and comment about it in this part of the article. Thank you!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Check out github.com/ryb73/ppx_decco , it's a JSON codec PPX. It should work something like this:
Now it can decode a
Js.Json.tthat you got fromFetch.Response.json:This will resolve with a
Js.Promise.t(Belt.Result.t(response, error))meaning the service didn't return an error message but the JSON may or may not have parsed successfully, or reject with your exception containing the service error message.This syntax looks much cleaner ๐คฉ. I will give it a try soon with the Request module and comment about it in this part of the article. Thank you!