fix(post card): clarify full error copying

This commit is contained in:
Tommaso Casaburi
2026-04-22 13:44:55 +07:00
parent fb19ee47c8
commit be13311834
5 changed files with 313 additions and 81 deletions
+52 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { formatErrorForDisplay } from '../error-utils';
import { formatErrorForDisplay, serializeErrorForClipboard } from '../error-utils';
describe('error utils', () => {
it('returns plain string errors unchanged', () => {
@@ -29,4 +29,55 @@ describe('error utils', () => {
}),
).toBe('publish failed: provider: plebpubsub; reason: timeout');
});
it('formats nested Error details without dropping the nested message', () => {
expect(
formatErrorForDisplay({
details: {
reason: new Error('provider timeout'),
},
message: 'publish failed',
}),
).toBe('publish failed: reason: provider timeout');
});
it('formats cyclic errors without recursing forever', () => {
const error = Object.assign(new Error('publish failed'), {
details: {
elapsedMs: BigInt(5000),
},
});
Object.assign(error.details, { self: error.details });
expect(formatErrorForDisplay(error)).toBe('publish failed: elapsedMs: 5000; self: [Circular]');
});
it('serializes cyclic errors as valid JSON for copying', () => {
const cause = new Error('provider timeout');
const error = Object.assign(new Error('publish failed'), {
attempts: [{ elapsedMs: BigInt(5000), provider: 'pubsub', reason: cause }],
});
Object.assign(error, { self: error });
expect(JSON.parse(serializeErrorForClipboard(error))).toMatchObject({
attempts: [
{
elapsedMs: '5000',
provider: 'pubsub',
reason: {
message: 'provider timeout',
name: 'Error',
},
},
],
message: 'publish failed',
name: 'Error',
self: '[Circular]',
});
});
it('keeps string errors unchanged and empty objects as JSON', () => {
expect(serializeErrorForClipboard('plain failure')).toBe('plain failure');
expect(serializeErrorForClipboard({})).toBe('{}');
});
});