Skip to content

Commit

Permalink
wip: fixing database return errors
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jun 18, 2024
1 parent 8b8b68b commit 45cbdb5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
39 changes: 28 additions & 11 deletions internal/adapters/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import (
"gorm.io/gorm"
)

// QueryError is an error that occurred while executing a query.
type QueryError struct {
Query string
Err error
}

// Error ...
func (e *QueryError) Error() string { return e.Query + ": " + e.Err.Error() }

// Unwrap ...
func (e *QueryError) Unwrap() error { return e.Err }

type database struct {
conn *gorm.DB
}
Expand Down Expand Up @@ -54,34 +66,39 @@ func (d *database) ReadWriteTx(ctx context.Context, fn func(context.Context, por
tx.Rollback()
}

if err := tx.Error; err != nil && !errors.Is(err, sql.ErrTxDone) {
return err
}

if err := tx.Commit().Error; err != nil {
if err := tx.Commit().Error; err != nil && !errors.Is(err, sql.ErrTxDone) {
return err
}

return nil
}

// NewQueryError returns a new QueryError.
func NewQueryError(query string, err error) *QueryError {
return &QueryError{
Query: query,
Err: err,
}
}

// ReadTx starts a read only transaction.
func (d *database) ReadTx(ctx context.Context, fn func(context.Context, ports.ReadTx) error) error {
tx := d.conn.WithContext(ctx).Begin()
if tx.Error != nil {
return tx.Error
return NewQueryError("begin read transaction", tx.Error)
}

if err := fn(ctx, &datastoreTx{tx}); err != nil {
err := fn(ctx, &datastoreTx{tx})
if err != nil {
tx.Rollback()
}

if err := tx.Error; err != nil && !errors.Is(err, sql.ErrTxDone) {
return err
if err := tx.Commit().Error; err != nil && !errors.Is(err, sql.ErrTxDone) {
return NewQueryError("commit read transaction", err)
}

if err := tx.Commit().Error; err != nil {
return err
if err != nil {
return NewQueryError("commit read transaction", err)
}

return nil
Expand Down
6 changes: 6 additions & 0 deletions internal/adapters/handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package handlers

import (
"context"
"errors"

"github.com/gofiber/fiber/v2"
"github.com/zeiss/knox/internal/controllers"
openapi "github.com/zeiss/knox/pkg/apis"
"github.com/zeiss/knox/pkg/dto"
"gorm.io/gorm"
)

var _ openapi.StrictServerInterface = (*apiHandlers)(nil)
Expand Down Expand Up @@ -208,6 +210,10 @@ func (a *apiHandlers) GetEnvironmentState(ctx context.Context, request openapi.G
query := dto.FromGetEnvironmentStateRequestObject(request)

data, err := a.state.GetState(ctx, query)
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { // the state was not found
return openapi.GetEnvironmentState404JSONResponse{}, nil
}

if err != nil {
return nil, fiber.NewError(fiber.StatusNotFound, err.Error())
}
Expand Down

0 comments on commit 45cbdb5

Please sign in to comment.