nGitDB exposes explicit error classes so application code can handle safety failures without string matching.
import {
GitDBError,
OwnershipViolationError,
SessionMisuseError,
ValidationFailureError,
} from "@nuanst-one/ngitdb";
All library errors extend:
GitDBError
InvalidResourcePathError is thrown when a resource path is not in <collection>/<id> format.
ResourceDefinitionError is thrown when the collection is not configured.
ResourceNotFoundError is thrown when the resolved JSON file does not exist.
SessionMisuseError is thrown when a workflow method is called at the wrong time.
Examples:
patch(...) before startSession(...)commit(...) without staged changescreatePullRequest(...) before a commitOwnershipViolationError is thrown when a patch targets a field that is not machine-owned.
try {
await db.patch("companies/acme-gmbh", {
legalName: "New Name GmbH",
});
} catch (error) {
if (error instanceof OwnershipViolationError) {
console.log(error.resourcePath);
console.log(error.fieldPath);
console.log(error.owner);
}
}
ValidationFailureError is thrown when a resource validator returns issues during commit.
try {
await db.commit("Commit staged changes");
} catch (error) {
if (error instanceof ValidationFailureError) {
console.log(error.resourcePath);
console.log(error.issues);
}
}
try {
await db.commit("Update enrichment fields");
} catch (error) {
if (error instanceof ValidationFailureError) {
return {
ok: false,
reason: "validation_failed",
issues: error.issues,
};
}
if (error instanceof GitDBError) {
return {
ok: false,
reason: error.name,
message: error.message,
};
}
throw error;
}