How to render error page from fetch hook? #9191
Answered
by
clemcode
kingRayhan
asked this question in
Questions (Nuxt 2)
-
I can render an error page from export default {
asyncData({ error }) {
try {
// TODO: http call
} catch (e) {
error({
statusCode: e.response.statusCode,
message: e.response.message,
})
}
},
} Now I want to know, if I want to render error page from export default {
fetch(){
try {
// TODO: http call
} catch (e) {
this.$error({
statusCode: e.response.statusCode,
message: e.response.message,
})
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
clemcode
May 4, 2021
Replies: 1 comment
-
Hi, Error handling in the
<div>
<p v-if="$fetchState.pending">Fetching posts...</p>
<p v-else-if="$fetchState.error">Error while fetching posts</p>
<ul v-else>
…
</ul>
</div> Take a look at this post for the details : https://nuxtjs.org/blog/understanding-how-fetch-works-in-nuxt-2-12/#error-handling |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
danielroe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Error handling in the
fetch()
hook occurs at component level, which means you can't directly render the error page as you would usingasyncData()
.fetch()
provides a$fetchState
object that you can use in your template to handle pending and error cases like so :Take a look at this post for the details : https://nuxtjs.org/blog/understanding-how-fetch-works-in-nuxt-2-12/#error-handling