Skip to content

Commit 39c80f3

Browse files
committed
Remove module state
1 parent 72da001 commit 39c80f3

4 files changed

Lines changed: 27 additions & 71 deletions

File tree

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This package enhances typescript error handling, meaning easier troubleshooting
66
- Customizable stack traces
77
- Set stack trace limit
88
- Set stack trace to "Just my Code"
9-
- Provides a overridable global error handler
9+
- Sets global uncaught global error handlers
1010

1111
## Setup
1212

@@ -17,7 +17,7 @@ This package enhances typescript error handling, meaning easier troubleshooting
1717
**Usage**
1818

1919
```typescript
20-
import { setupErrorHandling, handler } from 'ts-error-handler';
20+
import { setupErrorHandling } from 'ts-error-handler';
2121

2222
// Setup error handling options
2323
setupErrorHandling({
@@ -26,34 +26,31 @@ setupErrorHandling({
2626
})
2727

2828
// Example:
29-
try {
30-
// Throw an error
31-
throw new Error('Help!');
32-
}
33-
catch (e) {
34-
// Send the error to the handler
35-
handler(e);
36-
}
29+
throw new Error('Help!');
3730
```
3831

3932
**With ts-async-bootstrap**
4033

4134
```typescript
4235
import { bootstrap } from 'ts-async-bootstrap';
43-
import { setupErrorHandling, handler } from 'ts-error-handler';
36+
import { setupErrorHandling } from 'ts-error-handler';
4437

4538
import { register, main } from './somewhere-else';
4639

40+
function errorHandler(e: Error): void {
41+
console.error('Error!', e);
42+
}
43+
4744
// Setup error handling
4845
setupErrorHandling({
49-
handler: (e) => console.error('Error!', e)
46+
handler: errorHandler
5047
});
5148

5249
// Pass handler from ts-error-handler to bootstrap()
5350
bootstrap({
5451
register: register,
5552
run: main,
56-
errorHandler: handler
53+
errorHandler: errorHandler
5754
});
5855
```
5956

@@ -75,3 +72,7 @@ Set to true to show node-modules in stack traces when justMyCode is on
7572
**justMyCodeIncludeInternals**
7673

7774
Set to true to show node-internals in stack traces when justMyCode is on
75+
76+
**handler**
77+
78+
Global error handler for uncaught exceptions/promise-rejections. Default is `console.error`

src/handler-function.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* A handler function takes an Error and returns void
3+
*/
4+
export type HandlerFunction = (e: Error) => void;

src/index.ts

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,7 @@ if (!process[Symbol.for('ts-node.register.instance')]) {
88
});
99
}
1010

11-
/**
12-
* Global error handler
13-
*/
14-
15-
/**
16-
* A handler function takes an Error and returns void
17-
*/
18-
export type HandlerFunction = (e: Error) => void;
19-
20-
/**
21-
* Default error handler
22-
*
23-
* @param e
24-
*/
25-
let _handler: HandlerFunction = (e: Error) => {
26-
// eslint-disable-next-line no-console
27-
console.error('Unhandled', e);
28-
process.exit(1);
29-
};
30-
31-
export const handler: HandlerFunction = (e: Error) => _handler(e);
32-
33-
export function setHandler(f: HandlerFunction): void {
34-
_handler = f;
35-
}
36-
37-
/**
38-
* Register global handler for uncaught errors
39-
*/
40-
process.on('uncaughtException', handler);
41-
process.on('unhandledRejection', handler);
11+
import { HandlerFunction } from 'handler-function';
4212

4313
/**
4414
* Options for setupErrorHandling
@@ -68,6 +38,7 @@ export const defaultOptions: ErrorHandlerOptions = {
6838
justMyCode: true,
6939
justMyCodeIncludeNodeModules: false,
7040
justMyCodeIncludeInternals: false,
41+
handler: console.error
7142
};
7243

7344
/**
@@ -78,6 +49,12 @@ export const defaultOptions: ErrorHandlerOptions = {
7849
export function setupErrorHandling(options: Partial<ErrorHandlerOptions>) {
7950
options = { ...defaultOptions, ...options };
8051

52+
/**
53+
* Register global handler for uncaught errors
54+
*/
55+
process.on('uncaughtException', options.handler);
56+
process.on('unhandledRejection', options.handler);
57+
8158
/**
8259
* Stack traces
8360
*/
@@ -107,8 +84,4 @@ export function setupErrorHandling(options: Partial<ErrorHandlerOptions>) {
10784
.join('\n');
10885
};
10986
}
110-
111-
if (options.handler) {
112-
setHandler(options.handler);
113-
}
11487
}

test/spec/index.spec.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'jasmine';
2-
import { handler, setHandler, setupErrorHandling } from '../../src';
2+
import { setupErrorHandling } from '../../src';
33

44
import { getStackTrace } from '../util/get-stack-trace';
55

@@ -15,26 +15,4 @@ describe('index', () => {
1515
expect(trace).not.toContain('node_modules');
1616
expect(trace).not.toContain('internal/');
1717
});
18-
19-
it('allows setting an error handler', () => {
20-
let err = null;
21-
22-
setHandler(e => err = e);
23-
handler(new Error('test error'));
24-
25-
expect(err.message).toBe('test error');
26-
});
27-
28-
it('allows setting handler function from setup', () => {
29-
const spy = { on: () => {} };
30-
spyOn(spy, 'on');
31-
32-
setupErrorHandling({
33-
handler: spy.on
34-
});
35-
36-
handler(new Error());
37-
38-
expect(spy.on).toHaveBeenCalled();
39-
});
4018
});

0 commit comments

Comments
 (0)