[GODRIVER-2952] Update context.Canceled equality comparisons to errors.Is Created: 22/Aug/23  Updated: 05/Dec/23  Resolved: 06/Oct/23

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.0.0, 1.13.1

Type: Improvement Priority: Major - P3
Reporter: Preston Vasquez Assignee: Steve Silvester
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-2156 Enable all remaining golangci-lint li... Closed
Scoping
is scoped by GODRIVER-2603 Use errors API from Go 1.13 Closed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

In the Go Driver, there are various places where we compare context.Canceled to an error using equality (==). It would be more robust to use errors.Is, in case the error we are comparing to is wrapped. Here is a Go Playground example to demonstrate this behavior:

package main
 
import (
	"context"
	"errors"
	"fmt"
)
 
func main() {
	// Wrap a context.Canceled error:
	err := fmt.Errorf("%w", context.Canceled)
 
	fmt.Println("using errors package: ", errors.Is(err, context.Canceled))
	fmt.Println("using direct comparison: ", err == context.Canceled)
}

Here is a non-exhaustive list of places where direct comparisons occur:

x/mongo/driver/topology/connection.go#L317

x/mongo/driver/topology/connection.go#L871

x/mongo/driver/topology/server.go#521

x/mongo/driver/topology/server.go#621



 Comments   
Comment by Githook User [ 05/Dec/23 ]

Author:

{'name': 'Preston Vasquez', 'email': 'prestonvasquez@icloud.com', 'username': 'prestonvasquez'}

Message: GODRIVER-2952 Update context.Canceled equality comparisons (#1490)

Co-authored-by: Ramit Mittal <commitemailoframit@protonmail.com>
Branch: release/1.13
https://github.com/mongodb/mongo-go-driver/commit/bcf3b868ba36e896135b0ca3e3d7c77a4e902440

Comment by Githook User [ 06/Oct/23 ]

Author:

{'name': 'Ramit Mittal', 'email': 'commitemailoframit@protonmail.com', 'username': 'ramitmittal'}

Message: GODRIVER-2952 Update context.Canceled equality comparisons to errors.Is (#1413)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/9ca46ea399dff76b8f6123c19297be6bf762373c

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