How can I fetch the error message from 422 Unprocessable Entity response?

Viewed 579

I need some help about to get the error message from 422 Unprocessable Entity response.

I have this function as you can see here:

  const upDateMyProfile = async (path, data) => {
    state.loading = true
    try {
      const account = {
        account_gender: data.value.account.gender,
        account_firstname: data.value.account.firstname,
        account_lastname: data.value.account.lastname,
        partner_gender: data.value.partner.gender,
        partner_firstname: data.value.partner.firstname,
        partner_lastname: data.value.partner.lastname
      }
      const req = await fetch(`${url}/${path}`, {
        method: 'PUT',
        body: JSON.stringify(account),
        headers: {
          'Content-Type': 'application/json',
          Accept: 'application/json',
          Authorization: `Bearer ${AUTH_TOKEN.accesstoken}`
        }
      })
      state.toast = true
      state.statusCode = req.status
      if (req.status !== 204) {
        console.log()
      }
      return req
    } catch (error) {
      state.error = error
      state.toast = true
    } finally {
      state.loading = false
      setTimeout(() => {
        state.toast = false
      }, TIMEOUT)
    }
  }

Is a function to update a user profile details and the problem I have is I don't know how to get the error message from the server when a user sends an invalidate data value or leave a mandatory field empty.

The server respond with a 422 error: enter image description here

0 Answers
Related