From 5d34725344dfdaaaaaf053f8563004f853824c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Mon, 7 Dec 2020 00:36:36 +0100 Subject: [PATCH] API connection error handling --- src/store/actions.js | 10 ++++++++-- src/views/Fruit.vue | 2 +- src/views/Fruits.vue | 40 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index 0033257..1daf38c 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -2,8 +2,14 @@ import axios from "axios"; export default { getFruits: async ({ commit }) => { - const response = await axios.get("http://localhost:3000/fruit"); - commit("setFruits", response.data.data); + await axios + .get("http://localhost:3000/fruit") + .then(res => { + commit("setFruits", res.data.data); + }) + .catch(err => { + if (err.message === "Network Error") throw "Check API connectivity ..."; + }); }, getFruit: async ({ commit }, id) => { const response = await axios.get(`http://localhost:3000/fruit/${id}`); diff --git a/src/views/Fruit.vue b/src/views/Fruit.vue index d8a9f20..30c6deb 100644 --- a/src/views/Fruit.vue +++ b/src/views/Fruit.vue @@ -128,7 +128,7 @@ article { .alert { margin-top: 1.75rem; - background-color: lighten(@color-1, 10%); + background: linear-gradient(112.4deg, #1c9797 11.05%, #147171 89.93%); color: @color-2; padding: 1rem 1.5rem; border-radius: 4px; diff --git a/src/views/Fruits.vue b/src/views/Fruits.vue index 95dd4f5..989e57e 100644 --- a/src/views/Fruits.vue +++ b/src/views/Fruits.vue @@ -1,7 +1,11 @@ @@ -15,15 +19,45 @@ export default { computed: { ...mapState(["fruits"]) }, + data() { + return { + error: null + }; + }, async created() { - await this.$store.dispatch("getFruits"); + await this.$store.dispatch("getFruits").catch(err => (this.error = err)); } }; -