From 94af70b112cb8bc06d5a3efef7212904fdcb11a3 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 20 Apr 2022 20:20:11 +0200 Subject: [PATCH 1/2] Ignore "IndexNotFound" errors and log the rest without panicking. --- database/database.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/database/database.go b/database/database.go index 77c86d36..4587ddc6 100644 --- a/database/database.go +++ b/database/database.go @@ -199,9 +199,8 @@ func ensureDBSchema(ctx context.Context, db *mongo.Database, schema map[string][ } // Drop indexes we no longer need. _, err = db.Collection(collUsers).Indexes().DropOne(ctx, "email_unique") - // Ignore namespace not found errors. - if err != nil && !strings.Contains(err.Error(), "NamespaceNotFound") { - return err + if err != nil && !strings.Contains(err.Error(), "IndexNotFound") { + log.Debugf("Error while dropping index '%s': %v", "email_unique", err) } // Ensure current schema. for collName, models := range schema { From 9be7072d286ccdc6c22f01dc6e43c7ce4fc1bc49 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Thu, 21 Apr 2022 10:58:12 +0200 Subject: [PATCH 2/2] Ignore `NamespaceNotFound` errors while dropping indexes and add some comments explaining why we're handling errors like that. --- database/database.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/database/database.go b/database/database.go index 4587ddc6..23d3511e 100644 --- a/database/database.go +++ b/database/database.go @@ -199,7 +199,17 @@ func ensureDBSchema(ctx context.Context, db *mongo.Database, schema map[string][ } // Drop indexes we no longer need. _, err = db.Collection(collUsers).Indexes().DropOne(ctx, "email_unique") - if err != nil && !strings.Contains(err.Error(), "IndexNotFound") { + // We want to ignore IndexNotFound errors - we'll have that each time we run + // this code after the initial run on which we drop the index. + // We also want to ignore NamespaceNotFound errors - we'll have that on the + // very first run of the service when users collection doesn't exist, yet. + // We don't want to worry new portal operators and waste their time. + // All other errors we want to log for informational purposes but we don't + // want to return an error and prevent the service from running - if there + // is any issue with the database that would affect the operation of the + // service, it will surface during the next step where we ensure collections + // indexes exist. + if err != nil && !strings.Contains(err.Error(), "IndexNotFound") && !strings.Contains(err.Error(), "NamespaceNotFound") { log.Debugf("Error while dropping index '%s': %v", "email_unique", err) } // Ensure current schema.