Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2952

Update context.Canceled equality comparisons to errors.Is

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.0.0, 1.13.1
    • Affects Version/s: None
    • Component/s: None
    • None
    • Hide

      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?

      Show
      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?

      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

            Assignee:
            steve.silvester@mongodb.com Steve Silvester
            Reporter:
            preston.vasquez@mongodb.com Preston Vasquez
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: