about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-09 17:21:34 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-09 17:21:34 -0400
commit9b0d6aab1167a25400dae2a3c85cb4d08a7fa3cf (patch)
tree9b543cac1a4ae277f460ca651bcd6675b0aff722
parent825c7e2cd4cc77024c20e31224e3d311196d8e80 (diff)
downloadapi-9b0d6aab1167a25400dae2a3c85cb4d08a7fa3cf.tar.gz
cache.bap() now returns an error rather than handling the error silently
-rw-r--r--cache.go10
-rw-r--r--http.go10
2 files changed, 12 insertions, 8 deletions
diff --git a/cache.go b/cache.go
index be00083..0967c11 100644
--- a/cache.go
+++ b/cache.go
@@ -3,7 +3,6 @@ package main
 import (
 	"errors"
 	"io/ioutil"
-	"log"
 	"strings"
 	"sync"
 	"time"
@@ -52,7 +51,7 @@ func (cache *cacheWrapper) isFresh(path string) bool {
 
 // Wraps the two cache-checking functions.
 // One for /, the other for various requests.
-func (cache *cacheWrapper) bap(requestPath string) {
+func (cache *cacheWrapper) bap(requestPath string) error {
 	var split []string
 	var format, query string
 
@@ -65,7 +64,7 @@ func (cache *cacheWrapper) bap(requestPath string) {
 	cache.checkedInit(requestPath)
 
 	if cache.isFresh(requestPath) {
-		return
+		return nil
 	}
 
 	var bytes []byte
@@ -87,8 +86,7 @@ func (cache *cacheWrapper) bap(requestPath string) {
 	}
 
 	if err != nil {
-		log.Printf("Request error %s :: %s", requestPath, err.Error())
-		bytes = []byte("Internal Error")
+		return err
 	}
 
 	cache.Lock()
@@ -98,6 +96,8 @@ func (cache *cacheWrapper) bap(requestPath string) {
 		raw:     bytes,
 		expires: time.Now().Add(cacheDuration),
 	}
+
+	return nil
 }
 
 // yoinks the raw data and expiry time to send to the requester
diff --git a/http.go b/http.go
index 8ef76c5..e67560c 100644
--- a/http.go
+++ b/http.go
@@ -22,8 +22,11 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	cache.bap(r.URL.Path)
-	out, expires := cache.yoink(r.URL.Path)
+	err := cache.bap(r.URL.Path)
+	if err != nil {
+		errHTTP(w, r, err, http.StatusInternalServerError)
+		return
+	}
 
 	if format == "json" {
 		w.Header().Set("Content-Type", mimeJSON)
@@ -31,9 +34,10 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Content-Type", mimePlain)
 	}
 
+	out, expires := cache.yoink(r.URL.Path)
 	w.Header().Set("Expires", expires)
 
-	_, err := w.Write(out)
+	_, err = w.Write(out)
 	if err != nil {
 		errHTTP(w, r, err, http.StatusBadRequest)
 		return