~xenrox/10man-api

3a8a4ff67ddf95f0c03ccc77351f665a73cb7cf4 — Thorben Günther 10 months ago c55b1fa
Improve error handling further.
2 files changed, 8 insertions(+), 3 deletions(-)

M database/error.go
M graph/schema.resolvers.go
M database/error.go => database/error.go +6 -1
@@ 1,6 1,7 @@
package database

import (
	"database/sql"
	"errors"

	"github.com/lib/pq"


@@ 10,10 11,14 @@ import (
// that can be easier parsed and handled
func CheckErrorCode(err error) error {
	// https://www.postgresql.org/docs/current/errcodes-appendix.html
	if errors.Is(err, sql.ErrNoRows) {
		return errors.New("no result")
	}

	if err, ok := err.(*pq.Error); ok {
		// duplicate key value violates unique constraint: 23505 unique_violation
		if err.Code == "23505" {
			return errors.New("NotUnique")
			return errors.New("not unique")
		}
	}
	return err

M graph/schema.resolvers.go => graph/schema.resolvers.go +2 -2
@@ 401,7 401,7 @@ func (r *queryResolver) UserBySteam(ctx context.Context, steamID string) (*model
	err := database.DB.QueryRow(query, steamID).Scan(&user.ID,
		&user.TeamspeakID, &user.Elo, &user.Admin, &user.Avatar, &user.Name)
	if err != nil {
		return nil, err
		return nil, database.CheckErrorCode(err)
	}

	return &user, nil


@@ 418,7 418,7 @@ func (r *queryResolver) UserByTs(ctx context.Context, teamspeakID string) (*mode
	err := database.DB.QueryRow(query, teamspeakID).Scan(&user.ID,
		&user.SteamID, &user.Elo, &user.Admin, &user.Avatar, &user.Name)
	if err != nil {
		return nil, err
		return nil, database.CheckErrorCode(err)
	}

	return &user, nil