Skip to content

Commit 91c4516

Browse files
committed
Link standard documentation from README
1 parent fadded3 commit 91c4516

1 file changed

Lines changed: 60 additions & 25 deletions

File tree

‎README.md‎

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# [![eliware.org](https://eliware.org/logos/brand.png)](https://discord.gg/M6aTR9eTwN)
22

3+
Documentation: [docs](docs/README.md) · [specifications](specs/README.md) · [examples](examples/README.md)
4+
35
## @eliware/errors [![npm version](https://img.shields.io/npm/v/@eliware/errors.svg)](https://www.npmjs.com/package/@eliware/errors)[![license](https://img.shields.io/github/license/eliware/errors.svg)](LICENSE)[![build status](https://github.com/eliware/errors/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eliware/errors/actions)
46

57
> Minimal ESM-only Node.js process-level handler for uncaught exceptions, unhandled rejections, and warnings.
@@ -81,11 +83,51 @@ Registers process-level exception handlers. Returns an object with a `removeHand
8183
- `events`: Supported event names to register (default: all three)
8284
- `once`: Use one-shot listeners when supported
8385
- `signal`: AbortSignal that automatically removes handlers
84-
- **Returns:** `{ removeHandlers: () => void, removed: boolean }`; call it to detach the selected handlers. `processObj` must provide `on` plus `off` or `removeListener`.
86+
- **Returns:** `{ removeHandlers: () => void, removed: boolean }`; call it to detach the selected handlers. `removed` is runtime-mutated from `false` to `true` after successful cleanup and should be treated as read-only by consumers.
87+
- Each event is removed once.
88+
- If an event or abort-listener removal throws, successful removals are not retried; a later call retries only failed removals.
89+
- A failed event cleanup leaves `removed` set to `false`; inspect the thrown error and retry `removeHandlers()` after correcting the target.
90+
- `processObj` methods are called with `processObj` as their receiver. The target must provide callable `on` plus `off` or `removeListener`; `once` is optional and is used only when `once: true`. Synchronous callbacks from custom `once()` or `addEventListener()` implementations are supported; cleanup is reconciled after setup completes.
91+
- `log` must provide callable `error`, `warn`, and `debug` methods; malformed loggers are rejected during registration.
92+
93+
Registration is idempotent per process-like object; repeated calls return the existing registration. Cleanup is safe to call repeatedly and removes the registration from the internal registry. The returned `removed` property becomes `true` after successful cleanup, including one-shot and AbortSignal cleanup. Existing listeners are preserved. For production use, remember that handling `uncaughtException` can leave the process in an unsafe state; log it and shut down gracefully when appropriate.
94+
95+
Idempotence uses first-registration-wins semantics: options supplied by a later
96+
call are not applied to an existing registration. Call `removeHandlers()`
97+
before registering again with different options.
98+
This applies to later `events`, `log`, `once`, and `signal` options; they are
99+
ignored until the existing registration is removed.
100+
101+
The `signal` option removes handlers when the signal aborts; retain the
102+
registration or controller when explicit lifecycle control is needed:
103+
104+
```js
105+
const controller = new AbortController();
106+
const registration = registerHandlers({ signal: controller.signal });
107+
controller.abort();
108+
console.log(registration.removed); // true
109+
```
85110

86-
Registration is idempotent per process-like object; repeated calls return the existing registration. Cleanup is safe to call repeatedly and removes the registration from the internal registry. Existing listeners are preserved. For production use, remember that handling `uncaughtException` can leave the process in an unsafe state; log it and shut down gracefully when appropriate.
111+
For graceful shutdown, keep the registration and remove it from the shutdown
112+
hook:
87113

88-
Logger failures and listener-registration/removal failures are propagated to the caller. If registration fails partway through, listeners already added by this package are rolled back.
114+
```js
115+
const registration = registerHandlers();
116+
process.once('SIGTERM', () => registration.removeHandlers());
117+
```
118+
119+
Logger failures and listener-registration/removal failures are propagated to the caller. If registration fails partway through, listeners already added by this package are rolled back. Event-handler logger failures also propagate; custom loggers should handle their own transport failures so they do not obscure the original process event.
120+
If rollback itself fails during setup, the original error remains the thrown error
121+
and the cleanup failure is available as `error.rollbackError`; if one-shot handler
122+
cleanup fails after a logger error, it is available as `error.cleanupError`.
123+
These secondary properties are best-effort and are unavailable when the primary
124+
thrown value is primitive or non-extensible.
125+
When rollback fails, the registration remains in the internal registry so the
126+
returned handle can be retried; later registration calls reuse that handle.
127+
Because setup throws before returning its normal handle, the recovery handle is
128+
also available as `error.registration` when the thrown error is extensible.
129+
If abort-listener removal fails, the same retry rule applies: `removed` remains
130+
`false` and a later `removeHandlers()` retries the failed signal cleanup.
89131

90132
## Configuration
91133

@@ -100,31 +142,16 @@ provide `error(message, metadata)`, `warn(message, metadata)`, and
100142
Type definitions are included:
101143

102144
```ts
103-
export declare function registerHandlers(
104-
options?: {
105-
processObj?: ProcessLike;
106-
log?: typeof import('@eliware/log');
107-
events?: ProcessEvent[];
108-
once?: boolean;
109-
signal?: AbortSignal;
110-
}
111-
): { removeHandlers: () => void; readonly removed: boolean };
112-
113-
interface ProcessLike {
114-
on(event: string, listener: (...args: unknown[]) => void): unknown;
115-
once?(event: string, listener: (...args: unknown[]) => void): unknown;
116-
off?(event: string, listener: (...args: unknown[]) => void): unknown;
117-
removeListener?(event: string, listener: (...args: unknown[]) => void): unknown;
118-
}
119-
120-
type ProcessEvent = 'uncaughtException' | 'unhandledRejection' | 'warning';
121-
122-
export default registerHandlers;
145+
import registerHandlers, { type RegisteredHandlers } from '@eliware/errors';
146+
147+
const registration: RegisteredHandlers = registerHandlers({ events: ['warning'] });
123148
```
124149

150+
`removeHandlers()` returns `void` when cleanup succeeds and throws if the process-like object rejects a removal; call it again to retry failed removals.
151+
125152
## Errors / Troubleshooting
126153

127-
The package only registers handlers; it does not terminate or restart the process. After an uncaught exception, log the failure and shut down gracefully when appropriate. Use a process-like object and custom logger in tests.
154+
The package only registers handlers; it does not terminate or restart the process. After an uncaught exception, log the failure and shut down gracefully when appropriate. Because logger failures propagate from event handlers, production loggers should be reliable and applications should have a shutdown path that does not depend on logging succeeding. Use a process-like object and custom logger in tests.
128155

129156
## Development
130157

@@ -138,7 +165,11 @@ npm run pack
138165

139166
## Security
140167

141-
Do not expose sensitive exception or rejection data through custom loggers. Review logger configuration and redact secrets before logging process errors.
168+
The default logger receives sanitized error metadata (`{ name: 'Error', type:
169+
'Error' }`), warning type metadata, and only the rejection reason type. Custom
170+
loggers control any additional metadata and redaction and can bypass the
171+
library's sanitized defaults. Do not pass raw event values or secrets through
172+
custom logger configuration.
142173

143174
## Operations
144175

@@ -148,6 +179,10 @@ continuing after an uncaught exception may leave application state unsafe.
148179

149180
## Validation
150181

182+
Run all of these checks locally before committing; they are the package's
183+
recommended validation suite. Release automation should provide audit and
184+
package-artifact evidence separately:
185+
151186
```bash
152187
npm test
153188
npm run lint

0 commit comments

Comments
 (0)