diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-06-10 00:47:30 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-06-10 00:49:34 -0400 |
commit | 824556ab9829e0ff369808621bacb5cccbee27e9 (patch) | |
tree | 13e5dcf9cbfeec4763708daac244b7e30cd3c525 | |
parent | bedd5d588f4375b486734026c5eda861ab2bf485 (diff) | |
download | getwtxt-824556ab9829e0ff369808621bacb5cccbee27e9.tar.gz |
refactored http err handling to combine 3 funcs into 1
-rw-r--r-- | svc/handlers.go | 40 | ||||
-rw-r--r-- | svc/http.go | 21 | ||||
-rw-r--r-- | svc/http_test.go | 47 | ||||
-rw-r--r-- | svc/post.go | 40 |
4 files changed, 46 insertions, 102 deletions
diff --git a/svc/handlers.go b/svc/handlers.go index 02975ef..f83d4b2 100644 --- a/svc/handlers.go +++ b/svc/handlers.go @@ -33,7 +33,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { _, err := w.Write(staticCache.index) staticCache.mu.RUnlock() if err != nil { - log500(w, r, err) + errHTTP(w, r, err, http.StatusInternalServerError) return } @@ -54,7 +54,7 @@ func cssHandler(w http.ResponseWriter, r *http.Request) { _, err := w.Write(staticCache.css) staticCache.mu.RUnlock() if err != nil { - log500(w, r, err) + errHTTP(w, r, err, http.StatusInternalServerError) return } @@ -75,7 +75,7 @@ func apiFormatHandler(w http.ResponseWriter, r *http.Request) { func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) { out, err := twtxtCache.QueryAllStatuses() if err != nil { - log500(w, r, err) + errHTTP(w, r, err, http.StatusInternalServerError) } data := parseQueryOut(out) @@ -86,7 +86,7 @@ func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) { _, err = w.Write(data) if err != nil { - log500(w, r, err) + errHTTP(w, r, err, http.StatusInternalServerError) return } @@ -96,27 +96,28 @@ func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) { // handles "/api/plain/(users|mentions|tweets)" func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { - err := r.ParseForm() - if err != nil { - log500(w, r, err) - return - } + errLog("Error when parsing query values: ", r.ParseForm()) if r.FormValue("q") != "" || r.FormValue("url") != "" { err := apiEndpointQuery(w, r) if err != nil { - log500(w, r, err) + errHTTP(w, r, err, http.StatusInternalServerError) return } log200(r) return } + var err error page := 1 pageVal := r.FormValue("page") - if pageVal != "" { + + switch pageVal { + case "": + break + default: page, err = strconv.Atoi(pageVal) - if err != nil || page == 0 { + if err != nil || page < 1 { page = 1 } } @@ -138,7 +139,7 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { out = registry.ReduceToPage(page, out) default: - log404(w, r, fmt.Errorf("endpoint not found")) + errHTTP(w, r, fmt.Errorf("endpoint not found"), http.StatusNotFound) return } errLog("", err) @@ -151,11 +152,10 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { _, err = w.Write(data) if err != nil { - log500(w, r, err) - return + errHTTP(w, r, err, http.StatusInternalServerError) + } else { + log200(r) } - - log200(r) } // handles POST for "/api/plain/users" @@ -168,7 +168,7 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) { out, err := twtxtCache.QueryInStatus("#") if err != nil { - log500(w, r, err) + errHTTP(w, r, err, http.StatusInternalServerError) return } @@ -181,7 +181,7 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) { _, err = w.Write(data) if err != nil { - log500(w, r, err) + errHTTP(w, r, err, http.StatusInternalServerError) return } @@ -205,7 +205,7 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { _, err := w.Write(data) if err != nil { - log500(w, r, err) + errHTTP(w, r, err, http.StatusInternalServerError) return } diff --git a/svc/http.go b/svc/http.go index 347430e..0118912 100644 --- a/svc/http.go +++ b/svc/http.go @@ -2,6 +2,7 @@ package svc // import "github.com/getwtxt/getwtxt/svc" import ( "context" + "fmt" "log" "net" "net/http" @@ -69,24 +70,10 @@ func log200(r *http.Request) { log.Printf("*** %v :: 200 :: %v %v :: %v\n", uip, r.Method, r.URL, useragent) } -func log400(w http.ResponseWriter, r *http.Request, err string) { - uip := getIPFromCtx(r.Context()) - log.Printf("*** %v :: 400 :: %v %v :: %v\n", uip, r.Method, r.URL, err) - http.Error(w, "400 Bad Request: "+err, http.StatusBadRequest) -} - -func log404(w http.ResponseWriter, r *http.Request, err error) { +func errHTTP(w http.ResponseWriter, r *http.Request, err error, code int) { useragent := r.Header["User-Agent"] - uip := getIPFromCtx(r.Context()) - log.Printf("*** %v :: 404 :: %v %v :: %v :: %v\n", uip, r.Method, r.URL, useragent, err.Error()) - http.Error(w, err.Error(), http.StatusNotFound) -} - -func log500(w http.ResponseWriter, r *http.Request, err error) { - useragent := r.Header["User-Agent"] - uip := getIPFromCtx(r.Context()) - log.Printf("*** %v :: 500 :: %v %v :: %v :: %v\n", uip, r.Method, r.URL, useragent, err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) + log.Printf("*** %v :: %v :: %v %v :: %v :: %v\n", uip, code, r.Method, r.URL, useragent, err.Error()) + http.Error(w, fmt.Sprintf("Error %v: %v", code, err.Error()), code) } diff --git a/svc/http_test.go b/svc/http_test.go deleted file mode 100644 index 0faa0d3..0000000 --- a/svc/http_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package svc // import "github.com/getwtxt/getwtxt/svc" - -import ( - "errors" - "net/http" - "net/http/httptest" - "testing" -) - -func Test_log400(t *testing.T) { - initTestConf() - t.Run("log400", func(t *testing.T) { - w := httptest.NewRecorder() - req := httptest.NewRequest("POST", "/400", nil) - log400(w, req, "400 Test") - resp := w.Result() - if resp.StatusCode != http.StatusBadRequest { - t.Errorf("Didn't receive 400, received: %v\n", resp.StatusCode) - } - }) -} - -func Test_log404(t *testing.T) { - initTestConf() - t.Run("log404", func(t *testing.T) { - w := httptest.NewRecorder() - req := httptest.NewRequest("GET", "/404", nil) - log404(w, req, errors.New("404 Test")) - resp := w.Result() - if resp.StatusCode != http.StatusNotFound { - t.Errorf("Didn't receive 404, received: %v\n", resp.StatusCode) - } - }) -} - -func Test_log500(t *testing.T) { - initTestConf() - t.Run("log500", func(t *testing.T) { - w := httptest.NewRecorder() - req := httptest.NewRequest("POST", "/500", nil) - log500(w, req, errors.New("500 Test")) - resp := w.Result() - if resp.StatusCode != http.StatusInternalServerError { - t.Errorf("Didn't receive 500, received: %v\n", resp.StatusCode) - } - }) -} 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) + } + } } |