Skip to content

Commit

Permalink
better error handling in inveniordm post
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Nov 15, 2024
1 parent 53a76c3 commit 1f9c38c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var versionCmd = &cobra.Command{
Short: "Print the version number of commonmeta",
Long: `All software has versions. This is commonmeta's`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Println("Commonmeta v0.5.14 -- HEAD")
cmd.Println("Commonmeta v0.5.15 -- HEAD")
},
}

Expand Down
23 changes: 9 additions & 14 deletions inveniordm/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func Post(data commonmeta.Data, host string, apiKey string) ([]byte, error) {
}
output, err := json.Marshal(inveniordm)
if err != nil {
fmt.Println(err)
return nil, err
}
var communityID string
var communityIndex int
Expand Down Expand Up @@ -423,17 +423,17 @@ func Post(data commonmeta.Data, host string, apiKey string) ([]byte, error) {
Timeout: time.Second * 10,
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
return body, err
}

// publish draft record
var draft map[string]interface{}
err = json.Unmarshal(body, &draft)
if err != nil {
fmt.Println("error:", err)
return nil, err
}
ID := draft["id"].(string)
publishURL := url.URL{
Expand All @@ -450,10 +450,11 @@ func Post(data commonmeta.Data, host string, apiKey string) ([]byte, error) {
Timeout: time.Second * 10,
}
resp, err = client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ = io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
return body, err
}

// add record to community
c, _ := json.Marshal(communities)
Expand All @@ -471,13 +472,7 @@ func Post(data commonmeta.Data, host string, apiKey string) ([]byte, error) {
Timeout: time.Second * 10,
}
resp, err = client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ = io.ReadAll(resp.Body)
if err != nil {
fmt.Println("error:", err)
}
return body, err
}

0 comments on commit 1f9c38c

Please sign in to comment.