You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2021/04/12 11:27:36 UTC

[servicecomb-service-center] 01/01: SCB-2176 Fix: failed to connect mongo

This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a commit to branch test
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git

commit 7d0b7ba5fc4aff3dd420beed34a87f517ae06da3
Author: little-cui <su...@qq.com>
AuthorDate: Mon Apr 12 19:27:16 2021 +0800

    SCB-2176 Fix: failed to connect mongo
---
 datasource/mongo/client/errortypes.go |  6 ++++--
 datasource/mongo/mongo.go             | 11 +++++------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/datasource/mongo/client/errortypes.go b/datasource/mongo/client/errortypes.go
index 759315f..64dd599 100644
--- a/datasource/mongo/client/errortypes.go
+++ b/datasource/mongo/client/errortypes.go
@@ -20,8 +20,10 @@ import (
 )
 
 const (
-	DuplicateKey      = 11000
-	CollectionsExists = 48
+	DuplicateKey = 11000
+
+	MsgDBExists  = "already exists"
+	MsgDuplicate = "duplicate key error collection"
 )
 
 func IsDuplicateKey(err error) bool {
diff --git a/datasource/mongo/mongo.go b/datasource/mongo/mongo.go
index 8addf8f..1c34550 100644
--- a/datasource/mongo/mongo.go
+++ b/datasource/mongo/mongo.go
@@ -21,6 +21,7 @@ import (
 	"context"
 	"fmt"
 	"github.com/apache/servicecomb-service-center/pkg/util"
+	"strings"
 
 	"github.com/go-chassis/go-chassis/v2/storage"
 	"go.mongodb.org/mongo-driver/mongo"
@@ -210,9 +211,8 @@ func EnsureDep() {
 
 func wrapCreateCollectionError(err error) {
 	if err != nil {
-		// commandError can be returned by any operation
-		cmdErr, ok := err.(mongo.CommandError)
-		if ok && cmdErr.Code == client.CollectionsExists {
+		if strings.Contains(err.Error(), client.MsgDBExists) {
+			log.Info(fmt.Sprintf("failed to create collection, %s", err))
 			return
 		}
 		log.Fatal(fmt.Sprintf("failed to create collection with validation, err type: %s", util.Reflect(err).FullName), err)
@@ -221,9 +221,8 @@ func wrapCreateCollectionError(err error) {
 
 func wrapCreateIndexesError(err error) {
 	if err != nil {
-		// commandError can be returned by any operation
-		cmdErr, ok := err.(mongo.CommandError)
-		if ok && cmdErr.Code == client.DuplicateKey {
+		if strings.Contains(err.Error(), client.MsgDuplicate) {
+			log.Info(fmt.Sprintf("failed to create indexes, %s", err))
 			return
 		}
 		log.Fatal(fmt.Sprintf("failed to create indexes, err type: %s", util.Reflect(err).FullName), err)