summary refs log tree commit diff stats
path: root/svc/post.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-10 00:47:30 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-10 00:49:34 -0400
commit824556ab9829e0ff369808621bacb5cccbee27e9 (patch)
tree13e5dcf9cbfeec4763708daac244b7e30cd3c525 /svc/post.go
parentbedd5d588f4375b486734026c5eda861ab2bf485 (diff)
downloadgetwtxt-824556ab9829e0ff369808621bacb5cccbee27e9.tar.gz
refactored http err handling to combine 3 funcs into 1
Diffstat (limited to 'svc/post.go')
-rw-r--r--svc/post.go40
1 files changed, 22 insertions, 18 deletions
diff --git a/svc/post.go b/svc/post.go
index 85804dc..9050859 100644
--- a/svc/post.go
+++ b/svc/post.go
@@ -14,14 +14,14 @@ import (
 // registry before adding each user to the local cache.
 func apiPostUser(w http.ResponseWriter, r *http.Request) {
 	if err := r.ParseForm(); err != nil {
-		log400(w, r, "Error Parsing Values: "+err.Error())
+		errHTTP(w, r, fmt.Errorf("error parsing values: %v", err.Error()), http.StatusBadRequest)
 		return
 	}
 
 	nick := r.FormValue("nickname")
 	urls := r.FormValue("url")
 	if nick == "" || urls == "" {
-		log400(w, r, "Nickname or URL missing")
+		errHTTP(w, r, fmt.Errorf("nickname or URL missing"), http.StatusBadRequest)
 		return
 	}
 
@@ -29,32 +29,36 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 
 	out, remoteRegistry, err := registry.GetTwtxt(urls)
 	if err != nil {
-		log400(w, r, "Error Fetching twtxt Data: "+err.Error())
+		errHTTP(w, r, fmt.Errorf("error fetching twtxt Data: %v", err.Error()), http.StatusBadRequest)
 		return
 	}
 
-	if remoteRegistry {
+	switch remoteRegistry {
+	case true:
 		remoteRegistries.Mu.Lock()
 		remoteRegistries.List = append(remoteRegistries.List, urls)
 		remoteRegistries.Mu.Unlock()
 
 		if err := twtxtCache.CrawlRemoteRegistry(urls); err != nil {
-			log400(w, r, "Error Crawling Remote Registry: "+err.Error())
-			return
+			errHTTP(w, r, fmt.Errorf("error crawling remote registry: %v", err.Error()), http.StatusInternalServerError)
+		} else {
+			log200(r)
 		}
-		log200(r)
-		return
-	}
 
-	statuses, err := registry.ParseUserTwtxt(out, nick, urls)
-	errLog("Error Parsing User Data: ", err)
+	case false:
+		statuses, err := registry.ParseUserTwtxt(out, nick, urls)
+		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())
-		return
-	}
+		if err := twtxtCache.AddUser(nick, urls, "", uip, statuses); err != nil {
+			errHTTP(w, r, fmt.Errorf("error adding user to cache: %v", err.Error()), http.StatusBadRequest)
+			return
+		}
 
-	log200(r)
-	_, err = w.Write([]byte(fmt.Sprintf("200 OK\n")))
-	errLog("", err)
+		_, err = w.Write([]byte(fmt.Sprintf("200 OK\n")))
+		if err != nil {
+			errHTTP(w, r, err, http.StatusInternalServerError)
+		} else {
+			log200(r)
+		}
+	}
 }