Comments (10)
I'm busy for the rest of the week, but will look into this on Saturday.
from redux-promise-middleware.
We follow FSA which describes the action to contain the error instance in it's payload, not sure how the two can align.
from redux-promise-middleware.
Could it be implemented as a configuration option? I would be open to that, but since I do not use Bluebird I wouldn't want to implement it. Would you be open to making a PR @mlucool?
from redux-promise-middleware.
Sure I can take care of it
from redux-promise-middleware.
@tomatau What I suggested is inline with FSA and bluebird - both want error objects. We should have a data carrying Error in the library that stores data in a known place. Let's call this ErrorWithPayload instead of PromiseError?
class ExtendableError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
this.message = message;
Error.captureStackTrace(this, this.constructor.name)
}
}
// Use this with a promises when you need extra data:
// https://github.com/petkaantonov/bluebird/blob/master/docs/docs/warning-explanations.md#warning-a-promise-was-rejected-with-a-non-error
class ErrorWithPayload extends ExtendableError {
constructor(message, {payload}) {
super(message);
this.payload = payload || {};
}
}
const e = new ErrorWithPayload('foo', {my: 'data'});
console.log(e instanceof Error); // true - which is what FSA requires
// Usage
{
type: "MY_TYPE",
payload: new Promise((resolve,reject) => reject(e));
}
We pass all of e (i.e. the ErrorWithPayload) which is of type error and aligns with both. This provides a mechanism to trivially pass along more data.
Do you agree this fits with FSA @tomatau ?
from redux-promise-middleware.
To me this seems to be more the responsibility and choice of people using the middleware rather than something that needs to be supplied? There would be other packages/utilities that aid in the creation of error objects.
I feel the error object payload should be an instance specific to the use case of the action rather than specific to the API of the middleware.
from redux-promise-middleware.
After seeing more of what you want to add, I agree with @tomatau here. I would argue this middleware should not be responsible for creating an error object since the error itself doesn't actually originate directly from the middleware. In most cases errors will be traced to the function that returns the promise, e.g., a network quest with Fetch()
or something of the like. The job of this middleware is to handle the error supplied by the promise and then fire the correct actions, e.g., fulfilled or rejected.
from redux-promise-middleware.
Right now our rejected actions provide the error
property with a value true
in the rejected action object. I think what we should make the value of that property the Error
object instead of a boolean. This still follows FSA. Thoughts @tomatau @mlucool?
from redux-promise-middleware.
https://github.com/acdlite/flux-standard-action seems to indicate that error should only ever be set to true. "If error has any other value besides true, including undefined and null, the action MUST NOT be interpreted as an error."
I was not suggesting the middleware do anything extra with the error object I am proposing but expose it so everyone who wants to pass data in reject does not need to rewrite this. I can also write a 20 line lib which does the same. I only suggested it here because of how useful it is with promises and if we wanted to do further processing, it would then be standardized where the extra data is. If it's not a good fit, I am not adamant this is the right place and we can close this issue.
from redux-promise-middleware.
I'm still not fully understanding the problem here.
const myActionCreatore = () => ({
type: 'EXAMPLE',
payload: {
promise: new Promise(async (resolve, reject) => {
// do stuff
reject(
new MyCustomError('example error payload', { extra: 'data' })
)
})
}
})
/*
{
type: 'EXAMPLE_REJECTED',
payload: new MyCustomError('example error payload', { extra: 'data' })
error: true
}
*/
This doesn't seem like a library is needed to reject with a custom error payload of an error instance.
You want us to provide a hook to modify the whole payload to be an error object when we resolve an error action object? It's important to note that both fulfilled and rejected actions are resolved, as we catch the error in the middleware to stop uncaught errors and allow it to be passed to the reducer chain.
from redux-promise-middleware.
Related Issues (20)
- Switch to babel-preset-env HOT 1
- Consider Changing Public API HOT 6
- Question regarding catching error on dispatcher or handling it in reducer with _REJECTED HOT 1
- How to use the returned promise in typescript? HOT 2
- README fix: Upgrading Guide links are broken HOT 1
- Update docs with custom type suffixes page HOT 1
- Applying middleware shows invalid definitions (TS) and causes no actions to be dispatched HOT 3
- Update TypeScript Definitions HOT 1
- Release Version 6 HOT 2
- TypeScript definitions excluded in .npmignore HOT 4
- Fix TypeScript ActionType HOT 2
- Add helpers that add suffixes to action types HOT 2
- I don't know how to transfer other arguments HOT 1
- Upgrade URL (docs.psb.codes) is 404'ing HOT 2
- TypeScript: fulfilled action result from dispatch HOT 11
- Typescript Promise not work with AsyncAction HOT 3
- A question about the middleware's return value HOT 2
- Empty payload when pass nullable payload in getAction HOT 3
- Chaining Actions Typescript
- `promiseTypeDelimiter: ""` is ignored and instead uses default delimiter HOT 4
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 redux-promise-middleware.