diff --git a/cache/downloader.go b/cache/downloader.go index be4bdb1..f3c2191 100644 --- a/cache/downloader.go +++ b/cache/downloader.go @@ -1,7 +1,6 @@ package cache import ( - "errors" "io" "net/http" "os" @@ -9,6 +8,16 @@ import ( "sync" ) +// DownloadError conveys information about a download request that failed. +type DownloadError struct { + Status string +} + +// Error returns a description of the error. +func (d *DownloadError) Error() string { + return d.Status +} + // downloader attempts to download a file from a remote URL. type downloader struct { doneMutex sync.Mutex @@ -40,7 +49,9 @@ func newDownloader(rawurl, jsonFilename, dataFilename string) *downloader { } defer resp.Body.Close() if resp.StatusCode != 200 { - d.err = errors.New(resp.Status) + d.err = &DownloadError{ + Status: resp.Status, + } return } f, err := os.Create(dataFilename) diff --git a/server.go b/server.go index b34daef..b6d935b 100644 --- a/server.go +++ b/server.go @@ -48,8 +48,6 @@ func (s *Server) writeHeaders(w http.ResponseWriter, e *cache.Entry) { w.WriteHeader(http.StatusOK) } -// TODO: support for HEAD requests - // ServeHTTP processes an incoming request to the proxy. GET requests are // served with the storage backend and every other request is (out of // necessity) rejected since it can't be cached. @@ -67,7 +65,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { defer r.Close() e, err := r.GetEntry() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + if dErr, ok := err.(*cache.DownloadError); ok { + http.Error(w, dErr.Error(), http.StatusServiceUnavailable) + } else { + http.Error(w, err.Error(), http.StatusInternalServerError) + } log.Println("[ERR]", err) return }