fix: error ergonomics

This commit is contained in:
cha0s 2024-12-02 04:07:03 -06:00
parent 36d05154fe
commit d9e373d957
2 changed files with 20 additions and 1 deletions

View File

@ -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];

View File

@ -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'}",
);
});