Monday, 23 September 2019

How to deal with standard Error object on frontend?

I have a frontend app with many API requests, but it's a pain to work with error responses.

Sometimes I need to go through many nested objects like: error.response.data.email and sometimes it is error.response.data.address.province[0].

I can't predict all of the errors, and writing manual "parsers" looks like a dirty extra solution to me:

const errorsObject = err.response.data
    let errors = ''

    Object.keys(errorsObject).map(key => {
      if (key === 'address') {
        Object.keys(errorsObject.address).map(
          key => (errors += `${key} : ${errorsObject.address[key]}\n`)
        )
      } else {
        errors += `${key} : ${errorsObject[key]}\n`
      }
      return undefined
    })

    yield toast.error(errors)

Also it still doesn't cover everything.

Is there any frontend parsers for that? If not, our backend is Python(Django), maybe there is a package for backend side? Ideally I'd want to see a flat array of objects {title, message}.



from How to deal with standard Error object on frontend?

No comments:

Post a Comment