Skip to content

Commit 2bfdcb5

Browse files
committed
refactor: simplify raise() to always-throw, remove setErrorHandling
- raise() now returns 'never' — all errors always throw (data integrity) - Remove setErrorHandling/getErrorHandling/ErrorHandlingMode (global mutable state risk) - Remove all fallback returns after raise() (unreachable with never type) - Rationale: silent data corruption worse than crash for data structures - 2602 tests, 83 suites, 0 failures
1 parent d495f67 commit 2bfdcb5

13 files changed

Lines changed: 32 additions & 238 deletions

File tree

docs-site-docusaurus/docs/guide/guides.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -588,23 +588,6 @@ const result = tree
588588
.reduce((sum, v) => sum + (v ?? 0), 0);
589589
```
590590

591-
### 5. Configure Error Handling
592-
593-
```typescript
594-
import { setErrorHandling, getErrorHandling } from 'data-structure-typed';
595-
596-
// Default: throws errors
597-
setErrorHandling('throw');
598-
599-
// In production: log instead of crash
600-
setErrorHandling('warn'); // console.warn
601-
setErrorHandling('error'); // console.error
602-
setErrorHandling('silent'); // suppress entirely
603-
604-
// Check current mode
605-
console.log(getErrorHandling()); // 'silent'
606-
```
607-
608591
---
609592

610593
**Need more?** Check [INTEGRATIONS.md](/guide/integrations.md) for framework examples.

docs/rfcs/order-statistic-tree.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -283,25 +283,12 @@ Called after every structural change (rotation, insert fixup, delete fixup).
283283

284284
## Error Handling
285285

286-
Uses the library's centralized error handling system (`raise` from `src/common/error.ts`).
287-
288-
### Global error handling modes
289-
290-
```ts
291-
import { setErrorHandling } from 'data-structure-typed';
292-
293-
setErrorHandling('throw'); // default — fail-fast
294-
setErrorHandling('warn'); // console.warn and continue
295-
setErrorHandling('error'); // console.error and continue
296-
setErrorHandling('silent');// suppress all
297-
```
298-
299-
### Usage in order-statistic methods
286+
Uses the library's centralized `raise()` from `src/common/error.ts`. All errors always throw (data structure errors are never recoverable — silent failures cause data corruption).
300287

301288
```ts
302289
select(k: number): K | undefined {
303290
if (!this._enableOrderStatistic) {
304-
raise(Error, ERR.orderStatisticNotEnabled('select'));
291+
raise(Error, ERR.orderStatisticNotEnabled('select')); // always throws
305292
return undefined; // reached in warn/error/silent modes
306293
}
307294
// ...

src/common/error.ts

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,15 @@
11
/**
2-
* Error handling mode for the library.
3-
* - 'throw': Throw errors (default, fail-fast)
4-
* - 'warn': console.warn and continue
5-
* - 'error': console.error and continue
6-
* - 'silent': Suppress all errors
7-
*/
8-
export type ErrorHandlingMode = 'throw' | 'warn' | 'error' | 'silent';
9-
10-
let _errorHandlingMode: ErrorHandlingMode = 'throw';
11-
12-
/**
13-
* Set the global error handling mode.
14-
* @param mode - The error handling mode to use.
15-
*/
16-
export function setErrorHandling(mode: ErrorHandlingMode): void {
17-
_errorHandlingMode = mode;
18-
}
19-
20-
/**
21-
* Get the current error handling mode.
22-
*/
23-
export function getErrorHandling(): ErrorHandlingMode {
24-
return _errorHandlingMode;
25-
}
26-
27-
/**
28-
* Raise an error through the configured error handling mode.
29-
* In 'throw' mode, throws the error. In other modes, logs and continues.
2+
* Centralized error dispatch.
3+
* All library errors go through this function for consistent messaging and easy grep.
4+
* @remarks Always throws — data structure errors are never recoverable.
305
* @param ErrorClass - The error constructor (Error, TypeError, RangeError, etc.)
316
* @param message - The error message.
327
*/
338
export function raise(
349
ErrorClass: new (msg: string) => Error,
3510
message: string
36-
): void {
37-
switch (_errorHandlingMode) {
38-
case 'throw':
39-
throw new ErrorClass(message);
40-
case 'warn':
41-
console.warn(`[data-structure-typed] ${message}`);
42-
break;
43-
case 'error':
44-
console.error(`[data-structure-typed] ${message}`);
45-
break;
46-
case 'silent':
47-
break;
48-
}
11+
): never {
12+
throw new ErrorClass(message);
4913
}
5014

5115
/**

src/common/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
export { ERR, raise, setErrorHandling, getErrorHandling } from './error';
2-
export type { ErrorHandlingMode } from './error';
1+
export { ERR, raise } from './error';
32

43
export enum DFSOperation {
54
VISIT = 0,

src/data-structures/binary-tree/bst.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
12171217
): K | undefined | ReturnType<C> | undefined {
12181218
if (!this._enableOrderStatistic) {
12191219
raise(Error, ERR.orderStatisticNotEnabled('select'));
1220-
return undefined;
12211220
}
12221221
if (k < 0 || k >= this._size) return undefined;
12231222

@@ -1290,7 +1289,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
12901289
): number {
12911290
if (!this._enableOrderStatistic) {
12921291
raise(Error, ERR.orderStatisticNotEnabled('rank'));
1293-
return -1;
12941292
}
12951293
if (!this._root || this._size === 0) return -1;
12961294

@@ -1357,7 +1355,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
13571355
): (K | undefined)[] | ReturnType<C>[] {
13581356
if (!this._enableOrderStatistic) {
13591357
raise(Error, ERR.orderStatisticNotEnabled('rangeByRank'));
1360-
return [];
13611358
}
13621359
if (this._size === 0) return [];
13631360

src/data-structures/binary-tree/tree-map.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
6565
// Validate entries like native Map: each item must be a 2-tuple-like value.
6666
if (!Array.isArray(item) || item.length < 2) {
6767
raise(TypeError, ERR.invalidEntry('TreeMap'));
68-
continue;
6968
}
7069
k = item[0] as K;
7170
v = item[1] as V | undefined;
@@ -108,7 +107,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
108107
}
109108

110109
raise(TypeError, ERR.comparatorRequired('TreeMap'));
111-
return 0;
112110
};
113111
}
114112

src/data-structures/binary-tree/tree-set.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
9292
}
9393

9494
raise(TypeError, ERR.comparatorRequired('TreeSet'));
95-
return 0;
9695
};
9796
}
9897

src/data-structures/graph/abstract-graph.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ export abstract class AbstractGraph<
276276
return this._addEdge(newEdge);
277277
} else {
278278
raise(TypeError, ERR.invalidArgument('dest must be a Vertex or vertex key when srcOrEdge is an Edge.', 'Graph'));
279-
return false;
280279
}
281280
}
282281
}

src/data-structures/hash/hash-map.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,6 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
953953
return rawElement;
954954
}
955955
raise(TypeError, ERR.invalidArgument('If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.', 'HashMap'));
956-
return rawElement as unknown as [K, V];
957956
};
958957

959958
get toEntryFn() {

src/data-structures/linked-list/skip-linked-list.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ export class SkipList<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
102102
return a < b ? -1 : a > b ? 1 : 0;
103103
}
104104
raise(TypeError, ERR.comparatorRequired('SkipList'));
105-
return 0;
106105
};
107106
}
108107

0 commit comments

Comments
 (0)