1

send an axios put request and getting status code 400 error.

Server sends a user friendly validation eror message it looks like in dev tools: enter image description here

in catch block I expect to get the message with error.message but this equals to Request failed with status code 400

  try {
    dispatch({
      type: SETTINGS_ALERT,
    })
    const response = await axios.put(
      `/author/me`,
      {
        data: {
          DisplayName: displayName,
          Description: description,
        },
      }
    )
    dispatch({
      type: SETTINGS_UPDATE_PROFILE_SUCCESS,
      payload: response.data,
    })
  } catch (error) {
//need to handle it here, bu error.message not works
    dispatch({
      type: SETTINGS_LOAD_FAIL,
      error: error.message,
    })
  }

How can I read this message properly??

2
  • try using console.log(String(error) first. See what comes up. Commented May 13, 2021 at 16:41
  • I think that error.response.data is what you are looking for Commented May 13, 2021 at 16:46

1 Answer 1

2

The detailed message you want is in error.response.data:

dispatch({
      type: SETTINGS_LOAD_FAIL,
      error: error.response.data,
    })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.