Manage errors

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-10-29 14:03:50 +01:00
parent 55743cbb5c
commit 9a33f7b771
84 changed files with 1483 additions and 331 deletions

View File

@@ -16,6 +16,7 @@ package coredata
import (
"context"
"errors"
"fmt"
"maps"
"time"
@@ -23,6 +24,7 @@ import (
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"go.gearno.de/kit/pg"
)
@@ -42,12 +44,20 @@ type (
TrustCenterReferences []*TrustCenterReference
ErrTrustCenterReferenceNotFound struct {
ID string
Identifier string
}
ErrTrustCenterReferenceAlreadyExists struct {
message string
}
)
func (e ErrTrustCenterReferenceNotFound) Error() string {
return fmt.Sprintf("trust center reference not found: %s", e.ID)
return fmt.Sprintf("trust center reference not found: %q", e.Identifier)
}
func (e ErrTrustCenterReferenceAlreadyExists) Error() string {
return e.message
}
func (t TrustCenterReference) CursorKey(orderBy TrustCenterReferenceOrderField) page.CursorKey {
@@ -156,6 +166,14 @@ RETURNING rank;
err := conn.QueryRow(ctx, q, args).Scan(&t.Rank)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code == "23505" && pgErr.ConstraintName == "trust_center_references_trust_center_id_rank_key" {
return &ErrTrustCenterReferenceAlreadyExists{
message: fmt.Sprintf("trust center reference with trust_center_id %s and rank already exists", t.TrustCenterID),
}
}
}
return fmt.Errorf("cannot insert trust center reference: %w", err)
}
@@ -198,7 +216,7 @@ WHERE
}
if result.RowsAffected() == 0 {
return ErrTrustCenterReferenceNotFound{ID: t.ID.String()}
return ErrTrustCenterReferenceNotFound{Identifier: t.ID.String()}
}
return nil