denodrivers / mongo

🍃 MongoDB driver for Deno 🦕
https://deno.land/x/mongo
MIT License
509 stars 95 forks source link

Upstream the errors properly #370

Open devvaneio opened 2 years ago

devvaneio commented 2 years ago

Hi, congrats on the project.

I'm having a hard time trying to get errors from the driver, please take a look:

https://github.com/denodrivers/mongo/blob/d5445a476308a6ded290ded326654aa4c92e5e76/src/collection/collection.ts#L191

Could we replace that by the following:

 const [error] = writeErrors; 

There's no reason to not upstream the entire error. I tested it by changing the cache file.

Before:

MongoError: "Document failed validation"
    at Collection.insertMany (https://deno.land/x/mongo@v0.31.0/src/collection/collection.ts:192:13)
    at async Collection.insertOne (https://deno.land/x/mongo@v0.31.0/src/collection/collection.ts:149:29)
    at async createCampaign (file:///home/jamal/Desktop/side-projects/satiro.co/core/services/create-campaign.service.ts:10:17)
    at async Server.<anonymous> (file:///home/jamal/Desktop/side-projects/satiro.co/functions/create-campaign.function.ts:20:24)
    at async Server.#respond (https://deno.land/std@0.140.0/http/server.ts:298:18)

Now:

Error: MongoError: {"index":0,"code":121,"errInfo":{"failingDocumentId":"62ef419eb11a449f45637eed","details":{"operatorName":"$jsonSchema","schemaRulesNotSatisfied":[{"operatorName":"required","specifiedAs":{"required":["id","influencer","product","status","expiredAt","affiliateLink"]},"missingProperties":["id"]}]}},"errmsg":"Document failed validation"}
    at Collection.insertMany (https://deno.land/x/mongo@v0.31.0/src/collection/collection.ts:192:13)
    at async Collection.insertOne (https://deno.land/x/mongo@v0.31.0/src/collection/collection.ts:149:29)
    at async createCampaign (file:///home/jamal/Desktop/side-projects/satiro.co/core/services/create-campaign.service.ts:10:17)
    at async Server.<anonymous> (file:///home/jamal/Desktop/side-projects/satiro.co/functions/create-campaign.function.ts:20:24)
    at async Server.#respond (https://deno.land/std@0.140.0/http/server.ts:298:18)

With that, I can parse the errors and give some hints to solve the problem when the user is trying to save the document with the wrong schema or handle errors accordingly with its error code.

Update: We also need to add the errInfo to the class just like the nodejs driver: https://github.com/mongodb/node-mongodb-native/blob/417655ac9d87c1c9ca255dfcc81ca828ffde731a/src/error.ts#L171

lucsoft commented 2 years ago

we could also wrap the extra details in a cause error

image
SagarMahadik commented 1 year ago

Hi, first of thank you for the library. Makes life easier working with the mongo on Deno. I am working on the app, which requires the collection validationSchema. As per the new Mongo DB update, the details of this error are sent through the errorInfo field, which is very helpful. can you please help with entire details. Thank you !

{
   "code": 121,
   "errmsg": "Document failed validation",
   "errInfo": {
       "failingDocumentId": ObjectId("5fe0eb9642c10f01eeca66a9"),
       "details": {
           "operatorName": "$jsonSchema",
           "schemaRulesNotSatisfied": [
               {
                   "operatorName": "properties",
                   "propertiesNotSatisfied": [
                       {
                           "propertyName": "price",
                           "details": [
                               {
                                   "operatorName": "minimum",
                                   "specifiedAs": {
                                       "minimum": 0
                                   },
                                   "reason": "comparison failed",
                                   "consideredValue": -2
                               }
                           ]
                       }
                   ]
               }
           ]
       }
   }
}