dimah-formv0.2.0

Errors

Stable error codes, field issues, and helpers for mapping them in the UI.

Failed requests return JSON with a stable code, an English message, and optional issues.


Standard Error Response Schema

type ErrorResponseBody = {
  /** Human-readable explanation of the error */
  message: string;

  /** Stable machine-readable error code */
  code?: string;

  /** Optional parameters for message interpolation */
  params?: Record<string, string | number>;

  /** Field-level validation issues (present on VALIDATION_ERROR) */
  issues?: Array<{
    field: string;
    message: string;
    code?: string;
    params?: Record<string, string | number>;
  }>;
};

Core Error Codes (FORM_ERROR_CODES)

Error CodeHTTP StatusTrigger Condition
NOT_FOUND404Requested URL route does not match any registered endpoint.
UNKNOWN_FORM404Form ID or slug could not be found in code catalogs or database.
UNKNOWN_RESPONSE404Response ID does not exist in the database.
UNAUTHORIZED401Thrown by guard when the user is unauthenticated.
FORBIDDEN403Thrown by guard when the user lacks permissions or ownership.
STALE_UPDATE409updatedAt CAS token mismatch (concurrent write collision).
FORM_INACTIVE409startResponse called on a form with status "draft" or "archived".
RESPONSE_NOT_DRAFT409saveDraft or submitResponse attempted on a locked response.
RESPONSE_NOT_LOCKED409reopenResponse attempted on an active draft response.
CODE_AUTHORED_FORM409Overwrite (saveForm) or delete (deleteForm) attempted on a code-authored form.
FORM_HAS_RESPONSES409deleteForm attempted on a form that already has recorded responses.
SLUG_TAKEN409Form slug collides with an existing form.
VALIDATION_ERROR400Payload or answer validation failure (inspect issues[]).
UNKNOWN_FIELD_TYPE400Form references a field type not registered in fieldTypes.
RESUME_REQUIRES_RESPONDENT400startResponse({ resume: true }) called without a respondentId.
INTERNAL_ERROR500Unhandled server exception.

Field Issue Codes (FIELD_ISSUE_CODES)

When a 400 VALIDATION_ERROR occurs during submission, the issues[] array contains field-specific issue codes:

Issue CodeDescriptionParameters
REQUIREDVisible required field was left empty on submit.
UNKNOWN_FIELDAnswer key does not exist in the form snapshot.
UNKNOWN_FIELD_TYPEField type has no registered validator.{ type }
INVALIDCustom validator rejected the answer.
EXPECTED_STRINGAnswer must be a string.
EXPECTED_NUMBERAnswer must be a finite number.
EXPECTED_INTEGERAnswer must be an integer.
EXPECTED_BOOLEANAnswer must be a boolean (true or false).
EXPECTED_STRING_ARRAYAnswer must be an array of strings.
EXPECTED_EMAILAnswer must be a valid email address.
EXPECTED_DATEAnswer must be an ISO calendar date (YYYY-MM-DD).
TOO_SHORTText length is below minLength.{ min }
TOO_LONGText length exceeds maxLength.{ max }
TOO_SMALLNumeric value is below min.{ min }
TOO_LARGENumeric value exceeds max.{ max }
INVALID_FORMATText does not match the configured pattern regex.
INVALID_OPTIONSelected value is not in the allowed options list.
DUPLICATE_OPTIONmultiSelect array contains duplicate values.

Client Error Helpers

@dimah-form/react provides helpers to map error codes to user-friendly or localized messages:

import {
  fieldIssueMap,
  formErrorMessage,
  issuesByField,
} from "@dimah-form/react";

// Convert APIError issues array into a field-id-to-message map
const issues = fieldIssueMap(apiError);
// { fullName: "Full Name is required", email: "Invalid email" }

// Extract formatted top-level error message
const message = formErrorMessage(error);

Declaring Custom Error Codes with defineErrorCodes

When authoring plugins or custom field validators, use defineErrorCodes() to declare type-safe custom error catalogs:

import { defineErrorCodes } from "@dimah-form/server";

export const CUSTOM_ERROR_CODES = defineErrorCodes({
  PAYMENT_REQUIRED: "Payment verification failed",
  SUBSCRIPTION_EXPIRED: "Account subscription is inactive",
});

Next Steps

On this page