From 411c053dc4b314b2bc0d58d3899fd796ad0054c2 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 28 Sep 2023 14:11:02 -0300 Subject: data, util: use typeAppearance in more places --- src/data/things/composite.js | 30 +++++++++++++++--------------- src/data/things/validators.js | 6 +++--- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'src/data') diff --git a/src/data/things/composite.js b/src/data/things/composite.js index 33f49e9b..b6009525 100644 --- a/src/data/things/composite.js +++ b/src/data/things/composite.js @@ -395,7 +395,7 @@ function isInputToken(token) { function getInputTokenShape(token) { if (!isInputToken(token)) { - throw new TypeError(`Expected an input token, got ${token}`); + throw new TypeError(`Expected an input token, got ${typeAppearance(token)}`); } if (typeof token === 'object') { @@ -407,7 +407,7 @@ function getInputTokenShape(token) { function getInputTokenValue(token) { if (!isInputToken(token)) { - throw new TypeError(`Expected an input token, got ${token}`); + throw new TypeError(`Expected an input token, got ${typeAppearance(token)}`); } if (typeof token === 'object') { @@ -464,8 +464,8 @@ function validateInputValue(value, description) { } else { throw new TypeError( (type - ? `Expected ${type}, got ${value}` - : `Expected value, got ${value}`)); + ? `Expected ${type}, got ${typeAppearance(value)}` + : `Expected value, got ${typeAppearance(value)}`)); } } @@ -478,7 +478,7 @@ function validateInputValue(value, description) { : typeof value); if (typeofValue !== type) { - throw new TypeError(`Expected ${type}, got ${typeofValue}`); + throw new TypeError(`Expected ${type}, got ${typeAppearance(value)}`); } } @@ -503,11 +503,11 @@ export function templateCompositeFrom(description) { validateInputs: if ('inputs' in description) { - if (Array.isArray(description.inputs)) { - push(new Error(`Expected inputs to be object, got array`)); - break validateInputs; - } else if (typeof description.inputs !== 'object') { - push(new Error(`Expected inputs to be object, got ${typeof description.inputs}`)); + if ( + Array.isArray(description.inputs) || + typeof description.inputs !== 'object' + ) { + push(new Error(`Expected inputs to be object, got ${typeAppearance(description.inputs)}`)); break validateInputs; } @@ -543,7 +543,7 @@ export function templateCompositeFrom(description) { !Array.isArray(description.outputs) && typeof description.outputs !== 'function' ) { - push(new Error(`Expected outputs to be array or function, got ${typeof description.outputs}`)); + push(new Error(`Expected outputs to be array or function, got ${typeAppearance(description.outputs)}`)); break validateOutputs; } @@ -552,7 +552,7 @@ export function templateCompositeFrom(description) { description.outputs, decorateErrorWithIndex(value => { if (typeof value !== 'string') { - throw new Error(`${value}: Expected string, got ${typeof value}`) + throw new Error(`${value}: Expected string, got ${typeAppearance(value)}`) } else if (!value.startsWith('#')) { throw new Error(`${value}: Expected "#" at start`); } @@ -733,8 +733,8 @@ export function templateCompositeFrom(description) { } for (const name of wrongTypeOutputNames) { - const type = typeof providedOptions[name]; - push(new Error(`${name}: Expected string, got ${type}`)); + const appearance = typeAppearance(providedOptions[name]); + push(new Error(`${name}: Expected string, got ${appearance}`)); } }); @@ -865,7 +865,7 @@ export function compositeFrom(description) { if (!Array.isArray(description.steps)) { throw new TypeError( - `Expected steps to be array, got ${typeof description.steps}` + + `Expected steps to be array, got ${typeAppearance(description.steps)}` + (annotation ? ` (${annotation})` : '')); } diff --git a/src/data/things/validators.js b/src/data/things/validators.js index 048f7ebb..ba62fb84 100644 --- a/src/data/things/validators.js +++ b/src/data/things/validators.js @@ -1,7 +1,7 @@ import {inspect as nodeInspect} from 'node:util'; import {colors, ENABLE_COLOR} from '#cli'; -import {empty, withAggregate} from '#sugar'; +import {empty, typeAppearance, withAggregate} from '#sugar'; function inspect(value) { return nodeInspect(value, {colors: ENABLE_COLOR}); @@ -15,7 +15,7 @@ export function a(noun) { export function isType(value, type) { if (typeof value !== type) - throw new TypeError(`Expected ${a(type)}, got ${typeof value}`); + throw new TypeError(`Expected ${a(type)}, got ${typeAppearance(value)}`); return true; } @@ -132,7 +132,7 @@ export function isObject(value) { export function isArray(value) { if (typeof value !== 'object' || value === null || !Array.isArray(value)) - throw new TypeError(`Expected an array, got ${value}`); + throw new TypeError(`Expected an array, got ${typeAppearance(value)}`); return true; } -- cgit 1.3.0-6-gf8a5