Comments (7)
I normally use Promises (gasp!), but opted to support node-style callbacks for easy Isomorphic support in the examples.
Perhaps the question should be:
Who handles the errors & how?
I think you're right with preferring done(value)
for callbacks, especially to integrate solve
.
I very much like the idea of thrown errors (e.g. an ApiService
gets a 500) being available for the UI to answer.
For example, suppose you're in a <Mail.Messages /> component that lazy-loads this.props.messages
. If that fails, you'd want to render something like "Whoops! I couldn't get messages from the server. ?".
The question is, what in the component do we check to render this error?
Here's my proposal for the new API:
- Dynamic props should (somehow) have the equivalent
propTypes
checks.- If the prop is required (e.g.
React.PropTypes.any.isRequired
), then therender()
is neutered (like it is currently) until the props are resolved - If the prop is optional (e.g.
React.PropTypes.any
), then it's expected therender()
knows how to handle missing props by displaying Loading... or something.
- If the prop is required (e.g.
- Dynamic props return a promise or call
done(value)
, wherethis.props.myProp = value;
. - Any thrown errors will be caught by
Resolver.mixin
(or thethis.context.resolver
instance)- Errors could be available via
this.context.resolver.errors
. - The user could provide an
onError
callback for thenew Resolver({ ... })
that gets called when any error occurs during resolution. - Errors could be handled via
resolver.handle(Handler).catch(...)
on the server. - Should errors be emitted for listeners to pick up on? (e.g.
resolver.emit('error', { .. })
)
- Errors could be available via
As I created this project to encourage testability (by testing <Mail.Messages messages={...} />
on the server, and the client lazy-loading messages
into <Mail.Messages />
), the ideal solution would be something where we can test <Mail.Messages />
when it gets an error. Perhaps means mocking the failed service? But that seems like a coupling code-smell compared...
Back to you @jesseskinner!
from react-resolver.
I like the idea of using propTypes
to know when it's safe to render. Couldn't it use the actual propTypes
on the component, rather than something that is the "equivalent of propTypes
"?
This sounds good:
Dynamic props return a promise or call done(value), where this.props.myProp = value;.
For errors not caught within the statics definition, or that should be handled globally, I think this makes a lot of sense:
Errors could be handled via resolver.handle(Handler).catch(...) on the server.
If users want the errors to be available as props, they could use the mechanisms I described above to catch the errors as they happen, and use the done callback to expose them as part of the prop resolution.
So this would give users two ways to handle errors - inline (via callback or promises), and globally (via handler catch), and without adding new API to react-resolver. So the event handling of an onError
callback is probably superfluous, because it can be done by the user themselves with code in the catch
.
Regarding this suggestion:
Errors could be available via this.context.resolver.errors.
I think we should avoid introducing a third pathway for data, there are already props
and state
so having users access stuff on context
is probably unnecessary. Also, I believe context
is undocumented and might likely change in the future.
from react-resolver.
Just had a thought. Callbacks could support both mechanisms via arguments.length, whether there is one argument - done(result) - or two - done(error, result).
from react-resolver.
- Does
throw new Error(...)
get caught/wrapped by Bluebird?
from react-resolver.
I haven't used Bluebird but the standard with Promises is that any errors thrown inside the promise handler or then callbacks will result in a failed promise and the error being passed to any catch callbacks.
from react-resolver.
Note to self:
- Test cases for when errors are thrown
- Recommendation in the docs on how this is handled
- Discussion on if
React.PropTypes.isRequired
should be a pivot for bubbling up errors or trapping them
from react-resolver.
Cleaning up issues. v2 is a complete re-write that's in production, and it's best to deal with any new architecture updates post-launch.
Still want to continue collaborating, and having seen performance issues with ES6 Promises, callbacks will be coming back!
from react-resolver.
Related Issues (20)
- React.renderToStaticMarkup -> ReactDOMServer.renderToStaticMarkup HOT 2
- Can I use this without server rendering? HOT 3
- Add ability to cache resolved props HOT 9
- Using HOC breaks HMR
- Minimum universal react API HOT 1
- test
- Support variable? HOT 3
- Unhandled rejections server side HOT 1
- @context steals data from this.context HOT 6
- Allow factory to decide which props to re-use? HOT 5
- Documentation links are broken HOT 4
- Using same component on different routes HOT 3
- Using @client and @resolve on the same class causes @resolve to be called twice HOT 1
- Some way of manipulating data before passing it to component HOT 2
- Breakage with React 16: PropTypes no longer exported by React HOT 1
- "[email protected]" has incorrect peer dependency "react@^0.14.6 || ^15.0.0-0" HOT 5
- Need hydrate static method in the Resolver class for server side rendering HOT 2
- Fix compatibility with Babel 6
- Resolve multiple props with one callback HOT 5
- Initial Data from Django using React Resolver HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-resolver.