summary refs log tree commit diff stats
path: root/svc
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-06 18:16:29 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-08 02:29:23 -0400
commit6c1b09bbeb1a7268a0d590e0a20ab2d34b395c8f (patch)
tree2c843adf344830510ff517f344b3af451955f98c /svc
parent5fa9ec1b63acc6ac9dc0a8b5d2f8f044c27ea45e (diff)
downloadgetwtxt-6c1b09bbeb1a7268a0d590e0a20ab2d34b395c8f.tar.gz
cleaned up error handling
Diffstat (limited to 'svc')
-rw-r--r--svc/cache.go30
-rw-r--r--svc/db.go21
-rw-r--r--svc/handlers.go5
-rw-r--r--svc/init.go18
-rw-r--r--svc/post.go9
-rw-r--r--svc/query.go4
-rw-r--r--svc/svc.go4
7 files changed, 34 insertions, 57 deletions
diff --git a/svc/cache.go b/svc/cache.go
index 6b50a0e..04ed2cd 100644
--- a/svc/cache.go
+++ b/svc/cache.go
@@ -3,7 +3,6 @@ package svc // import "github.com/getwtxt/getwtxt/svc"
 import (
 	"bytes"
 	"io/ioutil"
-	"log"
 	"os"
 	"sync"
 	"time"
@@ -41,9 +40,8 @@ func cacheAndPush() {
 			refreshCache()
 		}
 		if dbTimer() {
-			if err := pushDB(); err != nil {
-				log.Printf("Error pushing cache to database: %v\n", err.Error())
-			}
+			err := pushDB()
+			errLog("Error pushing cache to database: ", err)
 		}
 		time.Sleep(1000 * time.Millisecond)
 	}
