跳到主要内容

Error Handling

CDF throws structured errors for bootstrap and configuration failures. Catch them to detect missing adaptors, duplicate registrations, invalid config, and provide meaningful feedback to users.

What it does

Instead of generic Error objects, CDF exports typed error classes with:

  • errorCode — machine-readable error identifier (e.g., ADAPTOR_NOT_FOUND)
  • context — additional details (missing adaptor name, expected values, etc.)
  • component — which CDF layer threw the error

This lets you handle specific failures in code, not just show generic error messages.

How to use it

Catch registry errors

Thrown during adaptor registration and bootstrap:

import { AdaptorRegistry, initCDF, ESPCDFRegistryError } from "@espressif/rainmaker-base-cdf";

const registry = AdaptorRegistry.getInstance();

try {
registry.register(myAdaptor);
registry.setActiveAdaptor(myAdaptor._identifier);
await initCDF({ sdkAdaptorRegistry: registry });
} catch (e) {
if (e instanceof ESPCDFRegistryError) {
console.error(`Registry failed: ${e.errorCode}`);
console.error("Details:", e.context);
// Handle specific error codes...
if (e.errorCode === "ADAPTOR_ALREADY_EXISTS") {
// User registered the same adaptor twice
}
}
throw e;
}

Catch config errors

Thrown when initCDF is called with missing or invalid config:

import { initCDF, ESPCDFConfigError } from "@espressif/rainmaker-base-cdf";

try {
await initCDF({} as any); // Empty config!
} catch (e) {
if (e instanceof ESPCDFConfigError) {
if (e.errorCode === "CDF_CONFIG_MISSING") {
console.error("Please pass a registry:", e.context);
// Show setup instructions to user
}
}
}

Error types

Error class hierarchy

ClassPurpose
ESPCDFErrorAbstract base class — has component, errorCode, context
ESPCDFConfigErrorThrown by initCDF() for missing or invalid config
ESPCDFRegistryErrorThrown by adaptor registry operations

Error codes

Registry errors

CodeWhenTypical fix
ADAPTOR_ALREADY_EXISTSRegistering a duplicate _identifierCheck if adaptor already registered
ADAPTOR_NOT_FOUNDCalling getAdaptor(), setActiveAdaptor(), or unregister() with unknown IDVerify adaptor was registered first
ADAPTOR_METHOD_PROPERTY_NOT_IMPLEMENTEDCalling an adaptor method that wasn't implementedCheck adaptor's interface implementation
NO_ACTIVE_ADAPTOR_SETCalling getActiveAdaptor() before setActiveAdaptor()Call setActiveAdaptor() first

Config errors

CodeWhenTypical fix
CDF_CONFIG_MISSINGCalling initCDF() with empty/null configPass a valid registry: initCDF({ sdkAdaptorRegistry })

See also

Setup & bootstrap

Implementation

API Reference

On this page