Manager: wrap Worker fetching errors
Do the same wrapping as for task/job errors, but then for workers.
This commit is contained in:
parent
6e3667225a
commit
1496736f7a
@ -9,8 +9,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrJobNotFound = PersistenceError{Message: "job not found", Err: gorm.ErrRecordNotFound}
|
||||
ErrTaskNotFound = PersistenceError{Message: "task not found", Err: gorm.ErrRecordNotFound}
|
||||
ErrJobNotFound = PersistenceError{Message: "job not found", Err: gorm.ErrRecordNotFound}
|
||||
ErrTaskNotFound = PersistenceError{Message: "task not found", Err: gorm.ErrRecordNotFound}
|
||||
ErrWorkerNotFound = PersistenceError{Message: "worker not found", Err: gorm.ErrRecordNotFound}
|
||||
)
|
||||
|
||||
type PersistenceError struct {
|
||||
@ -34,6 +35,10 @@ func taskError(errorToWrap error, message string, msgArgs ...interface{}) error
|
||||
return wrapError(translateGormTaskError(errorToWrap), message, msgArgs...)
|
||||
}
|
||||
|
||||
func workerError(errorToWrap error, message string, msgArgs ...interface{}) error {
|
||||
return wrapError(translateGormWorkerError(errorToWrap), message, msgArgs...)
|
||||
}
|
||||
|
||||
func wrapError(errorToWrap error, message string, format ...interface{}) error {
|
||||
// Only format if there are arguments for formatting.
|
||||
var formattedMsg string
|
||||
@ -66,3 +71,12 @@ func translateGormTaskError(gormError error) error {
|
||||
}
|
||||
return gormError
|
||||
}
|
||||
|
||||
// translateGormWorkerError translates a Gorm error to a persistence layer error.
|
||||
// This helps to keep Gorm as "implementation detail" of the persistence layer.
|
||||
func translateGormWorkerError(gormError error) error {
|
||||
if errors.Is(gormError, gorm.ErrRecordNotFound) {
|
||||
return ErrWorkerNotFound
|
||||
}
|
||||
return gormError
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ func (db *DB) FetchWorker(ctx context.Context, uuid string) (*Worker, error) {
|
||||
tx := db.gormDB.WithContext(ctx).
|
||||
First(&w, "uuid = ?", uuid)
|
||||
if tx.Error != nil {
|
||||
return nil, tx.Error
|
||||
return nil, workerError(tx.Error, "fetching worker")
|
||||
}
|
||||
return &w, nil
|
||||
}
|
||||
@ -53,7 +53,7 @@ func (db *DB) FetchWorkers(ctx context.Context) ([]*Worker, error) {
|
||||
workers := make([]*Worker, 0)
|
||||
tx := db.gormDB.WithContext(ctx).Model(&Worker{}).Scan(&workers)
|
||||
if tx.Error != nil {
|
||||
return nil, tx.Error
|
||||
return nil, workerError(tx.Error, "fetching all workers")
|
||||
}
|
||||
return workers, nil
|
||||
}
|
||||
|
@ -17,6 +17,12 @@ func TestCreateFetchWorker(t *testing.T) {
|
||||
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Test fetching non-existent worker
|
||||
fetchedWorker, err := db.FetchWorker(ctx, "dabf67a1-b591-4232-bf73-0b8de2a9488e")
|
||||
assert.ErrorIs(t, err, ErrWorkerNotFound)
|
||||
assert.Nil(t, fetchedWorker)
|
||||
|
||||
// Test existing worker
|
||||
w := Worker{
|
||||
UUID: uuid.New(),
|
||||
Name: "дрон",
|
||||
@ -27,10 +33,10 @@ func TestCreateFetchWorker(t *testing.T) {
|
||||
SupportedTaskTypes: "blender,ffmpeg,file-management",
|
||||
}
|
||||
|
||||
err := db.CreateWorker(ctx, &w)
|
||||
err = db.CreateWorker(ctx, &w)
|
||||
assert.NoError(t, err)
|
||||
|
||||
fetchedWorker, err := db.FetchWorker(ctx, w.UUID)
|
||||
fetchedWorker, err = db.FetchWorker(ctx, w.UUID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, fetchedWorker)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user