Skip to content

Commit b32e602

Browse files
committed
intro line made controllable by user
1 parent 2300296 commit b32e602

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/global-error-handler.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,30 @@ import {
1010
boldGreen,
1111
green,
1212
dimGray,
13+
bold,
1314
} from './utils/console-colors';
1415
import { centerText } from './utils';
16+
import { ErrorOptions, ErrorResponse } from './types';
17+
18+
1519

1620
// Internal config object (optional override)
17-
let errorOptions = {
21+
let errorOptions: ErrorOptions = {
1822
showStack:
1923
process.env.SHOW_STACK !== 'false' && process.env.NODE_ENV !== 'production',
2024
logError:
2125
process.env.LOG_ERROR !== 'false' && process.env.NODE_ENV !== 'production',
26+
introLine: `💥 Even the best code breaks sometimes! Let's debug...`,
2227
};
2328

24-
export function setErrorOptions(options: Partial<typeof errorOptions>) {
29+
export function setErrorOptions(options: Partial<ErrorOptions>) {
2530
errorOptions = {
2631
...errorOptions,
2732
...options,
2833
};
2934
}
3035

31-
export interface ErrorResponse {
32-
success: false;
33-
status: number;
34-
message: string;
35-
errorDetails?: string | object | null;
36-
stack?: string | string[];
37-
}
36+
3837

3938
export const globalErrorHandler = (
4039
err: unknown,
@@ -75,7 +74,10 @@ export const globalErrorHandler = (
7574

7675
// Log the error if configured to do so
7776
if (errorOptions.logError) {
78-
console.error(`\n${centerText(dimGray('Error Log'))}\n`);
77+
78+
if (errorOptions.introLine) {
79+
console.error(`\n${(bold(`${errorOptions.introLine}`))}\n`);
80+
}
7981

8082
// log the error status
8183
console.error(
@@ -117,7 +119,7 @@ export const globalErrorHandler = (
117119
if (remainingCount > 0) {
118120
console.error(
119121
dimGray(
120-
`...and ${remainingCount} more lines. 💡 See full stack in error respons`
122+
`...and ${remainingCount} more lines. 💡 See full stack in error response`
121123
)
122124
);
123125
}

src/types/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface ErrorOptions {
2+
showStack: boolean;
3+
logError: boolean;
4+
introLine: string | boolean;
5+
}
6+
7+
export interface ErrorResponse {
8+
success: false;
9+
status: number;
10+
message: string;
11+
errorDetails?: string | object | null;
12+
stack?: string | string[];
13+
}

src/utils/console-colors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export function green(text: string) : string {
3333
}
3434

3535

36+
export function bold (text: string) : string {
37+
return `${colors.bold}${text}${colors.reset}`;
38+
}
39+
3640
export function dimGray(text: string) : string {
3741
return `${colors.dimGray}${colors.underline}${text}${colors.reset}`;
3842
}

0 commit comments

Comments
 (0)