Skip to content

proposal: errors: In() to check Is against multiple errors #70961

@koaim

Description

@koaim

Proposal Details

Problem
The code looks redundant and cumbersome in cases when you need to check an error match several times. Example:

func foo() {
  err := bar()
  if errors.Is(err, err1) || errors.Is(err, err2) || errors.Is(err, err3) {
    // some logic
  }
}

Solution
A new function that will allow you to check for errors like this:

func foo() {
  err := bar()
  if errors.In(err, err1, err2, err3) {
    // some logic
  }
}

Implementation

package errors

func In(err error, target ...error) bool {
  for _, v := range target {
    if errors.Is(err, v) {
      return true
    }
  }

  return false
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions