Skip to content

Commit d17fe69

Browse files
committed
Format error messages
1 parent 1f3c47b commit d17fe69

File tree

7 files changed

+61
-54
lines changed

7 files changed

+61
-54
lines changed

lib/node_modules/@stdlib/_tools/bundle/pkg-list/lib/validate.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2626
var indexOf = require( '@stdlib/utils/index-of' );
2727
var isArray = require( '@stdlib/assert/is-array' );
2828
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
29+
var format = require( '@stdlib/string/format' );
2930

3031

3132
// VARIABLES //
@@ -66,66 +67,66 @@ var NS = [ 'tree', 'flat', 'none' ];
6667
*/
6768
function validate( opts, options ) {
6869
if ( !isPlainObject( options ) ) {
69-
return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' );
70+
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
7071
}
7172
if ( hasOwnProp( options, 'namespace' ) ) {
7273
opts.namespace = options.namespace;
7374
if ( !isString( opts.namespace ) ) {
74-
return new TypeError( 'invalid option. `namespace` option must be a string. Option: `' + opts.namespace + '`.' );
75+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'namespace', opts.namespace ) );
7576
}
7677
if ( indexOf( NS, opts.namespace ) === -1 ) {
77-
return new RangeError( 'invalid option. `namespace` option must be one of the following values: `['+NS.join( ',' )+']`.' );
78+
return new RangeError( format( 'invalid option. `%s` option must be one of the following: `[%s]`.', 'namespace', NS.join( ',' ) ) );
7879
}
7980
}
8081
if ( hasOwnProp( options, 'bundler' ) ) {
8182
opts.bundler = options.bundler;
8283
if ( !isString( opts.bundler ) ) {
83-
return new TypeError( 'invalid option. `bundler` option must be a string. Option: `' + opts.bundler + '`.' );
84+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'bundler', opts.bundler ) );
8485
}
8586
if ( indexOf( BUNDLERS, opts.bundler ) === -1 ) {
86-
return new RangeError( 'invalid option. `bundler` option must be one of the following values: `['+BUNDLERS.join( ',' )+']`.' );
87+
return new RangeError( format( 'invalid option. `%s` option must be one of the following: `[%s]`. Option: `%s`.', 'bundler', BUNDLERS.join( ',' ), opts.bundler ) );
8788
}
8889
}
8990
if ( hasOwnProp( options, 'requireName' ) ) {
9091
opts.requireName = options.requireName;
9192
if ( !isString( opts.requireName ) ) {
92-
return new TypeError( 'invalid option. `requireName` option must be a string. Option: `' + opts.requireName + '`.' );
93+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'requireName', opts.requireName ) );
9394
}
9495
}
9596
if ( hasOwnProp( options, 'exportName' ) ) {
9697
opts.exportName = options.exportName;
9798
if ( !isString( opts.exportName ) ) {
98-
return new TypeError( 'invalid option. `exportName` option must be a string. Option: `' + opts.exportName + '`.' );
99+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'exportName', opts.exportName ) );
99100
}
100101
}
101102
if ( hasOwnProp( options, 'standalone' ) ) {
102103
opts.standalone = options.standalone;
103104
if ( !isString( opts.standalone ) ) {
104-
return new TypeError( 'invalid option. `standalone` option must be a string. Option: `' + opts.standalone + '`.' );
105+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'standalone', opts.standalone ) );
105106
}
106107
}
107108
if ( hasOwnProp( options, 'transforms' ) ) {
108109
opts.transforms = options.transforms;
109110
if ( !isArray( opts.transforms ) ) {
110-
return new TypeError( 'invalid option. `transforms` option must be an array. Option: `' + opts.transforms + '`.' );
111+
return new TypeError( format( 'invalid option. `%s` option must be an array. Option: `%s`.', 'transforms', opts.transforms ) );
111112
}
112113
}
113114
if ( hasOwnProp( options, 'plugins' ) ) {
114115
opts.plugins = options.plugins;
115116
if ( !isArray( opts.plugins ) ) {
116-
return new TypeError( 'invalid option. `plugins` option must be an array. Option: `' + opts.plugins + '`.' );
117+
return new TypeError( format( 'invalid option. `%s` option must be an array. Option: `%s`.', 'plugins', opts.plugins ) );
117118
}
118119
}
119120
if ( hasOwnProp( options, 'external' ) ) {
120121
opts.external = options.external;
121122
if ( !isStringArray( opts.external ) ) {
122-
return new TypeError( 'invalid option. `external` option must be an array of strings. Option: `' + opts.external + '`.' );
123+
return new TypeError( format( 'invalid option. `%s` option must be an array of strings. Option: `%s`.', 'external', opts.external ) );
123124
}
124125
}
125126
if ( hasOwnProp( options, 'paths' ) ) {
126127
opts.paths = options.paths;
127128
if ( !isStringArray( opts.paths ) ) {
128-
return new TypeError( 'invalid option. `paths` option must be an array of strings. Option: `' + opts.paths + '`.' );
129+
return new TypeError( format( 'invalid option. `%s` option must be an array of strings. Option: `%s`.', 'paths', opts.paths ) );
129130
}
130131
}
131132
return null;

