Skip to content

Commit 491a632

Browse files
committed
not-all-exceptions-can-be-augmented
1 parent 4ed3566 commit 491a632

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/composition.ts

+18-16
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,34 @@ const pipeWithoutStack = <Fs extends Func[]>(
4343
Fs
4444
>;
4545

46-
const augmentException = (codeLocation: string) => (e: Error) => {
47-
e.message = (e.message ? (e.message + "\n") : "") + codeLocation;
48-
return e;
46+
// deno-lint-ignore no-explicit-any
47+
const augmentAndRethrowException = (location: string) => (e: any) => {
48+
if (e === undefined) {
49+
console.error(`undefined error within ${location}`);
50+
throw e;
51+
}
52+
try {
53+
e.message = (e.message ? (e.message + "\n") : "") + location;
54+
} catch (augmentError) {
55+
console.error(
56+
`error within ${location}, gamla could not augment error stack`,
57+
augmentError,
58+
e,
59+
);
60+
}
61+
throw e;
4962
};
5063

5164
export const errorBoundry = <F extends Func>(f: F) => {
5265
const location = currentLocation(4);
53-
const augment = augmentException(location);
5466
return ((...x) => {
5567
try {
5668
const result = f(...x);
5769
return (isPromise(result))
58-
? result.catch((e) => {
59-
if (e === undefined) {
60-
console.error(`undefined error within ${location}`);
61-
throw e;
62-
}
63-
throw augment(e);
64-
})
70+
? result.catch(augmentAndRethrowException(location))
6571
: result;
6672
} catch (e) {
67-
if (e === undefined) {
68-
console.error(`undefined error within ${location}`);
69-
throw e;
70-
}
71-
throw augment(e as Error);
73+
augmentAndRethrowException(location)(e);
7274
}
7375
}) as F;
7476
};

0 commit comments

Comments
 (0)