fix: error ergonomics
This commit is contained in:
parent
36d05154fe
commit
d9e373d957
|
@ -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];
|
||||
|
|
|
@ -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'}",
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user