Errors

Every failed request rejects with an Error that carries a code, so you can branch without parsing messages. The type is exported as DianaDbError.

codeMeaningWas the change applied?
SERVERThe server rejected the request: validation, access rights, unknown collection, locked schema…No
DURABILITY The server applied the change in memory but could not write it to its write-ahead log. The change is visible now and may be lost if the server crashes before its next snapshot Yes — don't retry
MEMORY The server is almost out of memory and refused the request. It refuses every request except health, version and removals until memory is freed (see memory guard) No
TIMEOUTNo answer within requestTimeoutValue (default 30000 ms)Unknown
CONNECTION Not connected (before connect() or after disconnect()), or the connection closed before the answer arrived. A connection the server closes right away usually means a wrong user name or password, and the message says so Unknown (No when not connected)
import { DianaDbError } from '@diana-db/odm';

try {
  await orderModel.insert(order);
} catch (e) {
  switch ((e as DianaDbError).code) {
    case 'DURABILITY':
      // applied, but not on disk yet: alert, don't retry
      break;
    case 'MEMORY':
      // refused, nothing changed: alert, retry once memory is freed
      break;
    case 'TIMEOUT':
    case 'CONNECTION':
      // outcome unknown: check before retrying a non-idempotent write
      break;
    default:
      // SERVER error, or a validation error thrown by the ODM itself (no code)
      throw e;
  }
}
  

Validation errors raised by the ODM itself — schema, queries, options, migrations without a database — are thrown before anything is sent and carry no code.