new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Nuxt · API · all subjects

utils/createerror

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

createError function signature

createError accepts either a string or an object as its parameter. If passed a string, it is used as the error message and status defaults to 500. If passed an object, you can set multiple properties: cause, data, message, name, stack, status, statusText, and fatal.

createError behavior on server-side

When an error created with createError is thrown on the server-side, it triggers a full-screen error page which can be cleared with clearError.

createError with cause parameter

The cause parameter preserves the original error being wrapped. In development, the cause chain is exposed via the cause property of the error, serialized as {name, message, stack, cause} where primitive causes are passed through as-is and other values are omitted. In production, causes are never included in error responses or in the error page payload.

createError in API routes with statusText

In API routes, using createError by passing an object with a short statusText is recommended because it can be accessed on the client side. A message passed to createError on an API route will not propagate to the client.

createError data property in API routes

When handling an error with useFetch, custom data passed via the data property of createError is available at error.value.data.data on the client side.

createError security consideration

Avoid putting dynamic user input in the message property to avoid potential security issues.

createError Vue example

Example showing createError in a page component: when useFetch returns no data, throw createError({ status: 404, statusText: 'Page Not Found' }) to display an error page.

createError API route example

Example showing createError in an API route: export default eventHandler(() => { throw createError({ status: 404, statusText: 'Page Not Found' }) })

createError cause wrapping example

Example showing how to wrap an original error with createError: try { await fetchMovie(route.params.slug) } catch (cause) { throw createError({ status: 500, message: 'Could not load movie', cause }) }

Give your agent this brain