Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstream the errors properly #370

Open
devvaneio opened this issue Aug 7, 2022 · 2 comments
Open

Upstream the errors properly #370

devvaneio opened this issue Aug 7, 2022 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@devvaneio
Copy link

devvaneio commented Aug 7, 2022

Hi, congrats on the project.

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

const [{ errmsg }] = writeErrors;

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

@erfanium erfanium added enhancement New feature or request good first issue Good for newcomers labels Aug 7, 2022
@lucsoft
Copy link
Collaborator

lucsoft commented Aug 7, 2022

we could also wrap the extra details in a cause error
image

@SagarMahadik
Copy link

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
                               }
                           ]
                       }
                   ]
               }
           ]
       }
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants