cmd/syncthing: Improve "cli debug file" handling
Proper URL encoding, and return a sensible error when it's not found.
This commit is contained in:
@@ -134,9 +134,11 @@ func (c *apiClient) Post(url, body string) (*http.Response, error) {
|
|||||||
return c.Do(request)
|
return c.Do(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errNotFound = errors.New("invalid endpoint or API call")
|
||||||
|
|
||||||
func checkResponse(response *http.Response) error {
|
func checkResponse(response *http.Response) error {
|
||||||
if response.StatusCode == http.StatusNotFound {
|
if response.StatusCode == http.StatusNotFound {
|
||||||
return errors.New("invalid endpoint or API call")
|
return errNotFound
|
||||||
} else if response.StatusCode == http.StatusUnauthorized {
|
} else if response.StatusCode == http.StatusUnauthorized {
|
||||||
return errors.New("invalid API key")
|
return errors.New("invalid API key")
|
||||||
} else if response.StatusCode != http.StatusOK {
|
} else if response.StatusCode != http.StatusOK {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
@@ -35,7 +36,10 @@ var debugCommand = cli.Command{
|
|||||||
|
|
||||||
func debugFile() cli.ActionFunc {
|
func debugFile() cli.ActionFunc {
|
||||||
return func(c *cli.Context) error {
|
return func(c *cli.Context) error {
|
||||||
return indexDumpOutput(fmt.Sprintf("debug/file?folder=%v&file=%v", c.Args()[0], normalizePath(c.Args()[1])))(c)
|
query := make(url.Values)
|
||||||
|
query.Set("folder", c.Args()[0])
|
||||||
|
query.Set("file", normalizePath(c.Args()[1]))
|
||||||
|
return indexDumpOutput("debug/file?" + query.Encode())(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,9 @@ func indexDumpOutput(url string) cli.ActionFunc {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
response, err := client.Get(url)
|
response, err := client.Get(url)
|
||||||
|
if errors.Is(err, errNotFound) {
|
||||||
|
return errors.New("not found (debugging disabled or folder/file not in database)")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user