Skip to content

Commit 2300296

Browse files
committed
console made more readable by adding an intro line
1 parent e38480e commit 2300296

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-error-toolkit",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "A lightweight and developer-friendly toolkit for robust error handling in Express.js applications.\nIncludes ready-to-use custom error classes, an async route error handler wrapper, a global error handler middleware, and a convenient 'not found' route handler.\nDesigned to simplify error management, reduce boilerplate, and improve app reliability — all with TypeScript support and easy integration.",
55
"author": "Rashedin Islam <[email protected]>",
66
"license": "MIT",

src/global-error-handler.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
yellow,
1010
boldGreen,
1111
green,
12+
dimGray,
1213
} from './utils/console-colors';
14+
import { centerText } from './utils';
1315

1416
// Internal config object (optional override)
1517
let errorOptions = {
@@ -73,6 +75,8 @@ export const globalErrorHandler = (
7375

7476
// Log the error if configured to do so
7577
if (errorOptions.logError) {
78+
console.error(`\n${centerText(dimGray('Error Log'))}\n`);
79+
7680
// log the error status
7781
console.error(
7882
`${boldRed('🔴 Error Status:')} ${red(String(errorResponse.status))}`
@@ -82,12 +86,12 @@ export const globalErrorHandler = (
8286

8387
// log the error message
8488
console.error(
85-
`${boldRed('🔴 Error Message:')} ${red((errorResponse.message))}`
89+
`${boldRed('🔴 Error Message:')} ${red(errorResponse.message)}`
8690
);
87-
88-
console.error(); // empty line for better readability
8991

90-
// Log error details if available
92+
console.error(); // empty line for better readability
93+
94+
// Log error details if available
9195
if (errorResponse.errorDetails) {
9296
console.error(boldYellow('🟡 Error Details:'));
9397
console.error(
@@ -99,16 +103,25 @@ export const globalErrorHandler = (
99103
);
100104
}
101105

102-
console.error(); // empty line for better readability
106+
console.error(); // empty line for better readability
103107

104108
// Log stack trace if available
105109
if (errorResponse.stack) {
106-
console.error(boldGreen('🟢 Stack Trace:'));
107-
(errorResponse.stack as string[]).forEach((line) =>
108-
console.error(green(line))
109-
);
110+
const stackLines = errorResponse.stack as string[];
111+
const previewLines = stackLines.slice(0, 3);
112+
const remainingCount = stackLines.length - previewLines.length;
113+
114+
console.error(boldGreen('🟢 Stack Trace (Preview):'));
115+
previewLines.forEach((line) => console.error(green(line)));
116+
117+
if (remainingCount > 0) {
118+
console.error(
119+
dimGray(
120+
`...and ${remainingCount} more lines. 💡 See full stack in error respons`
121+
)
122+
);
123+
}
110124
}
111125
}
112-
113126
res.status(statusCode).json(errorResponse);
114127
};

src/utils/console-colors.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ const colors = {
22
reset: '\x1b[0m',
33
bold: '\x1b[1m',
44
red: '\x1b[31m',
5-
yellow: '\x1b[33m',
65
green: '\x1b[32m',
6+
yellow: '\x1b[33m',
7+
dimGray: '\x1b[2m',
8+
underline: '\x1b[4m',
79
};
810

911
export function boldRed(text: string) : string {
@@ -29,3 +31,8 @@ export function boldGreen(text: string) : string {
2931
export function green(text: string) : string {
3032
return `${colors.green}${text}${colors.reset}`;
3133
}
34+
35+
36+
export function dimGray(text: string) : string {
37+
return `${colors.dimGray}${colors.underline}${text}${colors.reset}`;
38+
}

src/utils/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function centerText(
2+
text: string,
3+
width = process.stdout.columns || 80
4+
): string {
5+
const padding = Math.floor((width - text.length) / 2);
6+
return ' '.repeat(Math.max(padding, 0)) + text;
7+
}

0 commit comments

Comments
 (0)