Cannot catch error with $fetch (ohmyfetch)
See original GitHub issueEnvironment
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:
- Created a year ago
- Comments:10 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks man
See updated StackBlitz
Anyway, this issue can be closed 👍