Skip to content

Commit a8234d3

Browse files
authored
fix: hide unhandled error messages in prod (#2591)
1 parent b485a27 commit a8234d3

2 files changed

Lines changed: 32 additions & 21 deletions

File tree

src/runtime/internal/error.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ interface ParsedError {
2121

2222
export default defineNitroErrorHandler(
2323
function defaultNitroErrorHandler(error, event) {
24-
const { stack, statusCode, statusMessage, message } = normalizeError(error);
24+
const { stack, statusCode, statusMessage, message } = normalizeError(
25+
error,
26+
isDev
27+
);
2528

2629
const showDetails = isDev && statusCode !== 404;
2730

src/runtime/internal/utils.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,41 @@ export function isJsonRequest(event: H3Event) {
5555
);
5656
}
5757

58-
export function normalizeError(error: any) {
58+
export function normalizeError(error: any, isDev?: boolean) {
5959
// temp fix for https://github.com/unjs/nitro/issues/759
6060
// TODO: investigate vercel-edge not using unenv pollyfill
6161
const cwd = typeof process.cwd === "function" ? process.cwd() : "/";
62-
const stack = ((error.stack as string) || "")
63-
.split("\n")
64-
.splice(1)
65-
.filter((line) => line.includes("at "))
66-
.map((line) => {
67-
const text = line
68-
.replace(cwd + "/", "./")
69-
.replace("webpack:/", "")
70-
.replace("file://", "")
71-
.trim();
72-
return {
73-
text,
74-
internal:
75-
(line.includes("node_modules") && !line.includes(".cache")) ||
76-
line.includes("internal") ||
77-
line.includes("new Promise"),
78-
};
79-
});
62+
63+
// Hide details of unhandled/fatal errors in production
64+
const hideDetails = !isDev && (error.unhandled || error.fatal);
65+
66+
const stack = hideDetails
67+
? []
68+
: ((error.stack as string) || "")
69+
.split("\n")
70+
.splice(1)
71+
.filter((line) => line.includes("at "))
72+
.map((line) => {
73+
const text = line
74+
.replace(cwd + "/", "./")
75+
.replace("webpack:/", "")
76+
.replace("file://", "")
77+
.trim();
78+
return {
79+
text,
80+
internal:
81+
(line.includes("node_modules") && !line.includes(".cache")) ||
82+
line.includes("internal") ||
83+
line.includes("new Promise"),
84+
};
85+
});
8086

8187
const statusCode = error.statusCode || 500;
8288
const statusMessage =
8389
error.statusMessage ?? (statusCode === 404 ? "Not Found" : "");
84-
const message = error.message || error.toString();
90+
const message = hideDetails
91+
? "internal server error"
92+
: error.message || error.toString();
8593

8694
return {
8795
stack,

0 commit comments

Comments
 (0)