mirror of
https://github.com/rclone/rclone.git
synced 2025-01-25 15:49:33 +01:00
rc/webgui: improve error handling on web fetches
This commit is contained in:
parent
20300d1f61
commit
9c01ac9894
@ -15,19 +15,23 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/rclone/rclone/fs"
|
"github.com/rclone/rclone/fs"
|
||||||
"github.com/rclone/rclone/lib/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetLatestReleaseURL returns the latest release details of the rclone-webui-react
|
// GetLatestReleaseURL returns the latest release details of the rclone-webui-react
|
||||||
func GetLatestReleaseURL(fetchURL string) (string, string, int, error) {
|
func GetLatestReleaseURL(fetchURL string) (string, string, int, error) {
|
||||||
resp, err := http.Get(fetchURL)
|
resp, err := http.Get(fetchURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", 0, errors.New("Error getting latest release of rclone-webui")
|
return "", "", 0, errors.Wrap(err, "failed getting latest release of rclone-webui")
|
||||||
|
}
|
||||||
|
defer fs.CheckClose(resp.Body, &err)
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return "", "", 0, errors.Errorf("bad HTTP status %d (%s) when fetching %s", resp.StatusCode, resp.Status, fetchURL)
|
||||||
}
|
}
|
||||||
results := gitHubRequest{}
|
results := gitHubRequest{}
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&results); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&results); err != nil {
|
||||||
return "", "", 0, errors.New("could not decode results from http request")
|
return "", "", 0, errors.Wrap(err, "could not decode results from http request")
|
||||||
}
|
}
|
||||||
if len(results.Assets) < 1 {
|
if len(results.Assets) < 1 {
|
||||||
return "", "", 0, errors.New("could not find an asset in the release. " +
|
return "", "", 0, errors.New("could not find an asset in the release. " +
|
||||||
@ -124,14 +128,16 @@ func CheckAndDownloadWebGUIRelease(checkUpdate bool, forceUpdate bool, fetchURL
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DownloadFile is a helper function to download a file from url to the filepath
|
// DownloadFile is a helper function to download a file from url to the filepath
|
||||||
func DownloadFile(filepath string, url string) error {
|
func DownloadFile(filepath string, url string) (err error) {
|
||||||
|
|
||||||
// Get the data
|
// Get the data
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer fs.CheckClose(resp.Body, &err)
|
defer fs.CheckClose(resp.Body, &err)
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return errors.Errorf("bad HTTP status %d (%s) when fetching %s", resp.StatusCode, resp.Status, url)
|
||||||
|
}
|
||||||
|
|
||||||
// Create the file
|
// Create the file
|
||||||
out, err := os.Create(filepath)
|
out, err := os.Create(filepath)
|
||||||
|
Loading…
Reference in New Issue
Block a user