fix(alias): return user-friendly error when alias value is invalid (#6036)

pull/6042/head
Irena Rindos 6 months ago committed by GitHub
parent afc02f1b6b
commit ecef93615e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -83,6 +83,9 @@ func (r *Repository) CreateAlias(ctx context.Context, a *Alias, opt ...Option) (
if strings.Contains(err.Error(), `violates foreign key constraint "target_fkey"`) {
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.NotFound), errors.WithMsg("target with specified destination id %q was not found", a.GetDestinationId()))
}
if strings.Contains(err.Error(), `wt_target_alias_value_shape`) {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("alias value %q contains invalid characters", a.Value)))
}
return nil, errors.Wrap(ctx, err, op)
}
return newAlias, nil
@ -181,6 +184,9 @@ func (r *Repository) UpdateAlias(ctx context.Context, a *Alias, version uint32,
if strings.Contains(err.Error(), `violates foreign key constraint "target_fkey"`) {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithCode(errors.NotFound), errors.WithMsg("target with specified destination id %q was not found", a.GetDestinationId()))
}
if strings.Contains(err.Error(), `wt_target_alias_value_shape`) {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("alias value contains invalid characters"))
}
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
}

@ -75,6 +75,16 @@ func TestRepository_CreateAlias(t *testing.T) {
},
errContains: "public id not empty",
},
{
name: "invalid-alias-value",
in: &target.Alias{
Alias: &store.Alias{
ScopeId: "global",
Value: "invalid_alias",
},
},
errContains: "contains invalid characters",
},
{
name: "valid-with-value",
in: &target.Alias{
@ -748,6 +758,17 @@ func TestRepository_UpdateAlias(t *testing.T) {
assert.Nil(t, got2)
assert.Equal(t, db.NoRowsAffected, gotCount2, "row count")
})
t.Run("invalid-alias", func(t *testing.T) {
value := "invalid_alais"
c1 := target.TestAlias(t, db.New(conn), "test")
c1.Value = value
got1, gotCount1, err := repo.UpdateAlias(context.Background(), c1, 1, []string{"value"})
assert.Error(t, err)
assert.ErrorContains(t, err, "contains invalid characters")
assert.Nil(t, got1)
assert.Equal(t, db.NoRowsAffected, gotCount1, "row count")
})
}
func TestRepository_LookupAlias(t *testing.T) {

Loading…
Cancel
Save