[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: |
| Comment by Githook User [ 23/Apr/21 ] |
|
Author: {'name': 'Isabella Siu', 'email': 'isabella.siu@mongodb.com', 'username': 'iwysiu'}Message: |
| Comment by Isabella Siu (Inactive) [ 15/Apr/21 ] |
|
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. |