浏览代码

add error handling

Victor Vieux 12 年之前
父节点
当前提交
cb0bc4adc2
共有 3 个文件被更改,包括 12 次插入1 次删除
  1. 2 1
      api.go
  2. 3 0
      commands.go
  3. 7 0
      utils/utils.go

+ 2 - 1
api.go

@@ -295,7 +295,8 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht
 			w.Header().Set("Content-Type", "application/json")
 		}
 		if err := srv.ImagePull(image, tag, registry, w, version > 1.0); err != nil {
-			return err
+			fmt.Fprintf(w, utils.FormatError(err.Error(), version > 1.0))
+			return nil
 		}
 	} else { //import
 		if err := srv.ImageImport(src, repo, tag, r.Body, w); err != nil {

+ 3 - 0
commands.go

@@ -1261,6 +1261,7 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
 		type Message struct {
 			Status   string `json:"status,omitempty"`
 			Progress string `json:"progress,omitempty"`
+			Error    string `json:"error,omitempty"`
 		}
 		dec := json.NewDecoder(resp.Body)
 		for {
@@ -1272,6 +1273,8 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
 			}
 			if m.Progress != "" {
 				fmt.Fprintf(out, "Downloading %s\r", m.Progress)
+			} else if m.Error != "" {
+				return fmt.Errorf(m.Error)
 			} else {
 				fmt.Fprintf(out, "%s\n", m.Status)
 			}

+ 7 - 0
utils/utils.go

@@ -564,6 +564,13 @@ func FormatStatus(str string, json bool) string {
 	return str + "\r\n"
 }
 
+func FormatError(str string, json bool) string {
+	if json {
+		return "{\"error\" : \"" + str + "\"}"
+	}
+	return "Error: " + str + "\r\n"
+}
+
 func FormatProgress(str string, json bool) string {
 	if json {
 		return "{\"progress\" : \"" + str + "\"}"