From 42ef331816aabf3ebea23fc9e6bd1ca46f0cec23 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 17 Jul 2024 12:13:56 +0100 Subject: [PATCH] fix(nuxt): hide unhandled error messages in prod (#28156) --- packages/nuxt/src/core/runtime/nitro/error.ts | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/packages/nuxt/src/core/runtime/nitro/error.ts b/packages/nuxt/src/core/runtime/nitro/error.ts index 89f7ef65c6..7620f96981 100644 --- a/packages/nuxt/src/core/runtime/nitro/error.ts +++ b/packages/nuxt/src/core/runtime/nitro/error.ts @@ -115,29 +115,34 @@ function normalizeError (error: any) { // temp fix for https://github.com/unjs/nitro/issues/759 // TODO: investigate vercel-edge not using unenv pollyfill const cwd = typeof process.cwd === 'function' ? process.cwd() : '/' - const stack = ((error.stack as string) || '') - .split('\n') - .splice(1) - .filter(line => line.includes('at ')) - .map((line) => { - const text = line - .replace(cwd + '/', './') - .replace('webpack:/', '') - .replace('file://', '') - .trim() - return { - text, - internal: - (line.includes('node_modules') && !line.includes('.cache')) || - line.includes('internal') || - line.includes('new Promise'), - } - }) + + // Hide details of unhandled/fatal errors in production + const hideDetails = !import.meta.dev && error.unhandled + + const stack = hideDetails + ? [] + : ((error.stack as string) || '') + .split('\n') + .splice(1) + .filter(line => line.includes('at ')) + .map((line) => { + const text = line + .replace(cwd + '/', './') + .replace('webpack:/', '') + .replace('file://', '') + .trim() + return { + text, + internal: + (line.includes('node_modules') && !line.includes('.cache')) || + line.includes('internal') || + line.includes('new Promise'), + } + }) const statusCode = error.statusCode || 500 - const statusMessage = - error.statusMessage ?? (statusCode === 404 ? 'Not Found' : '') - const message = error.message || error.toString() + const statusMessage = error.statusMessage ?? (statusCode === 404 ? 'Not Found' : '') + const message = hideDetails ? 'internal server error' : (error.message || error.toString()) return { stack,