Skip to content

Commit

Permalink
fix: error ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
cha0s committed Dec 2, 2024
1 parent 36d0515 commit d9e373d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/codecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ export const Aliases = {
export const Codecs = {};

export function resolveCodec(blueprint) {
if (!blueprint) {
throw new TypeError('No blueprint specified.');
}
let {type} = blueprint;
if (undefined === type) {
throw new TypeError("No codec specified. Did you forget to include a 'type' key in your schema blueprint?");
try {
resolveCodec({type: blueprint});
}
catch (error) {
throw new TypeError("No codec specified. Did you forget to include a 'type' key in your schema blueprint?");
}
throw new TypeError(`Blueprint '${blueprint}' looks like a type. Try {type: '${blueprint}'}`);
}
const searched = new Set([type]);
let Codec = Codecs[type];
Expand Down
10 changes: 10 additions & 0 deletions src/codecs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ test('alias cycle', async () => {
Aliases.bar = 'foofoo';
expect(() => resolveCodec({type: 'foofoo'})).toThrowError();
});

test('no blueprint', async () => {
expect(() => resolveCodec()).toThrowError();
});

test('suggestion', async () => {
expect(() => resolveCodec('bool')).toThrowError(
"Blueprint 'bool' looks like a type. Try {type: 'bool'}",
);
});

0 comments on commit d9e373d

Please sign in to comment.