Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Refactor misleading database errors #417

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/database/mongo_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func (db *MongoDatabase) Connect(host string) error {
return ErrConnection
}

dial_info.Timeout = 60 * time.Second
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this locally and it didn't seem to work. If the API can't make a connection to mongodb within 60 seconds, it should crash?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you running all services with make run? If so, can you check which services are still running after the mongo timeout happens? The gateway and the stats service should still be running, as these services do not access mongo and would not be terminated on connection failure. All other services should be terminated.

While on this topic, perhaps we should change run.sh to terminate all services if anyone exits with an error...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no, I tested it with make setup which (ideally) should crash immediately once it detects mongod is not running. I didn't realize this fix was for make run.

It still looks like those services aren't dying after missing the mongod connection:

matas      35633   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service user
matas      35634   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service registration
matas      35635   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service decision
matas      35636   35628  3 16:25 pts/1    00:00:08 /home/matas/Desktop/api/bin/hackillinois-api --service rsvp
matas      35637   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service checkin
matas      35638   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service upload
matas      35639   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service mail
matas      35640   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service event
matas      35642   35628  0 16:25 pts/1    00:00:00 /home/matas/Desktop/api/bin/hackillinois-api --service stat
matas      35647   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service notifications
matas      35651   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service project
matas      35656   35628  3 16:25 pts/1    00:00:09 /home/matas/Desktop/api/bin/hackillinois-api --service profile
matas      35661   35628  3 16:25 pts/1    00:00:08 /home/matas/Desktop/api/bin/hackillinois-api --service room
matas      35666   35628  0 16:25 pts/1    00:00:00 /home/matas/Desktop/api/bin/hackillinois-api --service gateway

(ignore the missing auth service, I killed it manually)

Let's go over this in tomorrow's meeting, maybe I am missing something simple

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion! But yes, let's go over this tomorrow.

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)
Expand Down
13 changes: 11 additions & 2 deletions common/errors/DatabaseError.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package errors

import "net/http"
import (
"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 {
http_status_error = http.StatusNotFound
}

return ApiError{Status: http_status_error, Type: "DATABASE_ERROR", Message: message, RawError: raw_error}
}
3 changes: 2 additions & 1 deletion services/project/main.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down