Skip to content

Commit 953e3a9

Browse files
committed
setErrorOption added to make trace optional in global error handler
1 parent 95a62b3 commit 953e3a9

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
[![npm version](https://img.shields.io/npm/v/express-error-toolkit)](https://www.npmjs.com/package/express-error-toolkit)
44
![typescript](https://badgen.net/badge/icon/typescript?icon=typescript&label)
55
[![license](https://img.shields.io/npm/l/express-error-toolkit)](https://github.com/dev-rashedin/express-error-toolkit/blob/main/LICENSE)
6-
[![GitHub stars](https://img.shields.io/github/stars/dev-rashedin/express-error-toolkit?style=social)](https://github.com/dev-rashedin/express-error-toolkit/stargazers)
76
![minified](https://badgen.net/bundlephobia/min/express-error-toolkit)
87
![minified gzip](https://badgen.net/bundlephobia/minzip/express-error-toolkit)
98

@@ -59,6 +58,7 @@ yarn add express-error-toolkit
5958
pnpm add express-error-toolkit
6059
```
6160

61+
> ⚙️ Requires **Node.js v14 or higher**.
6262
> ℹ️ Make sure you have `express` installed in your project, as this toolkit is built specifically to enhance Express.js error handling.
6363
6464
---
@@ -126,11 +126,28 @@ import { globalErrorHandler } from 'express-error-toolkit';
126126
app.use(globalErrorHandler);
127127
```
128128

129-
In development mode (`NODE_ENV=development`), the error stack trace will be included in the response.
129+
By default, it includes stack trace in development (`NODE_ENV=development`).
130+
131+
132+
---
133+
134+
### 5. **Set Options Globally (Optional)**
135+
136+
You can configure the toolkit behavior (e.g., hide stack traces even in dev):
137+
138+
```ts
139+
import { setToolkitOptions } from 'express-error-toolkit';
140+
141+
setToolkitOptions({
142+
showStack: false,
143+
});
144+
```
145+
146+
This overrides the default behavior (based on `NODE_ENV` or `.env` file).
130147

131148
---
132149

133-
### 5. **httpError()**: Create generic custom errors
150+
### 6. **httpError()**: Create generic custom errors
134151

135152
```ts
136153
import { httpError } from 'express-error-toolkit';
@@ -148,7 +165,7 @@ throw new BadRequestError('Generic client error');
148165

149166
---
150167

151-
### 6. **isCustomAPIError()**: Type guard for checking error type
168+
### 7. **isCustomAPIError()**: Type guard for checking error type
152169

153170
```ts
154171
import { isCustomAPIError } from 'express-error-toolkit';
@@ -160,7 +177,7 @@ if (isCustomAPIError(err)) {
160177

161178
---
162179

163-
### 7. **Bonus**: Use status codes directly (re-exported from http-status-toolkit)
180+
### 8. **Bonus**: Use status codes directly (re-exported from http-status-toolkit)
164181

165182
```ts
166183
import { StatusCodes, getStatusMessage } from 'express-error-toolkit';

src/global-error-handler.ts

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

6+
// Internal config object (optional override)
7+
let errorOptions = {
8+
showStack: process.env.SHOW_STACK !== 'false',
9+
};
10+
11+
export function setErrorOptions(
12+
options: Partial<typeof errorOptions>
13+
) {
14+
errorOptions = {
15+
...errorOptions,
16+
...options,
17+
};
18+
}
19+
20+
621
export interface ErrorResponse {
722
success: false;
823
message: string;
@@ -21,6 +36,9 @@ export const globalErrorHandler = (
2136
let errorDetails: string | object | null | undefined;
2237
let stack: string | undefined;
2338

39+
const isDev = process.env.NODE_ENV?.trim() === 'development';
40+
const showStack = process.env.SHOW_STACK !== 'false';
41+
2442
if (err instanceof Error) {
2543
if (isCustomAPIError(err)) {
2644
const customErr = err as CustomAPIError;
@@ -42,12 +60,10 @@ export const globalErrorHandler = (
4260
errorResponse.errorDetails = errorDetails;
4361
}
4462

45-
if (process.env.NODE_ENV === 'development' && stack) {
46-
errorResponse.stack = stack;
47-
}
63+
// if (process.env.NODE_ENV === 'development' && stack) {
64+
// errorResponse.stack = stack;
65+
// }
4866

49-
const isDev = process.env.NODE_ENV?.trim() === 'development';
50-
const showStack = process.env.SHOW_STACK !== 'false';
5167

5268
if (isDev && stack && showStack) {
5369
errorResponse.stack = stack.split('\n').map((line) => line.trim());;

src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
export {
2-
BadRequestError,
32
NotFoundError,
4-
UnauthenticatedError,
53
ConflictError,
6-
TooManyRequestsError,
74
CustomAPIError,
5+
ForbiddenError,
6+
BadRequestError,
87
ValidationError,
9-
ForbiddenError
8+
UnauthenticatedError,
9+
TooManyRequestsError
1010
} from '../src/error';
1111

1212

13+
export { httpError } from './http-error'
1314
export { asyncHandler } from './async-handler'
14-
export { globalErrorHandler } from './global-error-handler'
1515
export { notFoundHandler } from './not-found-handler'
16-
export {httpError} from './http-error'
16+
export { isCustomAPIError } from './checking-custom-api-error'
17+
export { globalErrorHandler, setErrorOptions } from './global-error-handler';
1718

1819

1920
export { StatusCodes, getStatusMessage } from 'http-status-toolkit'

0 commit comments

Comments
 (0)