3
0

Add proper handling of unique-constraint errors

This commit is contained in:
Denis Arh
2020-08-19 15:26:33 +02:00
parent 6bfb1eebd9
commit b0ea1da89f
22 changed files with 212 additions and 136 deletions

View File

@@ -4,8 +4,9 @@ import (
"context"
"fmt"
"github.com/Masterminds/squirrel"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/store/rdbms"
_ "github.com/lib/pq"
"github.com/lib/pq"
"go.uber.org/zap"
"net/url"
"strings"
@@ -25,6 +26,7 @@ func New(ctx context.Context, dsn string) (s *Store, err error) {
}
cfg.PlaceholderFormat = squirrel.Dollar
cfg.ErrorHandler = errorHandler
s = new(Store)
if s.Store, err = rdbms.New(ctx, cfg); err != nil {
@@ -68,3 +70,16 @@ func ProcDataSourceName(dsn string) (c *rdbms.Config, err error) {
DBName: strings.Trim(u.Path, "/"),
}, nil
}
func errorHandler(err error) error {
if err != nil {
if implErr, ok := err.(*pq.Error); ok {
switch implErr.Code.Name() {
case "unique_violation":
return store.ErrNotUnique.Wrap(implErr)
}
}
}
return err
}