Skip to content

Commit bb81f94

Browse files
committed
adds proper error handling
1 parent 92a6c57 commit bb81f94

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

client.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ func NewPrometheus(root, graphDriverName string, maxParallelDownloads uint) (*Pr
7575
func (p *Prometheus) PullImage(imageName, dstName string) (*OciManifest, error) {
7676
progressCh := make(chan types.ProgressProperties)
7777
manifestCh := make(chan OciManifest)
78+
errorCh := make(chan error)
7879

7980
defer close(progressCh)
8081
defer close(manifestCh)
82+
defer close(errorCh)
8183

8284
manifest, err := p.PullManifestOnly(imageName)
8385
if err != nil {
@@ -88,7 +90,7 @@ func (p *Prometheus) PullImage(imageName, dstName string) (*OciManifest, error)
8890
layersAll := len(manifest.LayerInfos()) + 1
8991
layersDone := 0
9092

91-
err = p.pullImage(imageName, dstName, progressCh, manifestCh)
93+
err = p.pullImage(imageName, dstName, progressCh, manifestCh, errorCh)
9294
if err != nil {
9395
return nil, err
9496
}
@@ -108,6 +110,8 @@ func (p *Prometheus) PullImage(imageName, dstName string) (*OciManifest, error)
108110
}
109111
case manifest := <-manifestCh:
110112
return &manifest, nil
113+
case err := <-errorCh:
114+
return nil, err
111115
}
112116
}
113117
}
@@ -121,12 +125,12 @@ func (p *Prometheus) PullImage(imageName, dstName string) (*OciManifest, error)
121125
//
122126
// NOTE: The user is responsible for closing both channels once the operation
123127
// completes.
124-
func (p *Prometheus) PullImageAsync(imageName, dstName string, progressCh chan types.ProgressProperties, manifestCh chan OciManifest) error {
125-
err := p.pullImage(imageName, dstName, progressCh, manifestCh)
128+
func (p *Prometheus) PullImageAsync(imageName, dstName string, progressCh chan types.ProgressProperties, manifestCh chan OciManifest, errorCh chan error) error {
129+
err := p.pullImage(imageName, dstName, progressCh, manifestCh, errorCh)
126130
return err
127131
}
128132

129-
func (p *Prometheus) pullImage(imageName, dstName string, progressCh chan types.ProgressProperties, manifestCh chan OciManifest) error {
133+
func (p *Prometheus) pullImage(imageName, dstName string, progressCh chan types.ProgressProperties, manifestCh chan OciManifest, errorCh chan error) error {
130134
srcRef, err := alltransports.ParseImageName(fmt.Sprintf("docker://%s", imageName))
131135
if err != nil {
132136
return err
@@ -166,12 +170,14 @@ func (p *Prometheus) pullImage(imageName, dstName string, progressCh chan types.
166170
},
167171
)
168172
if err != nil {
173+
errorCh <- err
169174
return
170175
}
171176

172177
var manifest OciManifest
173178
err = json.Unmarshal(pulledManifestBytes, &manifest)
174179
if err != nil {
180+
errorCh <- err
175181
return
176182
}
177183

tests/pull_image_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ func TestPullImage(t *testing.T) {
7575
func TestPullImageAsync(t *testing.T) {
7676
progressCh := make(chan types.ProgressProperties)
7777
manifestCh := make(chan prometheus.OciManifest)
78+
errorCh := make(chan error)
7879

7980
defer close(progressCh)
8081
defer close(manifestCh)
8182

82-
err := pmt.PullImageAsync("docker.io/library/alpine:latest", "my-alpine", progressCh, manifestCh)
83+
err := pmt.PullImageAsync("docker.io/library/alpine:latest", "my-alpine", progressCh, manifestCh, errorCh)
8384
if err != nil {
8485
t.Fatalf("error pulling image: %v", err)
8586
}
@@ -91,6 +92,8 @@ func TestPullImageAsync(t *testing.T) {
9192
case manifest := <-manifestCh:
9293
fmt.Printf("Got manifest: %v\n", manifest)
9394
return
95+
case err := <-errorCh:
96+
t.Error(err)
9497
}
9598
}
9699
}

0 commit comments

Comments
 (0)