From af7730787bf6dda769c91fb4ec0b11b83174e4c4 Mon Sep 17 00:00:00 2001 From: Tan Yong Zhi Date: Tue, 26 Oct 2021 00:10:11 -0500 Subject: [PATCH 1/2] Fix misleading database errors --- common/database/mongo_database.go | 2 +- common/errors/DatabaseError.go | 15 +++++++++++++-- services/project/main.go | 3 ++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/common/database/mongo_database.go b/common/database/mongo_database.go index 223d9df3..7a45b90a 100644 --- a/common/database/mongo_database.go +++ b/common/database/mongo_database.go @@ -44,13 +44,13 @@ func (db *MongoDatabase) Connect(host string) error { return ErrConnection } + dial_info.Timeout = 60 * time.Second if config.IS_PRODUCTION { dial_info.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) { tls_config := &tls.Config{} connection, err := tls.Dial("tcp", addr.String(), tls_config) return connection, err } - dial_info.Timeout = 60 * time.Second } session, err := mgo.DialWithInfo(dial_info) diff --git a/common/errors/DatabaseError.go b/common/errors/DatabaseError.go index 7d032b1a..9d47f688 100644 --- a/common/errors/DatabaseError.go +++ b/common/errors/DatabaseError.go @@ -1,8 +1,19 @@ package errors -import "net/http" +import ( + "fmt" + "net/http" +) + +const MONGO_NOT_FOUND_ERR = "Error: NOT_FOUND" // An error that occurs when a database operation (e.g. fetch / insert / update) doesn't work. func DatabaseError(raw_error string, message string) ApiError { - return ApiError{Status: http.StatusInternalServerError, Type: "DATABASE_ERROR", Message: message, RawError: raw_error} + http_status_error := http.StatusInternalServerError + if raw_error == MONGO_NOT_FOUND_ERR { + fmt.Println(raw_error) + http_status_error = http.StatusNotFound + } + + return ApiError{Status: http_status_error, Type: "DATABASE_ERROR", Message: message, RawError: raw_error} } diff --git a/services/project/main.go b/services/project/main.go index cb127762..5b12881f 100644 --- a/services/project/main.go +++ b/services/project/main.go @@ -1,12 +1,13 @@ package project import ( + "log" + "github.com/HackIllinois/api/common/apiserver" "github.com/HackIllinois/api/services/project/config" "github.com/HackIllinois/api/services/project/controller" "github.com/HackIllinois/api/services/project/service" "github.com/gorilla/mux" - "log" ) func Initialize() error { From 6356352f3d0d9e10a1779e00285c6a936709799b Mon Sep 17 00:00:00 2001 From: Tan Yong Zhi Date: Tue, 26 Oct 2021 00:15:11 -0500 Subject: [PATCH 2/2] Remove debugging print msg --- common/errors/DatabaseError.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/common/errors/DatabaseError.go b/common/errors/DatabaseError.go index 9d47f688..e6cc910e 100644 --- a/common/errors/DatabaseError.go +++ b/common/errors/DatabaseError.go @@ -1,7 +1,6 @@ package errors import ( - "fmt" "net/http" ) @@ -11,7 +10,6 @@ const MONGO_NOT_FOUND_ERR = "Error: NOT_FOUND" func DatabaseError(raw_error string, message string) ApiError { http_status_error := http.StatusInternalServerError if raw_error == MONGO_NOT_FOUND_ERR { - fmt.Println(raw_error) http_status_error = http.StatusNotFound }