From 3df247b7df49d19d1a90252bc05df6fb33d42d4d Mon Sep 17 00:00:00 2001 From: Valentina Date: Fri, 31 Mar 2023 13:27:16 +0100 Subject: [PATCH] address PR comments --- dataset/get-metadata_test.go | 2 +- dataset/put-metadata.go | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/dataset/get-metadata_test.go b/dataset/get-metadata_test.go index 698d364..d1495cf 100644 --- a/dataset/get-metadata_test.go +++ b/dataset/get-metadata_test.go @@ -99,7 +99,7 @@ func TestUnitHandlers(t *testing.T) { if collectionID == mockCollectionId { return mockCollection, nil } else { - return c, errors.New("Collection not found") + return c, errors.New("collection not found") } }, } diff --git a/dataset/put-metadata.go b/dataset/put-metadata.go index a67c0e7..8404a8b 100644 --- a/dataset/put-metadata.go +++ b/dataset/put-metadata.go @@ -2,6 +2,7 @@ package dataset import ( "encoding/json" + "io" "io/ioutil" "net/http" @@ -100,15 +101,15 @@ func putMetadata(w http.ResponseWriter, req *http.Request, dc DatasetClient, zc } // PutEditableMetadata updates a given list of metadata fields, agreed as being editable for both a dataset and a version object -// This new endpoint makes an unique call to the dataset api updating only the relevant metadata fields in a transactional way +// This new endpoint makes a unique call to the dataset api updating only the relevant metadata fields in a transactional way // It also calls zebedee to update the collection func PutEditableMetadata(dc DatasetClient, zc ZebedeeClient) http.HandlerFunc { return dphandlers.ControllerHandler(func(w http.ResponseWriter, r *http.Request, lang, collectionID, accessToken string) { - putEditableMetadata(w, r, dc, zc, accessToken, collectionID, lang) + putEditableMetadata(w, r, dc, zc, accessToken, collectionID) }) } -func putEditableMetadata(w http.ResponseWriter, req *http.Request, dc DatasetClient, zc ZebedeeClient, userAccessToken, collectionID, lang string) { +func putEditableMetadata(w http.ResponseWriter, req *http.Request, dc DatasetClient, zc ZebedeeClient, userAccessToken, collectionID string) { ctx := req.Context() err := checkAccessTokenAndCollectionHeaders(userAccessToken, collectionID) @@ -129,7 +130,7 @@ func putEditableMetadata(w http.ResponseWriter, req *http.Request, dc DatasetCli "version": version, } - b, err := ioutil.ReadAll(req.Body) + b, err := io.ReadAll(req.Body) if err != nil { log.Error(ctx, "putMetadata endpoint: error reading body", err, log.Data(logInfo)) http.Error(w, "error reading body", http.StatusBadRequest) @@ -165,10 +166,15 @@ func putEditableMetadata(w http.ResponseWriter, req *http.Request, dc DatasetCli if err != nil { log.Error(ctx, "error adding version to collection", err, log.Data(logInfo)) http.Error(w, "error adding version to collection", http.StatusInternalServerError) + return } w.WriteHeader(http.StatusOK) - w.Write(b) + if _, err = w.Write(b); err != nil { + log.Error(ctx, "failed to write response body", err, log.Data(logInfo)) + http.Error(w, "failed to write response body", http.StatusInternalServerError) + return + } log.Info(ctx, "put metadata: request successful", log.Data(logInfo)) }