Skip to content

Commit

Permalink
btc: make API errors more verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jan 4, 2024
1 parent d830ebe commit 53085d3
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions btc/explorer_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,34 +196,47 @@ func (a *ExplorerAPI) PublishTx(rawTxHex string) (string, error) {
url := fmt.Sprintf("%s/tx", a.BaseURL)
resp, err := http.Post(url, "text/plain", strings.NewReader(rawTxHex))
if err != nil {
return "", err
return "", fmt.Errorf("error posting data to API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
}
defer resp.Body.Close()
body := new(bytes.Buffer)
_, err = body.ReadFrom(resp.Body)
if err != nil {
return "", err
return "", fmt.Errorf("error fetching data from API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
}
return body.String(), nil
}

func fetchJSON(url string, target interface{}) error {
resp, err := http.Get(url)
if err != nil {
return err
return fmt.Errorf("error fetching data from API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
}
defer resp.Body.Close()

body := new(bytes.Buffer)
_, err = body.ReadFrom(resp.Body)
if err != nil {
return err
return fmt.Errorf("error fetching data from API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
}
err = json.Unmarshal(body.Bytes(), target)
if err != nil {
if body.String() == "Transaction not found" {
return ErrTxNotFound
}

return fmt.Errorf("error decoding data from API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
}
return err

return nil
}

0 comments on commit 53085d3

Please sign in to comment.