7

According to ES2017 spec the message property of Error is non-enumerable. I'm wondering why so? With non-enumerable property message it's impossible for example to spread the Error object like

const payload = new Error('oops!');    

// Few lines after

const nextState = {
   ...payload
};

This especially useful when working with Redux and SFA(Standard Flux Actions). SFA allows the payload of an action to be an Error object. Such action can be processed by a reducer like

// e.g. with redux-actions library
// that provides `handleActions` helper

export default handleActions({  
   [ERROR_ACTION]: (state, ({ payload }) => ({ ...state, ...payload })
}, initialState)

But because of message property of Error is non-enumerable the payload from the code above won't be spreaded.

Has anyone a good answer for this?

  • Stack Overflow is not a good place for questions about language design decisions. You are probably better off asking on https://esdiscuss.org/ . – Felix Kling Feb 17 '17 at 00:11
  • If the state is just an error object, why not just set that as the state directly? Object Spread is for merging objects, merging multiple instances of a class rarely makes sense... – loganfsmyth Feb 17 '17 at 03:33
  • 1
    **Felix Kling**, thank you fro the link! **loganfsmyth**, yeah, I know. This is a simplified example not fully covering the problem. – Artyom Stepanishchev Feb 18 '17 at 10:10

0 Answers0