Kind: Class
Source: lib/error.js
InvalidArgumentError class
InvalidArgumentError represents an error raised when a caller passes an argument that does not meet an API's expected requirements. It lets validation code communicate argument-related failures distinctly from other error types.
Extends: CommanderError
Diagram
mermaidgraph LR A[Caller input] --> B[Argument validation] B -->|Invalid argument| C[InvalidArgumentError] C --> D[Caller handles error]
Usage
jsimport { InvalidArgumentError } from './lib/error.js';
function setTimeoutDelay(delay) {
if (typeof delay !== 'number' || delay < 0) {
throw new InvalidArgumentError('delay must be a non-negative number');
}
return delay;
}
try {
setTimeoutDelay(-1);
} catch (error) {
if (error instanceof InvalidArgumentError) {
console.error(error.message);
} else {
throw error;
}
}
AI Coding Instructions
- Throw
InvalidArgumentErrorwhen validation fails because of an invalid caller-supplied argument. - Include an error message that identifies the invalid argument and the expected constraint.
- Check for this class with
instanceof InvalidArgumentErrorwhen callers need argument-specific handling. - Keep argument validation close to the public API boundary before processing the input.
Used by
3 references from 3 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (3)
program—index.js:7Argument—lib/argument.js:3Option—lib/option.js:3
Was this page helpful?