mirror of
https://github.com/umputun/reproxy.git
synced 2024-09-11 08:55:25 +03:00
Handle docker API errors
This commit is contained in:
parent
813ed6df3d
commit
e7121846da
@ -324,7 +324,19 @@ func (d *dockerClient) ListContainers() ([]containerInfo, error) {
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var response []struct {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
e := struct {
|
||||
Message string `json:"message"`
|
||||
}{}
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&e); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse error from docker daemon: %w", err)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unexpected error from docker daemon: %s", e.Message)
|
||||
}
|
||||
|
||||
response := []struct {
|
||||
ID string `json:"Id"`
|
||||
Name string
|
||||
State string
|
||||
|
@ -201,3 +201,16 @@ func TestDockerClient(t *testing.T) {
|
||||
|
||||
assert.Empty(t, c[1].IP)
|
||||
}
|
||||
|
||||
func TestDockerClient_error(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, `{"message": "bruh"}`, http.StatusInternalServerError)
|
||||
}))
|
||||
|
||||
defer srv.Close()
|
||||
addr := fmt.Sprintf("tcp://%s", strings.TrimPrefix(srv.URL, "http://"))
|
||||
|
||||
client := NewDockerClient(addr, "bridge")
|
||||
_, err := client.ListContainers()
|
||||
require.EqualError(t, err, "unexpected error from docker daemon: bruh")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user