Skip to content

Commit 74a0e18

Browse files
committed
refactor: share normalizeThrown; drop unsafe error as Error casts
Extract Try.normalizeThrown into src/utils/normalize.ts so transformer error handlers can use it instead of casting unknown → Error. Keep the private wrapper on Try for source compatibility.
1 parent 7b71911 commit 74a0e18

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

src/core/Try.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ValidateKeys,
88
BreadcrumbExtractor as BreadcrumbExtractorType,
99
BreadcrumbExtractorUtil,
10+
normalizeThrown,
1011
} from '../utils';
1112
import { Reporter, NoopReporter } from './reporter';
1213

@@ -788,7 +789,7 @@ export class Try<
788789
if (this.config.debug) {
789790
console.error(e);
790791
}
791-
const error = Try.normalizeThrown(e);
792+
const error = normalizeThrown(e);
792793
this.exec.result = { success: false, error };
793794
return this.exec.result;
794795
})
@@ -809,7 +810,7 @@ export class Try<
809810
if (this.config.debug) {
810811
console.error(e);
811812
}
812-
const error = Try.normalizeThrown(e);
813+
const error = normalizeThrown(e);
813814
this.exec.isAsync = false;
814815
this.exec.result = { success: false, error };
815816
} finally {
@@ -823,10 +824,7 @@ export class Try<
823824
}
824825

825826
private static normalizeThrown(e: unknown): Error {
826-
if (e instanceof Error) return e;
827-
const wrapped = new Error(`Non-Error thrown (${typeof e})`);
828-
wrapped.cause = e;
829-
return wrapped;
827+
return normalizeThrown(e);
830828
}
831829

832830
private runFinallyCallback(): void | Promise<void> {

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
export * from './types';
66
export * from './transformers';
77
export * from './breadcrumbs';
8+
export * from './normalize';

src/utils/normalize.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Normalize any thrown value into a real `Error` instance. If the value is
3+
* already an `Error`, returned unchanged. Otherwise wrapped in a fresh
4+
* `Error` whose `message` records the `typeof` and whose `cause` preserves
5+
* the original value.
6+
*/
7+
export function normalizeThrown(e: unknown): Error {
8+
if (e instanceof Error) return e;
9+
const wrapped = new Error(`Non-Error thrown (${typeof e})`);
10+
wrapped.cause = e;
11+
return wrapped;
12+
}

src/utils/transformers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BreadcrumbTransformer } from './types';
22
import { BreadcrumbTransformationError } from './types';
3+
import { normalizeThrown } from './normalize';
34

45
/**
56
* Predefined transformer functions for common use cases
@@ -61,7 +62,7 @@ export class TransformerRegistry {
6162
return transformer(value);
6263
} catch (error) {
6364
const transformationError = new BreadcrumbTransformationError(
64-
error as Error,
65+
normalizeThrown(error),
6566
'custom',
6667
);
6768

@@ -87,7 +88,7 @@ export class TransformerRegistry {
8788
return transformer(value, paramIndex);
8889
} catch (error) {
8990
const transformationError = new BreadcrumbTransformationError(
90-
error as Error,
91+
normalizeThrown(error),
9192
transformerType,
9293
paramIndex,
9394
);

0 commit comments

Comments
 (0)