import { isDIDError, type DIDError } from '@d-id/client-sdk';
try {
await agentManager.connect();
} catch (error) {
if (!isDIDError(error)) {
throw error;
}
switch (error.kind) {
case 'HttpError':
console.error(error.status, error.code);
break;
case 'NetworkError':
console.error('offline?', error.endpoint);
break;
default:
// Total on purpose: `kind` is `never` here to the type checker, but a value from
// another copy of the SDK can still land in this branch.
console.error(error.kind, error.message);
}
}
Every error the SDK raises, as a union — what isDIDError narrows a caught value to.
Each member declares kind as a different literal, so a
switchonkindinside anisDIDErrorbranch narrows the value to one class and gives typed access to that class's own fields — status and code on an HttpError, key on a ValidationError, and so on. Thedefaultbranch of an exhaustiveswitchisneverto the type checker.Keep that branch total anyway. isDIDError recognizes any
Errorcarrying a stringkind, which is what lets it see across a bundle boundary, so a value built by a different copy of the SDK — a 2.xHttpError, whosekindwas the server's classification — can reach thedefaultbranch at runtime with akindoutside this union.BaseError itself is deliberately not a member: its
kindis a plainstring, which matches every literal and would stop the narrowing from working.