Comments (5)
@everdimension after using FSA for a while, yes I do agree with you that dispatched action is different.
For one thing, Error
is not that serializable (JSON.stringify(new Error('abc'))
gets an empty object string {}
).
But on the other hand, putting Error
(in a serializable format) in meta
is not a good idea because meta
can be used by middlewares and you run the risk of overwriting that information.
IMO, { payload: <serializable error>, error: true }
vs { error: <serializable error> }
is about the same. You have to check for if (action.error) {...}
anyway.
As for your first suggestion — extending the javascript Error object — I have to think about it, but I'm not sure it's such a good idea.
My understanding is that you should extend an Error instance with custom information only for debugging purposes, not to pass it around as data.
I do see the value of carrying custom information by extending Error is very valuable.
The are three consumers of an Error: some Engineer, some logging mechanism, and the JavaScript engine.
The JavaScript engine doesn't care, it just spilled out the error.
The logging mechanism does care. Theerror.message
often lacks the context for the logging to produce any meaningful message.
The engineer does care. It is never a good idea to try handling the error by parsing the error message. That is fragile, unreliable, and slow.
from flux-standard-action.
How do you carry the "request original data" in case of error? I was using payload to track id's of affected resources, but following this guide the payload attribute should be an error.
My state:
export interface Item {
objectID: string,
listPrice?: number,
dealPrice?: number,
}
export interface State {
[objectID: string]: Item;
}
My previous reducer:
export default function reducer(state: State = {}, action: ActionTypes): State {
switch (action.type) {
case ITEM_FETCH_REQUESTED:
case ITEM_FETCH_SUCCEEDED:
case ITEM_FETCH_FAILED:
return {
...state,
[action.payload.objectID]: item(state, action), // <= how to get the objectID of a failed entity?
};
default:
return state;
}
}
from flux-standard-action.
You can extend Error
to add more information to it.
IMO, as with rejecting promise, the rejecting parameter should be an Error
.
In fact, I believe rejecting promise MUST reject with an Error
.
That's how it works in async/await
situation:
function foo() {
return Promise.reject(new Error('something'))
}
async function boo() {
try {
await foo()
}
catch (err) {
// `err` should always be of type `Error`
}
}
https://stackoverflow.com/questions/42453683/how-to-reject-in-async-await-syntax/42453798#42453798
from flux-standard-action.
In fact, I believe rejecting promise MUST reject with an Error.
That's how it works in async/await situation
I see your point, but I'd say that the dispatched action is of different nature compared to a promise chain.
When using async or promises, you can easily keep the initial request object in scope and access it in the catch
block.
On the other hand, the idea of flux actions is that they can come into your app independently. When you're receiving an action, you usually do not have another action (which might've been an "initiator" in some sense) in scope or in a closure.
Dispatched action should be sufficient in a way that a reducer can describe the state update accordingly. Making payload
a faceless error strips this crucial information from the action.
As for your first suggestion — extending the javascript Error
object — I have to think about it, but I'm not sure it's such a good idea.
My understanding is that you should extend an Error
instance with custom information only for debugging purposes, not to pass it around as data.
from flux-standard-action.
For one thing, Error is not that serializable (JSON.stringify(new Error('abc')) gets an empty object string {})
btw, regarding this, I have created iso-error
to mitigate the problem.
from flux-standard-action.
Related Issues (20)
- Optional payload/meta properties HOT 4
- Adding types with optional properties made required HOT 4
- `ErrorFSA` type alias is incorrectly formatted HOT 2
- Migrate types to DefinitelyTyped
- Looking for Maintainers HOT 2
- In case of error, should meta be enforced?
- Why must an action "NOT include properties other than type, payload, and error, and meta"? HOT 4
- Flow equalent HOT 1
- An in-range update of eslint-config-airbnb-base is breaking the build 🚨 HOT 3
- An in-range update of cross-env is breaking the build 🚨 HOT 3
- Typings make meta a required property HOT 2
- An in-range update of mocha is breaking the build 🚨
- An in-range update of eslint-config-airbnb-base is breaking the build 🚨
- An in-range update of conventional-github-releaser is breaking the build 🚨 HOT 2
- Meta still not optional HOT 1
- GeoJSON conflicts with key `type` HOT 2
- No longer exporting `Action` type? HOT 1
- Suggestion - Provide class/factory function as default export HOT 6
- Request: isErrorAction utility function HOT 2
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 flux-standard-action.