Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better errors #3002

Merged
merged 16 commits into from
Jan 15, 2025
Merged

feat: better errors #3002

merged 16 commits into from
Jan 15, 2025

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Jan 14, 2025

This PR improves request error handlers with different implementations for development and production.

Followup:

  • Upstream sourcemap loader support to youch-core as an opt-in feature
  • Track ansi wrap issue

Errors in development

Using poppinss/youch 4.x for HTML rendering: (only if accept header explicitly includes text/html ~ browsers)

image

Note

Sourcemaps for the app are automatically applied, without the need of NODE_OPTIONS="--enable-source-maps".

JSON responses (when the accept header does not include text/html) are consistent with production errors and have more useful fields:

{
  "error": true,
  "url": "http://localhost:3000/",
  "statusCode": 500,
  "statusMessage": "Server Error",
  "message": "This is an error",
  "data": {
    "foo": "bar"
  },
  "stack": [
    "This is an error",
    "at createError (/Users/pooya/Code/nitro/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:72:15)",
    "at Object.handler (/Users/pooya/Code/nitro/playground/routes/index.ts:2:0)",
    "at Object.handler (/Users/pooya/Code/nitro/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1889:24)",
    "at Object.handler (/Users/pooya/Code/nitro/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:2202:34)",
    "at Object.handler (/Users/pooya/Code/nitro/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1963:31)",
    "at process.processTicksAndRejections (node:internal/process/task_queues:105:5)",
    "at async Server.toNodeHandle (/Users/pooya/Code/nitro/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:2255:7)"
  ]
}

For console errors (for unhandled or fatal errors) use consola + Youch.toANSI and include more info including method, URL, cause and source 🔥

image

Worker entry errors are now also improved by leveraging the same dev error handler:

image image

Errors in production

Nitro errors in production are always JSON with application/json mime:

{
  "error": true,
  "url": "http://localhost:3000/",
  "statusCode": 500,
  "statusMessage": "Server Error",
  "message": "Server Error",
  "data": {
    "foo": "bar"
  }
}

Server console errors include request info, and error data + cause (if any) using native runtime console.error handler for formatting:

image

Note

In production, NODE_OPTIONS="--enable-source-maps" is required in order to enable sourcemaps.

@pi0 pi0 marked this pull request as ready for review January 14, 2025 22:04
@pi0 pi0 merged commit b38da2c into v2 Jan 15, 2025
4 checks passed
@pi0 pi0 deleted the feat/better-dev-err branch January 15, 2025 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant