From ea31ace0ebd152615b2e4f2aed5cf2ad80c1c8c6 Mon Sep 17 00:00:00 2001 From: Navy Date: Sat, 12 Oct 2024 13:51:22 +0300 Subject: [PATCH] Helper func to parse validation error --- src/MariaDB.ts | 3 +++ src/MongoDB.ts | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/MariaDB.ts b/src/MariaDB.ts index 322bd8d..070f710 100644 --- a/src/MariaDB.ts +++ b/src/MariaDB.ts @@ -277,12 +277,15 @@ class MariaDB } return new Promise(resolve => { + this.#logger?.info('Closing pool'); if (!this.#pool) { + this.#logger?.debug('Pool was already closed'); resolve(); return; } this.#pool.end(() => { + this.#logger?.status('Pool closed'); this.#pool?.removeAllListeners(); resolve(); }); diff --git a/src/MongoDB.ts b/src/MongoDB.ts index 786c8a9..43184f3 100644 --- a/src/MongoDB.ts +++ b/src/MongoDB.ts @@ -1,5 +1,5 @@ import { inspect } from 'node:util'; -import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions, FindOptions, ModifyResult, DistinctOptions } from 'mongodb'; +import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions, FindOptions, ModifyResult, DistinctOptions, MongoServerError } from 'mongodb'; import { IServer, ILogger } from './interfaces/index.js'; interface Credentials { @@ -40,6 +40,16 @@ const objIsSubset = (superObj: StringIndexable, subObj: StringIndexable): boolea }); }; +export const parseValidationError = (error: MongoServerError) => +{ + if (error.code !== 121 || !error.errInfo) + throw new Error('Not a validation error'); + const rule = error.errInfo.details.schemaRulesNotSatisfied; + const missing = rule.missingProperties as string[]; + const required = rule.specifiedAs.required as string[]; + return { missing, required }; +}; + /** * A dedicated class to locally wrap the mongodb API wrapper *