Skip to content

Commit 35a9499

Browse files
committed
readme file updated and log error and stack showing condition changed
1 parent 470fd1d commit 35a9499

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,19 @@ By default, it includes stack trace and logs the error in development (`NODE_ENV
132132

133133
### 5. **Set Options Globally (Optional)**
134134

135-
You can configure the toolkit behavior (e.g., hide stack traces and disable console logging even in development):
135+
You can configure the error handling behavior (e.g., hide stack traces and disable console logging even in development) using either:
136+
137+
```.env
138+
SHOW_STACK=false
139+
LOG_ERROR=false
140+
```
141+
142+
or directly in your code:
136143

137144
```ts
138-
import { setToolkitOptions } from 'express-error-toolkit';
145+
import { setErrorOptions } from 'express-error-toolkit';
139146

140-
setToolkitOptions({
147+
setErrorOptions({
141148
showStack: false,
142149
logError: false
143150
});

src/global-error-handler.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import { StatusCodes, getStatusMessage } from 'http-status-toolkit';
33
import { isCustomAPIError } from './checking-custom-api-error';
44
import { CustomAPIError } from './error';
55

6+
7+
68
// Internal config object (optional override)
79
let errorOptions = {
8-
showStack: process.env.SHOW_STACK !== 'false',
9-
logError: process.env.LOG_ERROR !== 'false',
10+
showStack: process.env.SHOW_STACK !== 'false' && process.env.NODE_ENV !== 'production',
11+
logError:
12+
process.env.LOG_ERROR !== 'false' && process.env.NODE_ENV !== 'production',
1013
};
1114

1215
export function setErrorOptions(
@@ -28,18 +31,16 @@ export interface ErrorResponse {
2831

2932
export const globalErrorHandler = (
3033
err: unknown,
31-
req: Request,
34+
_req: Request,
3235
res: Response,
33-
next: NextFunction
36+
_next: NextFunction
3437
) => {
3538
let statusCode: number = StatusCodes.INTERNAL_SERVER_ERROR;
3639
let message = getStatusMessage(StatusCodes.INTERNAL_SERVER_ERROR);
3740
let errorDetails: string | object | null | undefined;
3841
let stack: string | undefined;
3942

4043

41-
const isDev = process.env.NODE_ENV?.trim() === 'development';
42-
4344
if (err instanceof Error) {
4445
if (isCustomAPIError(err)) {
4546
const customErr = err as CustomAPIError;
@@ -61,12 +62,19 @@ export const globalErrorHandler = (
6162
errorResponse.errorDetails = errorDetails;
6263
}
6364

64-
if (isDev && stack && errorOptions.showStack) {
65+
if (stack && errorOptions.showStack) {
6566
errorResponse.stack = stack.split('\n').map((line) => line.trim());;
6667
}
6768

68-
if (isDev && errorOptions.logError) {
69-
console.error(err);
69+
// Log the error if configured to do so
70+
if (errorOptions.logError) {
71+
console.error('\x1b[35m%s\x1b[31m', 'Error Message:', errorResponse.message);
72+
if (errorResponse.errorDetails) {
73+
console.error('Error Details:', errorResponse.errorDetails);
74+
}
75+
if (errorResponse.stack) {
76+
console.error('\x1b[35m%s\x1b[32m', 'Stack Trace:', errorResponse.stack);
77+
}
7078
}
7179

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

0 commit comments

Comments
 (0)