denodrivers / denodrivers/mongo

Upstream the errors properly

Open
#370 2 comments 0 reactions 0 assignees View on GitHub
enhancement good first issue
Dominant language
TypeScript
Stars
511
Forks
89
PR merge metrics
No merged PRs in 30d

Description

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:
```typescript
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. (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. (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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.