소스 검색

feat(api): fixed minor issue with error message when trying to create duplicate datasource, fixes #6164

Torkel Ödegaard 9 년 전
부모
커밋
a6918617ff
3개의 변경된 파일15개의 추가작업 그리고 1개의 파일을 삭제
  1. 5 0
      pkg/api/datasources.go
  2. 2 1
      pkg/models/datasource.go
  3. 8 0
      pkg/services/sqlstore/datasource.go

+ 5 - 0
pkg/api/datasources.go

@@ -92,6 +92,11 @@ func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
 	cmd.OrgId = c.OrgId
 
 	if err := bus.Dispatch(&cmd); err != nil {
+		if err == m.ErrDataSourceNameExists {
+			c.JsonApiErr(409, err.Error(), err)
+			return
+		}
+
 		c.JsonApiErr(500, "Failed to add datasource", err)
 		return
 	}

+ 2 - 1
pkg/models/datasource.go

@@ -22,7 +22,8 @@ const (
 
 // Typed errors
 var (
-	ErrDataSourceNotFound = errors.New("Data source not found")
+	ErrDataSourceNotFound   = errors.New("Data source not found")
+	ErrDataSourceNameExists = errors.New("Data source with same name already exists")
 )
 
 type DsAccess string

+ 8 - 0
pkg/services/sqlstore/datasource.go

@@ -60,6 +60,14 @@ func DeleteDataSource(cmd *m.DeleteDataSourceCommand) error {
 func AddDataSource(cmd *m.AddDataSourceCommand) error {
 
 	return inTransaction(func(sess *xorm.Session) error {
+
+		existing := m.DataSource{OrgId: cmd.OrgId, Name: cmd.Name}
+		has, _ := x.Get(&existing)
+
+		if has {
+			return m.ErrDataSourceNameExists
+		}
+
 		ds := &m.DataSource{
 			OrgId:             cmd.OrgId,
 			Name:              cmd.Name,