Skip to content

Commit a691861

Browse files
committed
feat(api): fixed minor issue with error message when trying to create duplicate datasource, fixes grafana#6164
1 parent 4f22635 commit a691861

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

pkg/api/datasources.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
9292
cmd.OrgId = c.OrgId
9393

9494
if err := bus.Dispatch(&cmd); err != nil {
95+
if err == m.ErrDataSourceNameExists {
96+
c.JsonApiErr(409, err.Error(), err)
97+
return
98+
}
99+
95100
c.JsonApiErr(500, "Failed to add datasource", err)
96101
return
97102
}

pkg/models/datasource.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const (
2222

2323
// Typed errors
2424
var (
25-
ErrDataSourceNotFound = errors.New("Data source not found")
25+
ErrDataSourceNotFound = errors.New("Data source not found")
26+
ErrDataSourceNameExists = errors.New("Data source with same name already exists")
2627
)
2728

2829
type DsAccess string

pkg/services/sqlstore/datasource.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func DeleteDataSource(cmd *m.DeleteDataSourceCommand) error {
6060
func AddDataSource(cmd *m.AddDataSourceCommand) error {
6161

6262
return inTransaction(func(sess *xorm.Session) error {
63+
64+
existing := m.DataSource{OrgId: cmd.OrgId, Name: cmd.Name}
65+
has, _ := x.Get(&existing)
66+
67+
if has {
68+
return m.ErrDataSourceNameExists
69+
}
70+
6371
ds := &m.DataSource{
6472
OrgId: cmd.OrgId,
6573
Name: cmd.Name,

0 commit comments

Comments
 (0)