-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: error translator support (#162)
* feat: error translator support * feat: added duplicated key error * refactor: updated the file name to follow gorm file naming * refactor: updated the error parsing with native driver error --------- Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
- Loading branch information
Showing
3 changed files
with
25 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package postgres | ||
|
||
import ( | ||
"github.com/jackc/pgx/v5/pgconn" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var errCodes = map[string]string{ | ||
"uniqueConstraint": "23505", | ||
} | ||
|
||
func (dialector Dialector) Translate(err error) error { | ||
if pgErr, ok := err.(*pgconn.PgError); ok { | ||
if pgErr.Code == errCodes["uniqueConstraint"] { | ||
return gorm.ErrDuplicatedKey | ||
} | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters