Some error handling work

This commit is contained in:
simon987 2020-01-03 14:13:39 -05:00
parent 0ea6e18fb9
commit 15c4090a94
13 changed files with 1615 additions and 2850 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea/
*.iml
*.iml
log.txt

View File

@ -138,6 +138,13 @@ func (api *WebAPI) GetLog(r *Request) {
logs := api.Database.GetLogs(req.Since, req.Level)
if logs == nil {
r.Json(JsonResponse{
Ok: false,
}, 500)
return
}
logrus.WithFields(logrus.Fields{
"getLogRequest": req,
"logCount": len(*logs),

View File

@ -306,6 +306,13 @@ func (api *WebAPI) GetProjectList(r *Request) {
projects := api.Database.GetAllProjects(id)
if projects == nil {
r.Json(JsonResponse{
Ok: false,
}, 500)
return
}
r.OkJson(JsonResponse{
Ok: true,
Content: GetProjectListResponse{

View File

@ -4,6 +4,6 @@ import "github.com/sirupsen/logrus"
func handleErr(err error) {
if err != nil {
logrus.WithError(err).Fatal("Error during database operation!")
logrus.WithError(err).Error("Error during database operation!")
}
}

View File

@ -81,6 +81,9 @@ func (database *Database) GetLogs(since int64, level LogLevel) *[]LogEntry {
rows, err := db.Query("SELECT * FROM log_entry WHERE timestamp > $1 AND level=$2",
since, level)
handleErr(err)
if err != nil {
return nil
}
for rows.Next() {

View File

@ -36,11 +36,17 @@ func (database *Database) MakeProjectSnapshots() {
extract(epoch from now() at time zone 'utc')
FROM project`)
handleErr(err)
if err != nil {
return
}
inserted, _ := insertRes.RowsAffected()
res, err := db.Exec(`DELETE FROM project_monitoring_snapshot WHERE timestamp < $1`,
int64(time.Now().Unix())-int64(config.Cfg.MonitoringHistory.Seconds()))
handleErr(err)
if err != nil {
return
}
deleted, _ := res.RowsAffected()
logrus.WithFields(logrus.Fields{
@ -60,6 +66,9 @@ func (database *Database) GetMonitoringSnapshotsBetween(pid int64, from int, to
worker_access_count, awaiting_verification_task_count, timestamp FROM project_monitoring_snapshot
WHERE project=$1 AND timestamp BETWEEN $2 AND $3 ORDER BY TIMESTAMP DESC `, pid, from, to)
handleErr(err)
if err != nil {
return
}
for rows.Next() {
@ -91,6 +100,9 @@ func (database *Database) GetNMonitoringSnapshots(pid int64, count int) (ss *[]P
worker_access_count, awaiting_verification_task_count, timestamp FROM project_monitoring_snapshot
WHERE project=$1 ORDER BY TIMESTAMP DESC LIMIT $2`, pid, count)
handleErr(err)
if err != nil {
return
}
for rows.Next() {
s := ProjectMonitoringSnapshot{}

View File

@ -168,6 +168,9 @@ func (database Database) GetAllProjects(managerId int64) *[]Project {
ORDER BY name`, managerId)
}
handleErr(err)
if err != nil {
return nil
}
for rows.Next() {
p := Project{}

View File

@ -156,6 +156,9 @@ func getSessionCtx(username string, password string, admin bool) *http.Client {
if admin {
manager, _ := testApi.Database.ValidateCredentials([]byte(username), []byte(password))
if manager == nil {
return nil
}
manager.WebsiteAdmin = true
testApi.Database.UpdateManager(manager)
}

View File

@ -59,7 +59,7 @@ func TestWebHookDontUpdateVersion(t *testing.T) {
getResp := getProjectAsAdmin(resp.Id).Content
if getResp.Project.Version != "old" {
if getResp.Project == nil || getResp.Project.Version != "old" {
t.Error()
}
}
@ -93,7 +93,7 @@ func TestWebHookUpdateVersion(t *testing.T) {
getResp := getProjectAsAdmin(resp.Id).Content
if getResp.Project.Version != "new" {
if getResp.Project == nil || getResp.Project.Version != "new" {
t.Error()
}
}

View File

@ -139,8 +139,9 @@ func TestGetLogs(t *testing.T) {
t.Error()
}
if len(*r.Content.Logs) <= 0 {
if r.Content.Logs == nil || len(*r.Content.Logs) <= 0 {
t.Error()
return
}
debugFound := false

View File

@ -37,12 +37,13 @@ func TestCreateGetProject(t *testing.T) {
getResp := getProjectAsAdmin(id).Content
if getResp.Project.Id != id {
if getResp.Project == nil || getResp.Project.Id != id {
t.Error()
}
if getResp.Project.Name != "Test name" {
if getResp.Project == nil || getResp.Project.Name != "Test name" {
t.Error()
return
}
if getResp.Project.Version != "Test Version" {
@ -147,8 +148,9 @@ func TestUpdateProjectValid(t *testing.T) {
proj := getProjectAsAdmin(pid).Content
if proj.Project.Public != false {
if proj.Project == nil || proj.Project.Public != false {
t.Error()
return
}
if proj.Project.Motd != "MotdB" {
t.Error()
@ -314,7 +316,8 @@ func TestHiddenProjectsNotShownInList(t *testing.T) {
}, testUserCtx)
if r.Ok != true {
t.Error()
t.Fail()
return
}
list := getProjectList(nil)
@ -417,6 +420,11 @@ func TestUserWithReadAccessShouldSeeHiddenProjectInList(t *testing.T) {
list := getProjectList(testUserCtx)
if list.Content.Projects == nil {
t.Fail()
return
}
found := false
for _, p := range *list.Content.Projects {
if p.Id == pHidden.Content.Id {
@ -441,6 +449,11 @@ func TestAdminShouldSeeHiddenProjectInList(t *testing.T) {
list := getProjectList(testAdminCtx)
if list.Content.Projects == nil {
t.Fail()
return
}
found := false
for _, p := range *list.Content.Projects {
if p.Id == pHidden.Content.Id {

File diff suppressed because it is too large Load Diff

View File

@ -32,7 +32,7 @@
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.803.21",
"@angular-devkit/build-angular": "^0.13.4",
"@angular/cli": "^7.2.4",
"@angular/compiler-cli": "^7.2.6",
"@angular/language-service": "~7.2.0",