handle-cli-error
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -13,3 +13,3 @@ import normalizeException from"normalize-exception"; | ||
opts:{silent,short,exitCode,timeout}}= | ||
getOpts(errorA,opts); | ||
getOpts(opts,errorA); | ||
printError(errorB,silent,short); | ||
@@ -16,0 +16,0 @@ exitProcess(exitCode,timeout); |
@@ -1,2 +0,1 @@ | ||
import isPlainObj from"is-plain-obj"; | ||
import normalizeException from"normalize-exception"; | ||
@@ -7,10 +6,9 @@ | ||
import{applyClassesOpts}from"./classes.js"; | ||
import{applyDefaultOpts}from"./default.js"; | ||
import{handleInvalidOpts,validateOpts}from"./validate.js"; | ||
import{removeUndefined,applyDefaultOpts}from"./default.js"; | ||
import{validateOpts}from"./validate.js"; | ||
export const getOpts=function(error,opts){ | ||
export const getOpts=function(opts,error){ | ||
try{ | ||
return safeGetOpts(error,opts); | ||
return safeGetOpts(opts,error); | ||
}catch(error_){ | ||
@@ -22,10 +20,6 @@ const errorA=normalizeException(error_); | ||
const safeGetOpts=function(error,opts={}){ | ||
if(!isPlainObj(opts)){ | ||
return handleInvalidOpts(`options must be a plain object: ${opts}`); | ||
} | ||
const optsA=applyClassesOpts(opts,error); | ||
const safeGetOpts=function(opts,error){ | ||
validateOpts(opts,["options"]); | ||
const optsA=applyClassesOpts(error,opts); | ||
const optsB=applyDefaultOpts(optsA); | ||
validateOpts(optsB); | ||
return{error,opts:optsB}; | ||
@@ -35,2 +29,9 @@ }; | ||
const applyClassesOpts=function({name},{classes={},...opts}={}){ | ||
const classesOpts=classes[name]||classes.default||{}; | ||
return{...opts,...removeUndefined(classesOpts)}; | ||
}; | ||
const INVALID_OPTS={ | ||
@@ -37,0 +38,0 @@ silent:false, |
@@ -0,1 +1,3 @@ | ||
import isPlainObj from"is-plain-obj"; | ||
import{MIN_EXIT_CODE,MAX_EXIT_CODE}from"../exit.js"; | ||
@@ -5,16 +7,37 @@ import{NO_TIMEOUT,INFINITE_TIMEOUT}from"../timeout.js"; | ||
export const validateOpts=function({silent,short,exitCode,timeout}){ | ||
validateBooleanOpt(silent,"silent"); | ||
validateBooleanOpt(short,"short"); | ||
validateExitCode(exitCode); | ||
validateTimeout(timeout); | ||
export const validateOpts=function(opts,optName){ | ||
if(opts===undefined){ | ||
return; | ||
} | ||
if(!isPlainObj(opts)){ | ||
handleInvalidOpts("must be a plain object",opts,optName); | ||
} | ||
Object.entries(opts).forEach(([key,optValue])=>{ | ||
validateOpt(optValue,[...optName,key]); | ||
}); | ||
}; | ||
const validateOpt=function(optValue,optName){ | ||
if(optValue===undefined){ | ||
return; | ||
} | ||
const validator=VALIDATORS[optName[optName.length-1]]; | ||
if(validator===undefined){ | ||
handleInvalidOpts("is an unknown option","",optName); | ||
} | ||
validator(optValue,optName); | ||
}; | ||
const validateBooleanOpt=function(value,optName){ | ||
if(typeof value!=="boolean"){ | ||
return handleInvalidOpts(`options.${optName} must be a boolean: ${value}`); | ||
handleInvalidOpts("must be a boolean",value,optName); | ||
} | ||
}; | ||
const validateExitCode=function(exitCode){ | ||
const validateExitCode=function(exitCode,optName){ | ||
if( | ||
@@ -25,4 +48,6 @@ !Number.isInteger(exitCode)|| | ||
{ | ||
return handleInvalidOpts( | ||
`options.exitCode must be between ${MIN_EXIT_CODE} and ${MAX_EXIT_CODE}: ${exitCode}`); | ||
handleInvalidOpts( | ||
`must be between ${MIN_EXIT_CODE} and ${MAX_EXIT_CODE}`, | ||
exitCode, | ||
optName); | ||
@@ -32,3 +57,3 @@ } | ||
const validateTimeout=function(timeout){ | ||
const validateTimeout=function(timeout,optName){ | ||
if( | ||
@@ -38,4 +63,6 @@ (!Number.isInteger(timeout)||timeout<=0)&& | ||
{ | ||
return handleInvalidOpts( | ||
`options.timeout must be 0, a positive integer or Infinity: ${timeout}`); | ||
handleInvalidOpts( | ||
"must be 0, a positive integer or Infinity", | ||
timeout, | ||
optName); | ||
@@ -49,6 +76,30 @@ } | ||
const validateClasses=function(classes,optName){ | ||
if(!isPlainObj(classes)){ | ||
handleInvalidOpts("must be a plain object",classes,optName); | ||
} | ||
export const handleInvalidOpts=function(message){ | ||
throw new Error(`handle-cli-error invalid usage: ${message}`); | ||
if(optName.length>2){ | ||
handleInvalidOpts("must not be defined",classes,optName); | ||
} | ||
Object.entries(classes).forEach(([className,classOpts])=>{ | ||
validateOpts(classOpts,[...optName,className]); | ||
}); | ||
}; | ||
const VALIDATORS={ | ||
silent:validateBooleanOpt, | ||
short:validateBooleanOpt, | ||
exitCode:validateExitCode, | ||
timeout:validateTimeout, | ||
classes:validateClasses}; | ||
const handleInvalidOpts=function(message,value,optName){ | ||
const fullOptName=optName.join("."); | ||
throw new Error( | ||
`handle-cli-error invalid usage: "${fullOptName}" ${message}: ${value}`); | ||
}; | ||
//# sourceMappingURL=validate.js.map |
{ | ||
"name": "handle-cli-error", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "exports": "./build/src/main.js", |
26386
256
11