Skip to content

Commit

Permalink
feat(Serialize): add information to errors for tracing data types
Browse files Browse the repository at this point in the history
  • Loading branch information
RealShadowNova committed Apr 6, 2024
1 parent 5c92ef9 commit e5dc61c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/lib/Serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export namespace Serialize {
return { [Keying.Type]: Type.Set, [Keying.Value]: toJsonCompatibleArray(Array.from(data)) };
}

throw new TypeError(`Serialize received an unknown type while formatting: "${data.constructor.name}".`);
throw new TypeError(`Serialize.toJsonCompatible() received an unknown type while formatting: "${data.constructor.name}".`);
}

function toJsonCompatibleArray(array: unknown[]): JsonCompatible[] {
Expand Down Expand Up @@ -132,7 +132,7 @@ export namespace Serialize {
return undefined;

default:
throw new TypeError('Serialize received an unknown type.');
throw new TypeError(`Serialize.fromJsonCompatible() received an unknown type while parsing: "${value}" (${type}).`);
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/lib/Serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ describe('Serialize', () => {

test('GIVEN unknown type THEN throws error', () => {
expect(() => Serialize.toJsonCompatible(() => true)).toThrowError(
new TypeError('Serialize received an unknown type while formatting: "Function".')
new TypeError('Serialize.toJsonCompatible() received an unknown type while formatting: "Function".')
);
});
});
Expand Down Expand Up @@ -936,7 +936,9 @@ describe('Serialize', () => {

test('GIVEN unknown type THEN throws error', () => {
// @ts-expect-error 2322 - Type 'function' is not assignable to type 'Serialize.Type'.
expect(() => Serialize.fromJsonCompatible({ [Serialize.Keying.Type]: 'function' })).toThrowError('Serialize received an unknown type.');
expect(() => Serialize.fromJsonCompatible({ [Serialize.Keying.Type]: 'function' })).toThrowError(
'Serialize.fromJsonCompatible() received an unknown type while parsing: "undefined" (function).'
);
});
});
});

0 comments on commit e5dc61c

Please sign in to comment.