zoekt-archive-index: Check status for reading HTTP response

Change-Id: Id941e3063d295ba9c65c01a7a87161621dc0cfde
diff --git a/cmd/zoekt-archive-index/archive.go b/cmd/zoekt-archive-index/archive.go
index bb5fa6e..1230f8d 100644
--- a/cmd/zoekt-archive-index/archive.go
+++ b/cmd/zoekt-archive-index/archive.go
@@ -3,8 +3,11 @@
 import (
 	"archive/tar"
 	"compress/gzip"
+	"fmt"
 	"io"
+	"io/ioutil"
 	"net/http"
+	"net/url"
 	"os"
 	"strings"
 )
@@ -52,8 +55,8 @@
 	return nil
 }
 
-// openArchiveReader opens the tar at the URL or filepath u. Also supported is
-// tgz files over http.
+// openArchive opens the tar at the URL or filepath u. Also supported is tgz
+// files over http.
 func openArchive(u string) (Archive, error) {
 	var (
 		r      io.Reader
@@ -65,6 +68,18 @@
 		if err != nil {
 			return nil, err
 		}
+		if resp.StatusCode < 200 || resp.StatusCode >= 300 {
+			b, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024))
+			resp.Body.Close()
+			if err != nil {
+				return nil, err
+			}
+			return nil, &url.Error{
+				Op:  "Get",
+				URL: u,
+				Err: fmt.Errorf("%s: %s", resp.Status, string(b)),
+			}
+		}
 		closer = resp.Body
 		r = resp.Body
 		if resp.Header.Get("Content-Type") == "application/x-gzip" {