package api import ( "context" "errors" "github.com/mattn/go-sqlite3" "github.com/tigorlazuardi/bluemage/go/gen/models" "github.com/tigorlazuardi/bluemage/go/pkg/errs" "github.com/tigorlazuardi/bluemage/go/pkg/log" "github.com/tigorlazuardi/bluemage/go/pkg/telemetry" ) func (api *API) DevicesUpdate(ctx context.Context, slug string, update *models.DeviceSetter) (err error) { ctx, span := tracer.Start(ctx, "DevicesUpdate") defer func() { telemetry.EndWithStatus(span, err) }() exist, err := api.DevicesExist(ctx, slug) if err != nil { return err } if !exist { err = errs.Failf("device with slug %q does not exist", slug) return err } ctx, coll := log.WithQueryCollector(ctx) target := &models.Device{Slug: slug} api.lockf(func() { err = models.Devices.Update(ctx, api.Executor, update, target) }) if err != nil { if sqlite3Err := new(sqlite3.Error); errors.As(err, &sqlite3Err) { if sqlite3Err.Code == sqlite3.ErrConstraint { err = errs. Wrapw( err, "a device with the same slug already exists", "slug", slug, "update", update, "query", coll, ) return err } } err = errs.Wrapw(err, "failed to update device", "slug", slug, "update", update, "query", coll) return err } return nil }