nuxt: `throw createError` does not show error page on client-side (in development)

Environment

  • Operating System: Windows_NT
  • Node Version: v16.16.0
  • Nuxt Version: 3.0.0-rc.12
  • Nitro Version: 0.6.0
  • Package Manager: yarn@1.22.10
  • Builder: vite
  • User Config: build, modules, loadingIndicator, app, css
  • Runtime Modules: nuxt-windicss@2.5.2, @pinia/nuxt@0.4.3
  • Build Modules: -

Reproduction

When I enter a page directly via the browser e.g: 192.168.1.1/test (it shows me an 404 page error, which is fine). But when I navigate to an existing page (which loads normally) and then click on a button with a internal link like: 192.168.1.1/hello it provides an error. It is unable to create an error.

https://stackblitz.com/edit/github-dk8jtl?file=app.vue

Click on the two buttons named “Team” and “Contact”. Everything working fine. Click then on “Error” button. Please check console for the error message which I provided.

Describe the bug

Uncaught (in promise) Error: Page Not Found at createError (index.mjs?v=f2c40dfc:35:15)

const err = new H3Error(input.message ?? input.statusMessage, input.cause ? { cause: input.cause } : void 0);

Additional context

I generate the error with the following line; throw createError({ statusCode: 404, statusMessage: 'Page Not Found' })

Logs

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 15
  • Comments: 46 (13 by maintainers)

Commits related to this issue

Most upvoted comments

As a workaround I use both showError() and createError()

In <script setup> context:

const { data, error } = await useAsyncData(route.path, async () => {
  try {
    const result = await $fetch('/api/call')

    return { result }
  } catch (e) {
    // This will trigger an error page client side
    showError({ message: 'Page not found', statusCode: 404 })
  }
})

if (error.value) {
  // This will trigger an error page server side
  throw createError({ message: 'Page not found', statusCode: 404 })
}

Please update documentation, because this is very confusing

fatal:true is working fine now on version 3.7.1 of nuxt !

This solution works for the moment. But, since it is recommended instead to use throw createError(). I hope it’s fixed in coming updates.

Edit: the clearError function is not being called from the back to home button in case of showError

As a workaround I use both showError() and createError()

In <script setup> context:

const { data, error } = await useAsyncData(route.path, async () => {
  try {
    const result = await $fetch('/api/call')

    return { result }
  } catch (e) {
    // This will trigger an error page client side
    showError({ message: 'Page not found', statusCode: 404 })
  }
})

if (error.value) {
  // This will trigger an error page server side
  throw createError({ message: 'Page not found', statusCode: 404 })
}

If you want an error page displayed on client, you need to set fatal: true in createError. Otherwise we assume you may handle the error with an error boundary or custom error handler.

useAsyncData will never throw an error. You need to handle the contents of error.value.

@danielroe I see that it was closed but the behavior between development and not development is something that at least to me is unexpected and seems more like a bug or design issue based on the discussion above where it is not expected by others.

How do we handle ensuring that we render the same pages in development and production?

The flow as an example

  • Authentication library that throws a 403 forbidden error
  • Wrapping middleware throws the exception in a way that will should work in the new nuxt3 system

I’ve tried this with and without fatal: true

  throw createError({statusCode: e.statusCode, fatal: true})

Hello, kindly let us know about any planned update on this bug? It’s not possible to show error page on client side as the fatal: true parameter doesn’t work as mentioned in the docs. The only workaround is to make a custom error component and display it in case of error from useFetch() https://nuxt.com/docs/getting-started/error-handling#createerror