refactor(api): sqlite error check uses new for type assertion

This commit is contained in:
Tigor Hutasuhut 2024-08-05 23:20:07 +07:00
parent b23ac7617e
commit efd2b6b187
2 changed files with 3 additions and 4 deletions

View file

@ -33,8 +33,7 @@ func (api *API) DevicesCreate(ctx context.Context, params *models.Device) (devic
})
})
if err != nil {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
if sqliteErr := new(sqlite3.Error); errors.As(err, sqliteErr) {
if sqliteErr.Code == sqlite3.ErrConstraint {
return nil, errs.
Wrapw(sqliteErr, "device already exists", "params", params).

View file

@ -12,10 +12,10 @@ func (api *API) GetDevice(ctx context.Context, slug string) (device *models.Devi
device, err = models.FindDevice(ctx, api.db, slug)
if err != nil {
if err.Error() == "sql: no rows in result set" {
return device, errs.Wrapw(err, "device not found", "device", device).Code(connect.CodeNotFound)
return device, errs.Wrapw(err, "device not found", "slug", slug).Code(connect.CodeNotFound)
}
return device, errs.Wrapw(err, "failed to find device", "device", device)
return device, errs.Wrapw(err, "failed to find device", "slug", slug)
}
return device, nil