question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot catch error with $fetch (ohmyfetch)

See original GitHub issue

Environment

Nuxt project info:


  • Operating System: Windows_NT
  • Node Version: v16.13.1
  • Nuxt Version: 3.0.0-rc.11
  • Nitro Version: 0.5.4
  • Package Manager: npm@8.5.2
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

<script lang="ts" setup>
import axios from 'axios'

const props = defineProps({
    image: {
        type: String
    }
})

const isExist = ref(false)

const isFileExist = async (filepath: string) => {
    try {
        await $fetch(filepath)
        return true
    } catch (e) {
        return false
    }
}

onMounted(async () => {
    isExist.value = await isFileExist(props.image)
})
</script>

<template>
    <img :src="isExist ? image ? '../assets/images/default.jpg'" />
</template>

Describe the bug

Hello everyone ! I want to make a function that check if an image exist in my project folder. My function works but when I use try catch statement, I have error 404 (when image is not found) display on the console. I want to catch this error with my catch sattement but it doesn’t work.

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
david4boucommented, Oct 1, 2022

Thanks man

0reactions
manniLcommented, Oct 1, 2022

See updated StackBlitz

Anyway, this issue can be closed 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to catch the error when making fetch api request?
When a fetch request comes back with an error code from the server, the catch of the promise is not executed, the then...
Read more >
ohmyfetch - npm
Start using ohmyfetch in your project by running `npm i ohmyfetch`. There are 96 other projects in the npm registry using ohmyfetch.
Read more >
Handling errors with useFetch : r/Nuxt - Reddit
Hi, I'm trying starting to mess around with useFetch , and I can't understand how you handle errors with it.
Read more >
How to Timeout a fetch() Request - Dmitri Pavlutin
You can use the expression error.name === 'AbortError' inside the catch block to determine if there was a request timeout. Open the demo...
Read more >
Error handling while using native fetch API in JavaScript
We will see, how to handle fetch API errors using promises and async ... This will fix the problem, you can even extract...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found