Error Handling
ClaDI exposes structured error contracts designed for robust runtime diagnostics and predictable handling.
IError
IError extends Error with machine-readable metadata:
code: stable error identifier.context: structured data for debugging/observability.source: subsystem or operation marker.cause: wrapped original error.
src/domain/interface/error.interface.ts
export interface IError extends Error {
cause?: Error;
code: string;
context?: Record<string, unknown>;
source?: string;
}BaseError
BaseError is the default implementation used across infrastructure components:
src/infrastructure/class/base/error.class.ts
import { BaseError } from "@elsikora/cladi";
throw new BaseError("Provider resolution failed", {
code: "PROVIDER_RESOLUTION_FAILED",
source: "DIContainer.resolve",
context: { token: "UserServiceToken" },
});Handling Pattern
src/error-handling.ts
try {
const service = container.resolve(ServiceToken);
service.execute();
} catch (error) {
if (error instanceof BaseError) {
logger.error(error.message, {
source: error.source,
context: { code: error.code, ...error.context },
});
} else {
logger.error("Unexpected error", {
source: "Bootstrap",
context: { error: String(error) },
});
}
}IBaseErrorOptions
Last updated on