lib/node_modules/@stdlib/_tools/github/create-token/lib/validate.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var isObject = require( '@stdlib/assert/is-object' );
2525
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2626
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
2727
var isURI = require( '@stdlib/assert/is-uri' );
28+
var format = require( '@stdlib/string/format' );
2829

2930

3031
// MAIN //
@@ -48,40 +49,40 @@ var isURI = require( '@stdlib/assert/is-uri' );
4849
*/
4950
function validate( opts, options ) {
5051
if ( !isObject( options ) ) {
51-
return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
52+
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
5253
}
5354
opts.username = options.username;
5455
if ( !isString( opts.username ) ) {
55-
return new TypeError( 'invalid option. `username` option must be a string primitive. Option: `' + opts.username + '`.' );
56+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'username', opts.username ) );
5657
}
5758
opts.password = options.password;
5859
if ( !isString( opts.password ) ) {
59-
return new TypeError( 'invalid option. `password` option must be a string primitive. Option: `' + opts.password + '`.' );
60+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'password', opts.password ) );
6061
}
6162
opts.scopes = options.scopes;
6263
if ( !isStringArray( opts.scopes ) ) {
63-
return new TypeError( 'invalid option. `scopes` option must be a string array. Option: `' + opts.scopes + '`.' );
64+
return new TypeError( format( 'invalid option. `%s` option must be a string array. Option: `%s`.', 'scopes', opts.scopes ) );
6465
}
6566
opts.note = options.note;
6667
if ( !isString( opts.note ) ) {
67-
return new TypeError( 'invalid option. `note` option must be a string primitive. Option: `' + opts.note + '`.' );
68+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'note', opts.note ) );
6869
}
6970
if ( hasOwnProp( options, 'otp' ) ) {
7071
opts.otp = options.otp;
7172
if ( !isString( opts.otp ) ) {
72-
return new TypeError( 'invalid option. `otp` option must be a string primitive. Option: `' + opts.otp + '`.' );
73+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'otp', opts.otp ) );
7374
}
7475
}
7576
if ( hasOwnProp( options, 'note_url' ) ) {
7677
opts.note_url = options.note_url;
7778
if ( !isURI( opts.note_url ) ) {
78-
return new TypeError( 'invalid option. `note_url` option must be a valid URI. Option: `' + opts.note_url + '`.' );
79+
return new TypeError( format( 'invalid option. `%s` option must be a valid URI. Option: `%s`.', 'note_url', opts.note_url ) );
7980
}
8081
}
8182
if ( hasOwnProp( options, 'client_id' ) ) {
8283
opts.client_id = options.client_id;
8384
if ( !isString( opts.client_id ) || opts.client_id.length !== 20 ) {
84-
return new TypeError( 'invalid option. `client_id` option must be a 20-character string primitive. Option: `' + opts.client_id + '`.' );
85+
return new TypeError( format( 'invalid option. `%s` option must be a 20-character string primitive. Option: `%s`.', 'client_id', opts.client_id ) );
8586
}
8687
}
8788
if ( hasOwnProp( options, 'client_secret' ) ) {
@@ -90,19 +91,19 @@ function validate( opts, options ) {
9091
!isString( opts.client_secret ) ||
9192
opts.client_secret.length !== 40
9293
) {
93-
return new TypeError( 'invalid option. `client_secret` option must be a 40-character string primitive. Option: `' + opts.client_secret + '`.' );
94+
return new TypeError( format( 'invalid option. `%s` option must be a 40-character string primitive. Option: `%s`.', 'client_secret', opts.client_secret ) );
9495
}
9596
}
9697
if ( hasOwnProp( options, 'fingerprint' ) ) {
9798
opts.fingerprint = options.fingerprint;
9899
if ( !isString( opts.fingerprint ) ) {
99-
return new TypeError( 'invalid option. `fingerprint` option must be a string primitive. Option: `' + opts.fingerprint + '`.' );
100+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'fingerprint', opts.fingerprint ) );
100101
}
101102
}
102103
if ( hasOwnProp( options, 'useragent' ) ) {
103104
opts.useragent = options.useragent;
104105
if ( !isString( opts.useragent ) ) {
105-
return new TypeError( 'invalid option. `useragent` option must be a string primitive. Option: `' + opts.useragent + '`.' );
106+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive. Option: `%s`.', 'useragent', opts.useragent ) );
106107
}
107108
}
108109
return null;

