Skip to content

InvalidArgumentError

reference
1 min readUpdated

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

mermaid
graph LR
  A[Caller input] --> B[Argument validation]
  B -->|Invalid argument| C[InvalidArgumentError]
  C --> D[Caller handles error]

Usage

js
import { 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 InvalidArgumentError when 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 InvalidArgumentError when 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)

  • programindex.js:7
  • Argumentlib/argument.js:3
  • Optionlib/option.js:3

Was this page helpful?

Download as PDF