Skip to content

Commit f44346a

Browse files
committed
stack in error response made optional based on showStack
1 parent 65cc03b commit f44346a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
A lightweight, production-ready error handling toolkit for Express.js applications — written in TypeScript with full support for both CommonJS and ESM.
1313

14+
If you like the project, please give the project a GitHub ⭐
15+
1416
It provides:
1517

1618
- Custom error classes (`NotFoundError`, `BadRequestError`, `ValidationError`, etc.)

src/global-error-handler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface ErrorResponse {
77
success: false;
88
message: string;
99
errorDetails?: string | object | null;
10-
stack?: string;
10+
stack?: string | string[];
1111
}
1212

1313
export const globalErrorHandler = (
@@ -46,6 +46,13 @@ export const globalErrorHandler = (
4646
errorResponse.stack = stack;
4747
}
4848

49+
const isDev = process.env.NODE_ENV?.trim() === 'development';
50+
const showStack = process.env.SHOW_STACK !== 'false';
51+
52+
if (isDev && stack && showStack) {
53+
errorResponse.stack = stack.split('\n').map((line) => line.trim());;
54+
}
55+
4956
if (process.env.NODE_ENV === 'development') {
5057
console.error(err);
5158
}

0 commit comments

Comments
 (0)