as_go126.go (571B)
1 //go:build go1.26 2 3 package pq 4 5 import ( 6 "errors" 7 "github.com/lib/pq/pqerror" 8 "slices" 9 ) 10 11 // As asserts that the given error is [pq.Error] and returns it, returning nil 12 // if it's not a pq.Error. 13 // 14 // It will return nil if the pq.Error is not one of the given error codes. If no 15 // codes are given it will always return the Error. 16 // 17 // This is safe to call with a nil error. 18 func As(err error, codes ...pqerror.Code) *Error { 19 if pqErr, ok := errors.AsType[*Error](err); ok && (len(codes) == 0 || slices.Contains(codes, pqErr.Code)) { 20 return pqErr 21 } 22 return nil 23 }