lib/node_modules/@stdlib/datasets/sotu/lib/validate.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2626
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
2727
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
2828
var isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array' ).primitives;
29+
var format = require( '@stdlib/string/format' );
2930

3031

3132
// MAIN //
@@ -55,15 +56,15 @@ var isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array'
5556
*/
5657
function validate( opts, options ) {
5758
if ( !isObject( options ) ) {
58-
return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' );
59+
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
5960
}
6061
if ( hasOwnProp( options, 'name' ) ) {
6162
opts.name = options.name;
6263
if (
6364
!isString( opts.name ) &&
6465
!isStringArray( opts.name )
6566
) {
66-
return new TypeError( 'invalid option. `name` option must be a string primitive or an array of strings. Option: `' + opts.name + '`.' );
67+
return new TypeError( format( 'invalid option. `%s` option must be a string primitive or an array of strings. Option: `%s`.', 'name', opts.name ) );
6768
}
6869
}
6970
if ( hasOwnProp( options, 'year' ) ) {
@@ -72,7 +73,7 @@ function validate( opts, options ) {
7273
!isPositiveInteger( opts.year ) &&
7374
!isPositiveIntegerArray( opts.year )
7475
) {
75-
return new TypeError( 'invalid option. `year` option must be a positive integer or an array of positive integers. Option: `' + opts.year + '`.' );
76+
return new TypeError( format( 'invalid option. `%s` option must be a positive integer or an array of positive integers. Option: `%s`.', 'year', opts.year ) );
7677
}
7778
}
7879
if ( hasOwnProp( options, 'range' ) ) {
@@ -81,13 +82,13 @@ function validate( opts, options ) {
8182
!isPositiveIntegerArray( opts.range ) ||
8283
opts.range.length !== 2
8384
) {
84-
return new TypeError( 'invalid option. `range` option must be a positive integer array of length two. Option: `' + opts.range + '`.' );
85+
return new TypeError( format( 'invalid option. `%s` option must be a positive integer array of length two. Option: `%s`.', 'range', opts.range ) );
8586
}
8687
if ( opts.range[ 0 ] < 1790 ) {
87-
return new RangeError( 'invalid option. `range[0]` cannot be less than `1790`. Option: `' + opts.range + '`.' );
88+
return new RangeError( format( 'invalid option. `%s` cannot be less than `1790`. Option: `%s`.', 'range[0]', opts.range ) );
8889
}
8990
if ( opts.range[ 1 ] > 5000 ) {
90-
return new RangeError( 'invalid option. `range[1]` cannot be greater than `5000`. Option: `' + opts.range + '`.' );
91+
return new RangeError( format( 'invalid option. `%s` cannot be greater than `5000`. Option: `%s`.', 'range[1]', opts.range ) );
9192
}
9293
}
9394
if ( hasOwnProp( options, 'party' ) ) {
@@ -96,7 +97,7 @@ function validate( opts, options ) {
9697
!isString( opts.party ) &&
9798
!isStringArray( opts.party )
9899
) {
99-
return new TypeError( 'invalid option. `party` option must be a primitive string or an array of strings. Option: `' + opts.party + '`.' );
100+
return new TypeError( format( 'invalid option. `%s` option must be a primitive string or an array of strings. Option: `%s`.', 'party', opts.party ) );
100101
}
101102
}
102103
return null;

lib/node_modules/@stdlib/ndarray/array/lib/main.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var createBuffer = require( '@stdlib/ndarray/base/buffer' );
3939
var getType = require( '@stdlib/ndarray/base/buffer-dtype' );
4040
var arrayShape = require( '@stdlib/array/shape' );
4141
var flattenArray = require( '@stdlib/utils/flatten-array' );
42+
var format = require( '@stdlib/string/format' );
4243
var isArrayLikeObject = require( './is_array_like_object.js' );
4344
var defaults = require( './defaults.json' );
4445
var castBuffer = require( './cast_buffer.js' );
@@ -127,23 +128,23 @@ function array() {
127128
} else {
128129
options = arguments[ 0 ];
129130
if ( !isObject( options ) ) {
130-
throw new TypeError( 'invalid argument. Must provide either a valid data source, options argument, or both. Value: `' + options + '`.' );
131+
throw new TypeError( format( 'invalid argument. Must provide either a valid data source, options argument, or both. Value: `%s`.', options ) );
131132
}
132133
if ( hasOwnProp( options, 'buffer' ) ) {
133134
buffer = options.buffer;
134135
if ( !isArrayLikeObject( buffer ) ) { // weak test
135-
throw new TypeError( 'invalid option. `buffer` option must be an array-like object, typed-array-like, a Buffer, or an ndarray. Option: `' + buffer + '`.' );
136+
throw new TypeError( format( 'invalid option. `%s` option must be an array-like object, typed-array-like, a Buffer, or an ndarray. Option: `%s`.', 'buffer', buffer ) );
136137
}
137138
}
138139
}
139140
} else {
140141
buffer = arguments[ 0 ];
141142
if ( !isArrayLikeObject( buffer ) ) { // weak test
142-
throw new TypeError( 'invalid option. Data source must be an array-like object, typed-array-like, a Buffer, or an ndarray. Value: `' + buffer + '`.' );
143+
throw new TypeError( format( 'invalid option. Data source must be an array-like object, typed-array-like, a Buffer, or an ndarray. Value: `%s`.' , buffer ) );
143144
}
144145
options = arguments[ 1 ];
145146
if ( !isObject( options ) ) {
146-
throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
147+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
147148
}
148149
// Note: we ignore whether `options` has a `buffer` property
149150
}
@@ -163,23 +164,23 @@ function array() {
163164
if ( hasOwnProp( options, 'casting' ) ) {
164165
opts.casting = options.casting;
165166
if ( !isCastingMode( opts.casting ) ) {
166-
throw new TypeError( 'invalid option. `casting` option must be a recognized casting mode. Option: `' + opts.casting + '`.' );
167+
throw new TypeError( format( 'invalid option. `%s` option must be a recognized casting mode. Option: `%s`.', 'casting', opts.casting ) );
167168
}
168169
} else {
169170
opts.casting = defaults.casting;
170171
}
171172
if ( hasOwnProp( options, 'flatten' ) ) {
172173
opts.flatten = options.flatten;
173174
if ( !isBoolean( opts.flatten ) ) {
174-
throw new TypeError( 'invalid option. `flatten` option must be a boolean. Option: `' + opts.flatten + '`.' );
175+
throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'flatten', opts.flatten ) );
175176
}
176177
} else {
177178
opts.flatten = defaults.flatten;
178179
}
179180
if ( hasOwnProp( options, 'ndmin' ) ) {
180181
opts.ndmin = options.ndmin;
181182
if ( !isNonNegativeInteger( opts.ndmin ) ) {
182-
throw new TypeError( 'invalid option. `ndmin` option must be a nonnegative integer. Option: `' + opts.ndmin + '`.' );
183+
throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'ndmin', opts.ndmin ) );
183184
}
184185
// TODO: validate that minimum number of dimensions does not exceed the maximum number of possible dimensions (in theory, infinite; in practice, determined by max array length; see https://github.com/stdlib-js/stdlib/blob/ac350059877c036640775d6b30d0e98e840d07cf/lib/node_modules/%40stdlib/ndarray/ctor/lib/main.js#L57)
185186
} else {
@@ -190,10 +191,10 @@ function array() {
190191
if ( hasOwnProp( options, 'dtype' ) ) {
191192
dtype = options.dtype;
192193
if ( !isDataType( dtype ) ) {
193-
throw new TypeError( 'invalid option. `dtype` option must be a recognized data type. Option: `' + dtype + '`.' );
194+
throw new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', dtype ) );
194195
}
195196
if ( btype && !isAllowedCast( btype, dtype, opts.casting ) ) {
196-
throw new Error( 'invalid option. Data type cast is not allowed. Casting mode: `' + opts.casting + '`. From: `' + btype + '`. To: `' + dtype + '`.' );
197+
throw new Error( format( 'invalid option. Data type cast is not allowed. Casting mode: `%s`. From: `%s`. To: `%s`.', opts.casting, btype, dtype ) );
197198
}
198199
} else if ( btype ) {
199200
// TODO: reconcile difference in behavior when provided a generic array and no `dtype` option. Currently, we cast here, but do not allow casting a generic array (by default) when explicitly providing a `dtype` option.
@@ -231,7 +232,7 @@ function array() {
231232
order = defaults.order;
232233
}
233234
} else if ( !isOrder( order ) ) {
234-
throw new TypeError( 'invalid option. `order` option must be a recognized order. Option: `' + order + '`.' );
235+
throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', order ) );
235236
}
236237
} else {
237238
order = defaults.order;
@@ -254,7 +255,7 @@ function array() {
254255
if ( hasOwnProp( options, 'copy' ) ) {
255256
opts.copy = options.copy;
256257
if ( !isBoolean( opts.copy ) ) {
257-
throw new TypeError( 'invalid option. `copy` option must be a boolean. Option: `' + opts.copy + '`.' );
258+
throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'copy', opts.copy ) );
258259
}
259260
} else {
260261
opts.copy = defaults.copy;
@@ -263,7 +264,7 @@ function array() {
263264
if ( hasOwnProp( options, 'shape' ) ) {
264265
shape = options.shape;
265266
if ( !isArrayLikeObject( shape ) ) { // weak test
266-
throw new TypeError( 'invalid option. `shape` option must be an array-like object containing nonnegative integers. Option: `' + shape + '`.' );
267+
throw new TypeError( format( 'invalid option. `%s` option must be an array-like object containing nonnegative integers. Option: `%s`.', 'shape', shape ) );
267268
}
268269
ndims = shape.length;
269270
len = numel( shape );

0 commit comments

Comments
 (0)