Nuxt Fetch Error Handling

Jul 27, 2021
<template>  <div>    <template v-if="$fetchState.pending">      Loading ...    </template>    <template v-else-if="$fetchState.error">      Error: {{ $fetchState.error.message }}    </template>    <template v-else>      OK    </template>  </div></template><script>export default {  async fetch() {    const id = this.$route.params.id    if (!id) {      this.error('Missing ID', 404)      return    }    // load data  },  methods: {    error(message, statusCode) {      if (process.server) {        this.$nuxt.context.res.statusCode = statusCode      }      // this doesn't work well under certain condition      // this.$nuxt.error({ message: 'Place not found', statusCode: 404 })      throw new Error(message)    },      }}</script>

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.