SolidJSS
SolidJSโ€ข14mo agoโ€ข
2 replies
Cone

Throwing a redirect inside a query

When throwing a redirect from a query, the throw gets caught and returns undefined instead. The inferred return type is wrong in this case, but when throwing other values the throw is not caught. Is the query function typing wrong, or am I using query wrong?

import { query, redirect } from "@solidjs/router"

const queryThatThrows = query(async () => {
  "use server"
  const condition = true
  if (condition) {
    throw redirect("/foo")
  }
  return true
}, "query-that-throws")

const otherQuery = query(async () => {
  "use server"
  const v = await queryThatThrows()
  type V = typeof v // boolean
  // this still runs because the throw gets caught
  console.log(v) // undefined
}, "other-query")
Was this page helpful?