Skip to main content
Version: 1.4.0

Error Handling

JSONVault raises typed errors so you can distinguish between invalid input, policy denials, and query failures without string matching. Each error inherits from JsonVaultError, exposes a stable code, and carries a details object for extra context.

Error classes

ErrorCodeWhen it is thrown
InvalidArgumentErrorERR_INVALID_ARGUMENTMissing or malformed input such as CLI flags, document paths, or schema rules
InvalidOperationErrorERR_INVALID_OPERATIONOperation is not allowed in the current state (changing a primary key, changelog disabled, decryption failure, etc.)
AlreadyExistsErrorERR_ALREADY_EXISTSEntity already exists (duplicate documents, migration files, indexes)
NotFoundErrorERR_NOT_FOUNDRequested resource (migration, adapter, config file) could not be located
QueryErrorERR_QUERYSQL / compiled query parsing or execution error
PolicyDeniedErrorERR_POLICY_DENIEDCollection policy rejected a read or write
JsonVaultErrorERR_JSONVAULTBase class for internal failures (should be rare)

Each subclass also exposes a static code property if you prefer comparing against strings.

Handling errors

Express handler
const {
JsonDatabase,
InvalidArgumentError,
AlreadyExistsError,
QueryError,
} = require("jsonvault");

const db = await JsonDatabase.open({ path: "./data" });

app.post("/orders", async (req, res, next) => {
try {
const doc = await db.collection("orders").insertOne(req.body);
res.status(201).json(doc);
} catch (error) {
if (error instanceof InvalidArgumentError) {
return res.status(400).json({ code: error.code, message: error.message });
}
if (error instanceof AlreadyExistsError) {
return res.status(409).json({ code: error.code, message: error.message });
}
if (error instanceof QueryError) {
return res.status(422).json({ code: error.code, message: error.message, details: error.details });
}
return next(error);
}
});

The .details object is free-form and varies by source (e.g. schema path, SQL clause, policy context). Pass it through to logs or telemetry for easier debugging.

CLI behaviour

The jsonvault CLI surfaces the same errors. For example, jsonvault dump --limit=-1 exits with an ERR_INVALID_ARGUMENT message. When scripting, check the process exit code or parse stderr for the error code to branch accordingly.