[b/67851068] fix http code for not found blob (#26)
diff --git a/api.go b/api.go
index 5f5ab0e..11f496d 100644
--- a/api.go
+++ b/api.go
@@ -183,16 +183,23 @@
blobId := vars["blobId"]
fs, err := a.dbMan.getLocalFSLocation(blobId)
if err != nil {
- a.writeInternalError(w, "BlobId "+blobId+" has no mapping blob file")
+ if err == sql.ErrNoRows {
+ a.writeError(w, http.StatusNotFound, API_ERR_NOT_FOUND, "cannot find the blob")
+ } else {
+ log.Errorf("apiReturnBlobData error from db: %v", err)
+ a.writeInternalError(w, "BlobId "+blobId+" has no mapping blob file")
+ }
return
}
byte, err := ioutil.ReadFile(fs)
if err != nil {
+ log.Errorf("apiReturnBlobData error read file: %v, %v", fs, err)
a.writeInternalError(w, err.Error())
return
}
_, err = io.Copy(w, bytes.NewReader(byte))
if err != nil {
+ log.Errorf("apiReturnBlobData error copy file: %v, %v", fs, err)
a.writeInternalError(w, err.Error())
}
w.Header().Set("Content-Type", headerSteam)
diff --git a/api_test.go b/api_test.go
index 630d993..bcd177a 100644
--- a/api_test.go
+++ b/api_test.go
@@ -369,7 +369,7 @@
})
Context("GET /blobs", func() {
- It("should get file bytesfrom endpoint", func() {
+ It("should get file bytes from endpoint", func() {
// setup http client
uri, err := url.Parse(apiTestUrl)
Expect(err).Should(Succeed())
@@ -396,6 +396,33 @@
Expect(err).Should(Succeed())
Expect(string(body)).Should(Equal(randString))
})
+
+ It("should get error reponse", func() {
+ // setup http client
+ uri, err := url.Parse(apiTestUrl)
+ Expect(err).Should(Succeed())
+ uri.Path = blobEndpointPath + strconv.Itoa(testCount) + "/" + util.GenerateUUID()
+
+ // set test data
+ testData := [][]interface{}{
+ {"", sql.ErrNoRows},
+ {"", fmt.Errorf("test error")},
+ }
+ expectedCode := []int{
+ http.StatusNotFound,
+ http.StatusInternalServerError,
+ }
+
+ for i, data := range testData {
+ dummyDbMan.localFSLocation = data[0].(string)
+ dummyDbMan.err = data[1].(error)
+ // http get
+ res, err := http.Get(uri.String())
+ Expect(err).Should(Succeed())
+ res.Body.Close()
+ Expect(res.StatusCode).Should(Equal(expectedCode[i]))
+ }
+ })
})
Context("GET /configurations/{configId}", func() {
diff --git a/data.go b/data.go
index e99cba7..558c133 100644
--- a/data.go
+++ b/data.go
@@ -381,25 +381,22 @@
}
-func (dbc *dbManager) getLocalFSLocation(blobId string) (localFsLocation string, err error) {
+func (dbc *dbManager) getLocalFSLocation(blobId string) (string, error) {
log.Debugf("Getting the blob file for blobId {%s}", blobId)
- rows, err := dbc.getDb().Query("SELECT local_fs_location FROM APID_BLOB_AVAILABLE WHERE id = '" + blobId + "'")
+ localFsLocation := sql.NullString{}
+ err := dbc.getDb().QueryRow("SELECT local_fs_location FROM APID_BLOB_AVAILABLE WHERE id = '" + blobId + "'").Scan(&localFsLocation)
if err != nil {
- log.Errorf("SELECT local_fs_location failed %v", err)
+ if err != sql.ErrNoRows {
+ log.Errorf("SELECT local_fs_location failed %v", err)
+ }
return "", err
}
-
- defer rows.Close()
- for rows.Next() {
- err = rows.Scan(&localFsLocation)
- if err != nil {
- log.Errorf("Scan local_fs_location failed %v", err)
- return "", err
- }
- log.Debugf("Got the blob file {%s} for blobId {%s}", localFsLocation, blobId)
+ if localFsLocation.Valid {
+ return localFsLocation.String, nil
}
- return
+ log.Warnf("local_fs_location for blob %s is null!", blobId)
+ return "", nil
}
func (dbc *dbManager) loadLsnFromDb() error {
diff --git a/data_test.go b/data_test.go
index 65aace8..43ca0c8 100644
--- a/data_test.go
+++ b/data_test.go
@@ -202,6 +202,9 @@
location, err := testDbMan.getLocalFSLocation(testBlobId)
Expect(err).Should(Succeed())
Expect(location).Should(Equal(testBlobLocalFsPrefix + testBlobId))
+ // negative test
+ location, err = testDbMan.getLocalFSLocation("non-existent")
+ Expect(err).Should(Equal(sql.ErrNoRows))
})
It("should get configuration by Id", func() {
diff --git a/mock_test.go b/mock_test.go
index f8f804a..fb0e06f 100644
--- a/mock_test.go
+++ b/mock_test.go
@@ -63,7 +63,7 @@
}
func (d *dummyDbManager) getLocalFSLocation(string) (string, error) {
- return d.localFSLocation, nil
+ return d.localFSLocation, d.err
}
func (d *dummyDbManager) getConfigById(id string) (*Configuration, error) {