Skip to content

pg error code helpers #492

@jmhodges

Description

@jmhodges

I've written something like

const uniquenessViolation = pq.ErrorCode("23505")
...
if pgerr, ok := err.(*pq.Error); ok {
   if pgerr.Code == uniquenessViolation {
       return ErrSomeSpecificErrorForAPINiceness
   }
}

a few times in various packages.

Maybe I'm doing something wrong by doing this, but it might be nice to have some funcs named things like pq.IsUniquenessViolation(error) to help out on this sort of thing. It seems like we could generate them once from the errorCodeNames maps (while handling the "000" class cases by ignoring them or checking for the class or whichever way you'd like).

Does this sound like a patch you'd accept?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions