[GODRIVER-1962] WithTransaction mongodb error detection doesn't unwrap errors Created: 13/Apr/21  Updated: 28/Oct/23  Resolved: 23/Apr/21

Status: Closed
Project: Go Driver
Component/s: API, Error Handling
Affects Version/s: None
Fix Version/s: 1.5.2

Type: Bug Priority: Major - P3
Reporter: Ed Pelc Assignee: Isabella Siu (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

The session `WithTransaction` currently tries to cast the error returned by the user supplied function to see if it is a `CommandError`. I believe it should instead use `errors.As()` to see if the returned error unwraps to a command error so that if a developer wraps an error(ie so you can return a prettier error to users) then the command error will still be detected by the mongodb driver and retry logic still applied.

 

This block specifically the first line with `err.(CommandError)` should be changed to use `errors.As` instead which will check if the error is a CommandError directly or if it unwraps to one and contains a CommandError.

https://github.com/mongodb/mongo-go-driver/blob/master/mongo/session.go#L196-L200

Example use case: https://play.golang.org/p/pOkZ7a9anzj

 

https://golang.org/pkg/errors/#As

 

I believe this is the cause of WriteConflict errors when we moved transactions to production.



 Comments   
Comment by Githook User [ 23/Apr/21 ]

Author:

{'name': 'Isabella Siu', 'email': 'isabella.siu@mongodb.com', 'username': 'iwysiu'}

Message: GODRIVER-1962 unwrap errors in WithTransaction (#634)
Branch: release/1.5
https://github.com/mongodb/mongo-go-driver/commit/54c8ba3f46762dd1af6916ba4de2b4fbab7d06b3

Comment by Githook User [ 23/Apr/21 ]

Author:

{'name': 'Isabella Siu', 'email': 'isabella.siu@mongodb.com', 'username': 'iwysiu'}

Message: GODRIVER-1962 unwrap errors in WithTransaction (#634)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/9a3bd057f614a7dafbfb402a3ac0f7a73643b239

Comment by Isabella Siu (Inactive) [ 15/Apr/21 ]

Hi epelc@greatcloak.com!

Thanks for the bug report! We'll try to have a fix for this in for the next patch version.

Comment by Ed Pelc [ 13/Apr/21 ]

This is the current alternative because the driver can't unwrap WithTransaction errors.

 

https://play.golang.org/p/jS-aw2-4ROC

 

You'd be able to return wrapped errors directly if the driver switched to `errors.As`. Also note that the go standard library now and has for a while recommended using `errors.As` instead of a type assertion.

Generated at Thu Feb 08 08:37:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.