Class: ValidationError

ValidationError(message, code)

new ValidationError(message, code)

Custom error class for validation errors Extends the native Error class with an additional error code property

Parameters:
Name Type Description
message string

Human-readable error message describing the validation failure

code string

Machine-readable error code for programmatic error handling (e.g., INVALID_EMAIL_FORMAT, MISSING_DATE, AGE_TOO_YOUNG)

Properties:
Name Type Description
name string

Always set to "ValidationError"

code string

The error code provided during construction

message string

The error message provided during construction

Source:
Examples
// Throw a validation error with code
throw new ValidationError("Email address must be in a valid format", "INVALID_EMAIL_FORMAT");
// Catch and handle validation error
try {
  validateEmail("invalid-email");
} catch (error) {
  if (error instanceof ValidationError) {
    console.log(error.code); // "INVALID_EMAIL_FORMAT"
    console.log(error.message); // "Email address must be in a valid format"
  }
}

Extends

  • Error