Skip to content

Commit 96513ed

Browse files
committed
Preserve stack trace in err()
1 parent 773e120 commit 96513ed

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,4 @@ declare const utils: {
197197
randomPrivateKey: () => Bytes;
198198
precompute: (w?: number, p?: Point) => Point;
199199
};
200-
export { secp256k1_CURVE as CURVE, etc, getPublicKey, getSharedSecret, Point, Point as ProjectivePoint, sign, signAsync, Signature, utils, verify };
200+
export { secp256k1_CURVE as CURVE, etc, getPublicKey, getSharedSecret, Point, Point as ProjectivePoint, sign, signAsync, Signature, utils, verify, };

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ const L2 = 64;
2828
// Helpers and Precomputes sections are reused between libraries
2929
// ## Helpers
3030
// ----------
31-
// error helper, messes-up stack trace
32-
const err = (m = '') => {
33-
throw new Error(m);
31+
const captureTrace = (...args) => {
32+
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function') {
33+
Error.captureStackTrace(...args);
34+
}
35+
};
36+
const err = (message = '') => {
37+
const e = new Error(message);
38+
captureTrace(e, err);
39+
throw e;
3440
};
3541
const isBig = (n) => typeof n === 'bigint'; // is big integer
3642
const isStr = (s) => typeof s === 'string'; // is string
@@ -794,4 +800,4 @@ const wNAF = (n) => {
794800
return { p, f }; // return both real and fake points for JIT
795801
};
796802
// !! Remove the export below to easily use in REPL / browser console
797-
export { secp256k1_CURVE as CURVE, etc, getPublicKey, getSharedSecret, Point, Point as ProjectivePoint, sign, signAsync, Signature, utils, verify };
803+
export { secp256k1_CURVE as CURVE, etc, getPublicKey, getSharedSecret, Point, Point as ProjectivePoint, sign, signAsync, Signature, utils, verify, };

index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ export type WeierstrassOpts<T> = Readonly<{
5252

5353
// ## Helpers
5454
// ----------
55-
// error helper, messes-up stack trace
56-
const err = (m = ''): never => {
57-
throw new Error(m);
55+
const captureTrace = (...args: Parameters<typeof Error.captureStackTrace>): void => {
56+
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function') {
57+
Error.captureStackTrace(...args);
58+
}
59+
};
60+
const err = (message = ''): never => {
61+
const e = new Error(message);
62+
captureTrace(e, err);
63+
throw e;
5864
};
5965
const isBig = (n: unknown): n is bigint => typeof n === 'bigint'; // is big integer
6066
const isStr = (s: unknown): s is string => typeof s === 'string'; // is string

0 commit comments

Comments
 (0)