pagekit / vue-resource

The HTTP client for Vue.js
MIT License
10.08k stars 1.6k forks source link

Can't handle the error callback in this.$http.post #596

Open Kuchiriel opened 7 years ago

Kuchiriel commented 7 years ago

I receive a valid response from success callback, but from the error callback I don't receive any valid response that I can handle.

I am using it on latest Chromium with vue-admin.

Code: https://jsfiddle.net/cbgjvmxb/

Thanks.

Kuchiriel commented 7 years ago

I discovered the problem, when the backend returns a status code that is considered an error, such as 400, a problem occurs. But if I use a http code such as 200, I can get the object with the status code and the message.

commercial-hippie commented 7 years ago

@Kuchiriel have you managed to get this one working? I have the same issue.

Kuchiriel commented 7 years ago

@commercial-hippie I still using status code 200, and I create a separate function to handle both response and error, giving priority to the positive status code.

addUser: function () {
      this.axios.post(`${defaultURL}/v1/register`, {
        email: toLower(this.email),
        nome: toLower(this.nome),
        perfil: toLower(this.perfil),
        unidade: toLower(this.unidade),
        senha: this.senha
      }).then(res => {
        this.notify(res)
        this.close()
      }).catch(err => {
        this.notify(err)
      })
    },
Example: notify: function (res) {
      if (res.status === 201) {
        openNotification({
          title: 'Gerenciamento',
          message: res.data.message,
          type: 'success'
        })
      } else {
        console.log(res)
        openNotification({
          title: 'Erro',
          message: `${res.message},
          reporte para os desenvolvedores da aplicação`,
          type: 'danger'
        })
      }
    },
commercial-hippie commented 7 years ago

@Kuchiriel interesting, okay thanks for that!