Browse code

Merge pull request #12649 from jlhawn/fix_pull_err_explosion

Correctly format API error on image pull

Doug Davis authored on 2015/04/23 09:28:24
Showing 2 changed files
... ...
@@ -738,6 +738,15 @@ func (s *Server) postImagesCreate(eng *engine.Engine, version version.Version, w
738 738
 		}
739 739
 	}
740 740
 
741
+	var (
742
+		opErr   error
743
+		useJSON = version.GreaterThan("1.0")
744
+	)
745
+
746
+	if useJSON {
747
+		w.Header().Set("Content-Type", "application/json")
748
+	}
749
+
741 750
 	if image != "" { //pull
742 751
 		if tag == "" {
743 752
 			image, tag = parsers.ParseRepositoryTag(image)
... ...
@@ -754,17 +763,10 @@ func (s *Server) postImagesCreate(eng *engine.Engine, version version.Version, w
754 754
 			MetaHeaders: metaHeaders,
755 755
 			AuthConfig:  authConfig,
756 756
 			OutStream:   utils.NewWriteFlusher(w),
757
-		}
758
-		if version.GreaterThan("1.0") {
759
-			imagePullConfig.Json = true
760
-			w.Header().Set("Content-Type", "application/json")
761
-		} else {
762
-			imagePullConfig.Json = false
757
+			Json:        useJSON,
763 758
 		}
764 759
 
765
-		if err := s.daemon.Repositories().Pull(image, tag, imagePullConfig); err != nil {
766
-			return err
767
-		}
760
+		opErr = s.daemon.Repositories().Pull(image, tag, imagePullConfig)
768 761
 	} else { //import
769 762
 		if tag == "" {
770 763
 			repo, tag = parsers.ParseRepositoryTag(repo)
... ...
@@ -775,12 +777,7 @@ func (s *Server) postImagesCreate(eng *engine.Engine, version version.Version, w
775 775
 			Changes:   r.Form["changes"],
776 776
 			InConfig:  r.Body,
777 777
 			OutStream: utils.NewWriteFlusher(w),
778
-		}
779
-		if version.GreaterThan("1.0") {
780
-			imageImportConfig.Json = true
781
-			w.Header().Set("Content-Type", "application/json")
782
-		} else {
783
-			imageImportConfig.Json = false
778
+			Json:      useJSON,
784 779
 		}
785 780
 
786 781
 		newConfig, err := builder.BuildFromConfig(s.daemon, &runconfig.Config{}, imageImportConfig.Changes)
... ...
@@ -789,9 +786,12 @@ func (s *Server) postImagesCreate(eng *engine.Engine, version version.Version, w
789 789
 		}
790 790
 		imageImportConfig.ContainerConfig = newConfig
791 791
 
792
-		if err := s.daemon.Repositories().Import(src, repo, tag, imageImportConfig); err != nil {
793
-			return err
794
-		}
792
+		opErr = s.daemon.Repositories().Import(src, repo, tag, imageImportConfig)
793
+	}
794
+
795
+	if opErr != nil {
796
+		sf := streamformatter.NewStreamFormatter(useJSON)
797
+		return fmt.Errorf(string(sf.FormatError(opErr)))
795 798
 	}
796 799
 
797 800
 	return nil
... ...
@@ -98,8 +98,13 @@ func (s *DockerSuite) TestPullImageFromCentralRegistry(c *check.C) {
98 98
 
99 99
 // pulling a non-existing image from the central registry should return a non-zero exit code
100 100
 func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
101
-	pullCmd := exec.Command(dockerBinary, "pull", "fooblahblah1234")
102
-	if out, _, err := runCommandWithOutput(pullCmd); err == nil {
101
+	testRequires(c, Network)
102
+
103
+	name := "sadfsadfasdf"
104
+	pullCmd := exec.Command(dockerBinary, "pull", name)
105
+	out, _, err := runCommandWithOutput(pullCmd)
106
+
107
+	if err == nil || !strings.Contains(out, fmt.Sprintf("Error: image library/%s:latest not found", name)) {
103 108
 		c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
104 109
 	}
105 110
 }