cretueusebiu / vform

Handle Laravel-Vue forms and validation with ease.
https://vform.vercel.app
MIT License
607 stars 120 forks source link

How to catch errors from "form.post" #105

Closed balduinofernando closed 4 years ago

balduinofernando commented 4 years ago

Let's say I have this code:

async saveCategory() {
      await this.form
        .submit("post", "/api/category")
        .then(response => {
          Toast.fire({
            type: "success",
            title: response.data.message
          });
          $("#newCategoryModal").modal("hide");
        })
        .catch(error => {
          /* Toast.fire({
            type: "error",
            title: error.message
          }); */
        });
    }

The problem I had was that the catch block is not working. For example I can't show the error using SweetAlert or a custom Flash utility on the .catch(error=>{}) Block.

Is there any way of doing this?

cretueusebiu commented 4 years ago

When you use async/await you must use a try/catch block:

async saveCategory() {
  try {
    const response = await this.form.post(....)
    console.log(response.data)
  } catch (e) {
    console.log(e, e.response.data) 
  }
}