Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Allow for validation failures in the resolve prop #57

Closed
peterszerzo opened this issue Sep 26, 2018 · 5 comments · Fixed by #72
Closed

Allow for validation failures in the resolve prop #57

peterszerzo opened this issue Sep 26, 2018 · 5 comments · Fixed by #72

Comments

@peterszerzo
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently, the resolve prop of the Get component is a simple function that does some transformation on the data coming from the backend. The developer using restful-react is responsible that it is correct (no runtime errors), and add additional logic on their own to handle unexpected responses from the server. In order to offer some legwork to help folks make absolutely sure that the data passing this function either fails gracefully or passes through with sound data, I'd like to suggest two features:

  • catch runtime errors in the fetch method (adding a .catch in https://github.com/contiamo/restful-react/blob/master/src/Get.tsx#L152), sending runtime errors to either the global error management of the library, or the local one if localErrorsOnly is enabled. This would help with third-party parsing/validating solutions like https://github.com/jquense/yup#mixedcastvalue-any-any, which are super powerful, but always throw runtime errors when they fail.
  • allow for the resolve prop to accept a promise which either resolves with the correctly shaped data or rejects with an error. Again, this might help with third-party validation solutions which offer some validation features only asynchronously (recursive data structures and the like).

Describe alternatives you've considered
I cannot think of an alternative that doesn't bypass resolve entirely, leaving validation to be performed inside the render method and killing performance.

Additional context
At a high-level, the purpose of these suggestions is to allow a style of programming that does super-strict validation before data reaches the internals of a react component tree. This provides both safety and debugging convenience: there is always a failure before data even reaches presentational views, narrowing down issues to the validator logic, which can be unit-tested with an ever-broadening list of fixtures from the backend. No more looking around React code to fix backend-frontend miscommunication.

@peterszerzo peterszerzo changed the title Allow for validation failures in the resolve Allow for validation failures in the resolve prop Sep 26, 2018
@fabien0102
Copy link
Contributor

I like the idea, I think the simple way to do this is to try/catch the resolve part in the fetch method to permit to throw inside the user resolve method.

After we can just set the classic error props and retrieve the normal flow.

@peterszerzo
Copy link
Contributor Author

The fetch method is already async, so the error would turn up in the catch block anyway. But you may be right that doing the try/catch inside gives us a better chance to localize/tell apart different kinds of errors a bit better.

@TejasQ
Copy link
Contributor

TejasQ commented Sep 27, 2018

I think this can be quite useful! I would +1 this.

@peterszerzo
Copy link
Contributor Author

Awesome - shall I go ahead with the implementation?

@TejasQ
Copy link
Contributor

TejasQ commented Sep 27, 2018

Ja

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants