Skip to content

Handling errors

Every failure is a DriveDBError with a stable code, the HTTP status, a readable message and sometimes details (such as the field that failed).

import { DriveDB, DriveDBError } from '@faeizfurqan/drivedb';
const db = new DriveDB(process.env.DRIVEDB_URL!).db();
try {
await db.collection('clients').insertOne({ name: 'Bad date', nextFollowUp: 'someday' });
} catch (error) {
if (!(error instanceof DriveDBError)) throw error;
switch (error.code) {
case 'ValidationError': // a value doesn't fit the field's type
console.log('Fix the data:', error.message);
break;
case 'Unauthorized': // revoked, expired or wrong connection string
case 'ReadOnlyKey': // a read-only connection string tried to write
console.log('Check the connection string');
break;
default:
if (error.retryable)
console.log('Try again later'); // rate limit or Drive hiccup
else throw error;
}
}

The SDK retries by itself, up to 3 times with backoff:

  • 429 RateLimited: after the Retry-After the server sends;
  • 503 when Google Drive is briefly unavailable;
  • network errors and timeouts, for reads only. A write whose answer got lost may have been applied, so it is not repeated; check (for example with findOne) before trying again, or use your own _id so a repeated insert fails with DuplicateKey instead of creating a copy.

error.retryable tells you whether waiting and trying again makes sense. Tune with new DriveDB(url, { retries: 5, timeoutMs: 10_000 }).

See the error reference for every code.

Made by Faeiz Furqan