Skip to content

Commit

Permalink
feat: provide better error trace
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 16, 2023
1 parent 47fba2b commit 832ade1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/utilities/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { configure } from 'safe-stable-stringify';

const safeStringify = configure({
circularValue: 'Magic circle!',
deterministic: false,
strict: false,
});
Expand All @@ -11,7 +10,7 @@ export const stringify = (value: unknown): string => {
return safeStringify(value) ?? '';
} catch (error) {
// eslint-disable-next-line no-console
console.error('could not serialize value', value);
console.error('[roarr] could not serialize value', value);

throw error;
}
Expand Down
49 changes: 48 additions & 1 deletion test/roarr/utilities/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,64 @@
import { stringify } from '../../../src/utilities/stringify';
import test from 'ava';

test('stringifies key=value', (t) => {
test('stringifies key=value (string)', (t) => {
t.is(
stringify({
foo: 'bar',
}),
'{"foo":"bar"}',
);
});

test('stringifies key=value (number)', (t) => {
t.is(
stringify({
foo: 123,
}),
'{"foo":123}',
);
});

test('stringifies key=value (function)', (t) => {
t.is(
stringify({
foo: () => {},
}),
'{}',
);
});

test('stringifies key=value (undefined)', (t) => {
t.is(
stringify({
foo: undefined,
}),
'{}',
);
});

test('stringifies key=value (null)', (t) => {
t.is(
stringify({
foo: null,
}),
'{"foo":null}',
);
});

test('stringifies key=value (Symbol)', (t) => {
t.is(
stringify({
foo: Symbol('bar'),
}),
'{}',
);
});

test('stringifies key=value (circular)', (t) => {
const foo = {};

foo['foo'] = foo;

t.is(stringify(foo), '{"foo":"[Circular]"}');
});

0 comments on commit 832ade1

Please sign in to comment.