Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature sqlstate support #215

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions error_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
var errCodes = map[string]error{
"23505": gorm.ErrDuplicatedKey,
"23503": gorm.ErrForeignKeyViolated,
"42703": gorm.ErrInvalidField,
}

type ErrMessage struct {
Code string `json:"Code"`
Severity string `json:"Severity"`
Message string `json:"Message"`
Code string
Severity string
Message string
}

// Translate it will translate the error to native gorm errors.
Expand Down
8 changes: 7 additions & 1 deletion error_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package postgres

import (
"errors"
"testing"

"github.com/jackc/pgx/v5/pgconn"
"gorm.io/gorm"
"testing"
)

func TestDialector_Translate(t *testing.T) {
Expand All @@ -30,6 +31,11 @@ func TestDialector_Translate(t *testing.T) {
args: args{err: &pgconn.PgError{Code: "23503"}},
want: gorm.ErrForeignKeyViolated,
},
{
name: "it should return gorm.ErrInvalidField error if the status code is 42703",
args: args{err: &pgconn.PgError{Code: "42703"}},
want: gorm.ErrInvalidField,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading