Source File
error_translator.go
Belonging Package
gorm.io/driver/postgres
package postgres
import (
)
var errCodes = map[string]error{
"23505": gorm.ErrDuplicatedKey,
"23503": gorm.ErrForeignKeyViolated,
"42703": gorm.ErrInvalidField,
}
type ErrMessage struct {
Code string
Severity string
Message string
}
// Translate it will translate the error to native gorm errors.
// Since currently gorm supporting both pgx and pg drivers, only checking for pgx PgError types is not enough for translating errors, so we have additional error json marshal fallback.
func ( Dialector) ( error) error {
if , := .(*pgconn.PgError); {
if , := errCodes[.Code]; {
return
}
return
}
, := json.Marshal()
if != nil {
return
}
var ErrMessage
:= json.Unmarshal(, &)
if != nil {
return
}
if , := errCodes[.Code]; {
return
}
return
}
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |