Home > database >  How do I check if an error is strconv.NumError using errors.Is
How do I check if an error is strconv.NumError using errors.Is

Time:11-18

I have this error

enter image description here

The error is of type ParseInt. How do I check for this error I am assuming I would use errors.Is but not sure how I would do it for this case

CodePudding user response:

https://pkg.go.dev/[email protected]#NumError

type NumError struct {
    Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat, ParseComplex)
    Num  string // the input
    Err  error  // the reason the conversion failed (e.g. ErrRange, ErrSyntax, etc.)
}

The error is of type ParseInt.

"ParseInt" is the name of the "failing function", the one that returned the error. The actual error type is *strconv.NumError. You can check for that and the func name like so:

if e, ok := err.(*strconv.NumError); ok && e.Func == "ParseInt" {
    // do xyz
}
  • Related