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
@ -11,6 +11,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
ErrJobNotFound = PersistenceError{Message: "job not found", Err: gorm.ErrRecordNotFound}
|
ErrJobNotFound = PersistenceError{Message: "job not found", Err: gorm.ErrRecordNotFound}
|
||||||
ErrTaskNotFound = PersistenceError{Message: "task 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 {
|
type PersistenceError struct {
|
||||||
@ -34,6 +35,10 @@ func taskError(errorToWrap error, message string, msgArgs ...interface{}) error
|
|||||||
return wrapError(translateGormTaskError(errorToWrap), message, msgArgs...)
|
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 {
|
func wrapError(errorToWrap error, message string, format ...interface{}) error {
|
||||||
// Only format if there are arguments for formatting.
|
// Only format if there are arguments for formatting.
|
||||||
var formattedMsg string
|
var formattedMsg string
|
||||||
@ -66,3 +71,12 @@ func translateGormTaskError(gormError error) error {
|
|||||||
}
|
}
|
||||||
return gormError
|
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).
|
tx := db.gormDB.WithContext(ctx).
|
||||||
First(&w, "uuid = ?", uuid)
|
First(&w, "uuid = ?", uuid)
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
return nil, tx.Error
|
return nil, workerError(tx.Error, "fetching worker")
|
||||||
}
|
}
|
||||||
return &w, nil
|
return &w, nil
|
||||||
}
|
}
|
||||||
@ -53,7 +53,7 @@ func (db *DB) FetchWorkers(ctx context.Context) ([]*Worker, error) {
|
|||||||
workers := make([]*Worker, 0)
|
workers := make([]*Worker, 0)
|
||||||
tx := db.gormDB.WithContext(ctx).Model(&Worker{}).Scan(&workers)
|
tx := db.gormDB.WithContext(ctx).Model(&Worker{}).Scan(&workers)
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
return nil, tx.Error
|
return nil, workerError(tx.Error, "fetching all workers")
|
||||||
}
|
}
|
||||||
return workers, nil
|
return workers, nil
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,12 @@ func TestCreateFetchWorker(t *testing.T) {
|
|||||||
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
|
ctx, cancel, db := persistenceTestFixtures(t, 1*time.Second)
|
||||||
defer cancel()
|
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{
|
w := Worker{
|
||||||
UUID: uuid.New(),
|
UUID: uuid.New(),
|
||||||
Name: "дрон",
|
Name: "дрон",
|
||||||
@ -27,10 +33,10 @@ func TestCreateFetchWorker(t *testing.T) {
|
|||||||
SupportedTaskTypes: "blender,ffmpeg,file-management",
|
SupportedTaskTypes: "blender,ffmpeg,file-management",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := db.CreateWorker(ctx, &w)
|
err = db.CreateWorker(ctx, &w)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
fetchedWorker, err := db.FetchWorker(ctx, w.UUID)
|
fetchedWorker, err = db.FetchWorker(ctx, w.UUID)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, fetchedWorker)
|
assert.NotNil(t, fetchedWorker)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user