Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-6348): Wrap thrown errors in JS Error objects with stacks #25

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,19 @@ if (!nativeBinding) {

const { compress, decompress } = nativeBinding;

module.exports.compress = compress;
module.exports.decompress = decompress;
// Error objects created via napi don't have JS stacks; wrap them so .stack is present
// https://github.com/nodejs/node/issues/25318#issuecomment-451068073
module.exports.compress = async function (data) {
durran marked this conversation as resolved.
Show resolved Hide resolved
try {
return await compress(data);
} catch (e) {
throw new Error(`zstd: ${e.message}`);
}
};
module.exports.decompress = async function (data) {
try {
return await decompress(data);
} catch (e) {
throw new Error(`zstd: ${e.message}`);
}
};
13 changes: 13 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,17 @@ describe('zstd', () => {
});
});
});

describe('#decompress', () => {
context('when decompressing invalid data', () => {
it('includes a stack trace', async () => {
try {
await decompress(Buffer.from('invalid'));
} catch (error) {
expect(error.message).to.equal('zstd: Unknown frame descriptor');
expect(error.stack).to.match(/module.exports.decompress/);
}
});
});
});
});
Loading