@@ -60,9 +58,7 @@ func refreshCache() {
 	for k := range twtxtCache.Users {
 		twtxtCache.Mu.RUnlock()
 		err := twtxtCache.UpdateUser(k)
-		if err != nil {
-			log.Printf("%v\n", err.Error())
-		}
+		errLog("", err)
 		twtxtCache.Mu.RLock()
 	}
 	twtxtCache.Mu.RUnlock()
@@ -70,9 +66,7 @@ func refreshCache() {
 	remoteRegistries.Mu.RLock()
 	for _, v := range remoteRegistries.List {
 		err := twtxtCache.CrawlRemoteRegistry(v)
-		if err != nil {
-			log.Printf("Error while refreshing local copy of remote registry user data: %v\n", err.Error())
-		}
+		errLog("Error refreshing local copy of remote registry data: ", err)
 	}
 	remoteRegistries.Mu.RUnlock()
 	confObj.Mu.Lock()
@@ -90,14 +84,10 @@ func pingAssets() {
 	confObj.Mu.RUnlock()
 
 	cssStat, err := os.Stat(assetsDir + "/style.css")
-	if err != nil {
-		log.Printf("%v\n", err.Error())
-	}
+	errLog("", err)
 
 	indexStat, err := os.Stat(assetsDir + "/tmpl/index.html")
-	if err != nil {
-		log.Printf("%v\n", err.Error())
-	}
+	errLog("", err)
 
 	staticCache.mu.RLock()
 	indexMod := staticCache.indexMod
@@ -113,9 +103,7 @@ func pingAssets() {
 		confObj.Mu.RLock()
 		err = tmpls.ExecuteTemplate(buf, "index.html", confObj.Instance)
 		confObj.Mu.RUnlock()
-		if err != nil {
-			log.Printf("%v\n", err.Error())
-		}
+		errLog("", err)
 
 		staticCache.mu.Lock()
 		staticCache.index = buf.Bytes()
@@ -126,9 +114,7 @@ func pingAssets() {
 	if !cssMod.Equal(cssStat.ModTime()) {
 
 		css, err := ioutil.ReadFile(assetsDir + "/style.css")
-		if err != nil {
-			log.Printf("%v\n", err.Error())
-		}
+		errLog("", err)
 
 		staticCache.mu.Lock()
 		staticCache.css = css
diff --git a/svc/db.go b/svc/db.go
index f5e8678..a6ddf18 100644
--- a/svc/db.go
+++ b/svc/db.go
@@ -2,13 +2,12 @@ package svc // import "github.com/getwtxt/getwtxt/svc"
 
 import (
 	"database/sql"
-	"log"
 	"net"
 	"strings"
 	"time"
 
 	"github.com/getwtxt/registry"
-	_ "github.com/mattn/go-sqlite3"
+	_ "github.com/mattn/go-sqlite3" // for the sqlite3 driver
 	"github.com/syndtr/goleveldb/leveldb"
 )
 
@@ -45,11 +44,11 @@ func initDatabase() {
 	case "sqlite":
 		var lite *sql.DB
 		lite, err := sql.Open("sqlite3", confObj.DBPath)
-		errFatal(err)
+		errFatal("Error opening sqlite3 DB: ", err)
 		litePrep, err := lite.Prepare("CREATE TABLE IF NOT EXISTS getwtxt (urlKey TEXT PRIMARY KEY, isUser BOOL, blobKey TEXT, data BLOB)")
-		errFatal(err)
+		errFatal("Error preparing sqlite3 DB: ", err)
 		_, err = litePrep.Exec()
-		errFatal(err)
+		errFatal("Error creating sqlite3 DB: ", err)
 		db = &dbSqlite{db: lite}
 
 	case "postgres":
@@ -59,9 +58,7 @@ func initDatabase() {
 	}
 	confObj.Mu.RUnlock()
 
-	if err != nil {
-		log.Fatalf("%v\n", err.Error())
-	}
+	errFatal("", err)
 
 	dbChan <- db
 
@@ -161,9 +158,7 @@ func (lvl dbLevel) pull() {
 			data.Date = val
 		case "Status":
 			thetime, err := time.Parse(time.RFC3339, split[2])
-			if err != nil {
-				log.Printf("%v\n", err.Error())
-			}
+			errLog("", err)
 			data.Status[thetime] = val
 		}
 
@@ -179,9 +174,7 @@ func (lvl dbLevel) pull() {
 
 	iter.Release()
 	err := iter.Error()
-	if err != nil {
-		log.Printf("Error while pulling DB into registry cache: %v\n", err.Error())
-	}
+	errLog("Error while pulling DB into registry cache: ", err)
 }
 
 func (lite dbSqlite) push() error {
diff --git a/svc/handlers.go b/svc/handlers.go
index 3dcb84f..13555d5 100644
--- a/svc/handlers.go
+++ b/svc/handlers.go
@@ -3,7 +3,6 @@ package svc // import "github.com/getwtxt/getwtxt/svc"
 import (
 	"crypto/sha256"
 	"fmt"
-	"log"
 	"net/http"
 	"strconv"
 	"time"
@@ -14,9 +13,7 @@ import (
 
 func getEtag(modtime time.Time) string {
 	shabytes, err := modtime.MarshalText()
-	if err != nil {
-		log.Printf("%v\n", err.Error())
-	}
+	errLog("", err)
 	return fmt.Sprintf("%x", sha256.Sum256(shabytes))
 }
 
diff --git a/svc/init.go b/svc/init.go
index aed3449..8e9bb20 100644
--- a/svc/init.go
+++ b/svc/init.go
@@ -40,6 +40,18 @@ var remoteRegistries = &RemoteRegistries{}
 
 var staticCache = &staticAssets{}
 
+func errFatal(context string, err error) {
+	if err != nil {
+		log.Fatalf(context+"%v\n", err.Error())
+	}
+}
+
+func errLog(context string, err error) {
+	if err != nil {
+		log.Printf(context+"%v\n", err.Error())
+	}
+}
+
 // I'm not using init() because it runs
 // even during testing and was causing
 // problems.
@@ -92,10 +104,8 @@ func watchForInterrupt() {
 
 			case *dbLevel:
 				lvl := dbType
-				if err := lvl.db.Close(); err != nil {
-					log.Printf("%v\n", err.Error())
-				}
-
+				err := lvl.db.Close()
+				errLog("", err)
 			}
 
 			if !confObj.StdoutLogging {
diff --git a/svc/post.go b/svc/post.go
index 0355afa..85804dc 100644
--- a/svc/post.go
+++ b/svc/post.go
@@ -2,7 +2,6 @@ package svc // import "github.com/getwtxt/getwtxt/svc"
 
 import (
 	"fmt"
-	"log"
 	"net/http"
 
 	"github.com/getwtxt/registry"
@@ -48,9 +47,7 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 	}
 
 	statuses, err := registry.ParseUserTwtxt(out, nick, urls)
-	if err != nil {
-		log.Printf("Error Parsing User Data: %v\n", err.Error())
-	}
+	errLog("Error Parsing User Data: ", err)
 
 	if err := twtxtCache.AddUser(nick, urls, "", uip, statuses); err != nil {
 		log400(w, r, "Error Adding User to Cache: "+err.Error())
@@ -59,7 +56,5 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 
 	log200(r)
 	_, err = w.Write([]byte(fmt.Sprintf("200 OK\n")))
-	if err != nil {
-		log.Printf("%v\n", err.Error())
-	}
+	errLog("", err)
 }
diff --git a/svc/query.go b/svc/query.go
index a27747d..f2696dd 100644
--- a/svc/query.go
+++ b/svc/query.go
@@ -64,9 +64,7 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error {
 
 	pageVal = strings.TrimSpace(pageVal)
 	page, err := strconv.Atoi(pageVal)
-	if err != nil {
-		log.Printf("%v\n", err.Error())
-	}
+	errLog("", err)
 
 	vars := mux.Vars(r)
 	endpoint := vars["endpoint"]
diff --git a/svc/svc.go b/svc/svc.go
index 6870350..717a4d6 100644
--- a/svc/svc.go
+++ b/svc/svc.go
@@ -113,9 +113,7 @@ func Start() {
 
 	log.Printf("Listening on %v\n", portnum)
 	err := server.ListenAndServe()
-	if err != nil {
-		log.Printf("%v\n", err.Error())
-	}
+	errLog("", err)
 
 	closeLog <- true
 }