question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why SHOULD payload be an error object? error: true should be enough

See original GitHub issue

I think this is quite limiting.

You say this —

  • An action whose error is true is analogous to a rejected Promise

Isn’t that enough? What if I deal with a server that sends helpful error code and messages in case of failure? For example a 401 response may include something like this:

{
  "code": "WRONG_PASSWORD",
  "message": "Password field should not be empty"
}

So I naturally dispatch this object as payload and set { error: true }.

I guess what you’re implying is that I should put this error object inside the meta property. This seems contradictory to me. This is object is obviously payload.


Another thing that bothers me is that I can’t find a payload of type Error useful for anything. It can only hold a string, so it’s pretty useless to keep in state, isn’t it? (not to say that strings are useless, but you can see that we could pass much more information about the error)

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:12
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
unionalcommented, Feb 12, 2018

@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.

1reaction
wedneyyuricommented, Oct 7, 2019

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;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Exception payload / error handling - Google Groups
In this case field represents a json property locator for the field that failed (based on the location of the error object) and...
Read more >
javascript - Error: request entity too large - Stack Overflow
When one bloated and redundant property was removed, the payload size was reduced by a factor of a 100. This significantly improved performance...
Read more >
Error handling - Apollo GraphQL Docs
Apollo Server throws errors automatically when applicable. For example, it throws a GRAPHQL_VALIDATION_FAILED error whenever an incoming operation isn't valid ...
Read more >
Error handling in TypeScript like a pro - Journal - Plain
Errors are a part of every application. The simplest approach you could take is to not handle any errors, but that's not great...
Read more >
Use case-specific error handling in Mule 4 | MuleSoft Blog
The basic idea of this flow is that the flow will attempt to assert that the payload is number (which will fail because...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found