Skip to content

Commit 812aa7f

Browse files
committed
global error handler updated
1 parent 718b419 commit 812aa7f

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
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.0",
3+
"version": "1.1.1",
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: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { Request, Response, NextFunction } from 'express';
22
import { StatusCodes, getStatusMessage } from 'http-status-toolkit';
3-
import { isCustomAPIError } from './checking-custom-api-error';
3+
import { isCustomAPIError } from './checking-custom-api-error';
44
import { CustomAPIError } from './error';
5-
import { boldRed, red, boldYellow, yellow, boldGreen, green } from './utils/console-colors';
5+
import {
6+
boldRed,
7+
red,
8+
boldYellow,
9+
yellow,
10+
boldGreen,
11+
green,
12+
} from './utils/console-colors';
613

714
// Internal config object (optional override)
815
let errorOptions = {
9-
showStack: process.env.SHOW_STACK !== 'false' && process.env.NODE_ENV !== 'production',
16+
showStack:
17+
process.env.SHOW_STACK !== 'false' && process.env.NODE_ENV !== 'production',
1018
logError:
1119
process.env.LOG_ERROR !== 'false' && process.env.NODE_ENV !== 'production',
1220
};
1321

14-
export function setErrorOptions(
15-
options: Partial<typeof errorOptions>
16-
) {
22+
export function setErrorOptions(options: Partial<typeof errorOptions>) {
1723
errorOptions = {
1824
...errorOptions,
1925
...options,
@@ -22,6 +28,7 @@ export function setErrorOptions(
2228

2329
export interface ErrorResponse {
2430
success: false;
31+
status: number;
2532
message: string;
2633
errorDetails?: string | object | null;
2734
stack?: string | string[];
@@ -34,13 +41,12 @@ export const globalErrorHandler = (
3441
_next: NextFunction
3542
) => {
3643
let statusCode: number = StatusCodes.INTERNAL_SERVER_ERROR;
37-
let message = getStatusMessage(StatusCodes.INTERNAL_SERVER_ERROR);
44+
let message: string = getStatusMessage(StatusCodes.INTERNAL_SERVER_ERROR);
3845
let errorDetails: string | object | null | undefined;
3946
let stack: string | undefined;
4047

41-
4248
if (err instanceof Error) {
43-
if (isCustomAPIError(err)) {
49+
if (isCustomAPIError(err)) {
4450
const customErr = err as CustomAPIError;
4551
statusCode = customErr.statusCode || statusCode;
4652
message = customErr.message.trim() || message;
@@ -53,6 +59,7 @@ export const globalErrorHandler = (
5359

5460
const errorResponse: ErrorResponse = {
5561
success: false,
62+
status: statusCode,
5663
message,
5764
};
5865

@@ -61,31 +68,36 @@ export const globalErrorHandler = (
6168
}
6269

6370
if (stack && errorOptions.showStack) {
64-
errorResponse.stack = stack.split('\n').map((line) => line.trim());;
71+
errorResponse.stack = stack.split('\n').map((line) => line.trim());
6572
}
6673

6774
// Log the error if configured to do so
6875
if (errorOptions.logError) {
69-
console.error(boldRed('🔴 Error Message:'));
70-
console.error(red(errorResponse.message));
76+
console.error(
77+
`${boldRed('🔴 Error Status:')} ${red(String(errorResponse.status))}`
78+
);
79+
console.error(
80+
`${boldRed('🔴 Error Message:')} ${red(String(errorResponse.message))}`
81+
);
82+
7183

72-
if (errorResponse.errorDetails) {
73-
console.error(boldYellow('🟡 Error Details:'));
74-
console.error(
75-
yellow(
76-
typeof errorResponse.errorDetails === 'object'
77-
? JSON.stringify(errorResponse.errorDetails, null, 2)
78-
: errorResponse.errorDetails
79-
)
80-
);
81-
}
84+
if (errorResponse.errorDetails) {
85+
console.error(boldYellow('🟡 Error Details:'));
86+
console.error(
87+
yellow(
88+
typeof errorResponse.errorDetails === 'object'
89+
? JSON.stringify(errorResponse.errorDetails, null, 2)
90+
: errorResponse.errorDetails
91+
)
92+
);
93+
}
8294

83-
if (errorResponse.stack) {
84-
console.error(boldGreen('🟢 Stack Trace:'));
85-
(errorResponse.stack as string[]).forEach((line) =>
86-
console.error(green(line))
87-
);
88-
}
95+
if (errorResponse.stack) {
96+
console.error(boldGreen('🟢 Stack Trace:'));
97+
(errorResponse.stack as string[]).forEach((line) =>
98+
console.error(green(line))
99+
);
100+
}
89101
}
90102

91103
res.status(statusCode).json(errorResponse);

0 commit comments

Comments
 (0)