krmzv
krmzv

Reputation: 387

Redux-Form - Array of errors

I'm stuck with handling array of objects that contain errors inside my react-native/redux application.

I have a handleResponse function that looks something like this:

function handleResponse (response) {
  let status = JSON.stringify(response.data.Status)
  let res = JSON.stringify(response)
  if (Number(status) === 200) {
    return res
  } else {
    throw new SubmissionError('Something happened')
  }
}

But instead of plain text passed as argument to SubmissionError function - I want somehow to take out errors from array of objects.

Array of objects containing error messages looks like this:

{
  ErrorMessages: [
    { ErrorMessage: 'foo' },
    { ErrorMessage: 'bar' }
  ]
}

How can I, for example, throw foo and bar errors?

Upvotes: 0

Views: 89

Answers (1)

Preview
Preview

Reputation: 35846

You can't really throw two things at once, unless you wrap them in a Promise or do other tricks, I would just concatenate the errors together and do a single throw:

function handleResponse (res) {
  if (Number(res.data.Status) === 200) {
    return JSON.stringify(res)
  }

  const errors = res.ErrorMessages.map(e => e.ErrorMessage).join(', ')
  throw new SubmissionError(`Some errors occurred: ${errors}.`)
}

Upvotes: 1

Related Questions