8000 using try/catch with an async method inside a middleware causes a `nuxt instance unavailable` error. · Issue #14269 · nuxt/nuxt · GitHub
[go: up one dir, main page]

Skip to content
using try/catch with an async method inside a middleware causes a nuxt instance unavailable error. #14269
@amrnn90

Description

@amrnn90

Environment


  • Operating System: Linux
  • Node Version: v17.9.0
  • Nuxt Version: 3.0.0-rc.4
  • Package Manager: npm@8.5.5
  • Builder: vite
  • User Config: modules, runtimeConfig, autoImports
  • Runtime Modules: @nuxtjs/tailwindcss@5.1.3
  • Build Modules: -

Reproduction

Visit the /secret page to see the error:

https://stackblitz.com/edit/nuxt-starter-elpq43?file=middleware%2Fauth.ts

Describe the bug

I'm trying to write an async middleware. However, when catching the rejected promise an error is thrown by nuxt when using navigateTo():

async function fetchUser() {
  throw new Error();
}

export default defineNuxtRouteMiddleware(async (to, from) => {
  let user;

  try {
    user = await fetchUser();
  } catch (e) {
    user = null;
  }

  if (!user) return navigateTo('/');
});

Additional context

No response

Logs

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0