add error handling

This commit is contained in:
Victor Vieux 2013-05-25 14:12:02 +00:00
parent d3b9733507
commit cb0bc4adc2
3 changed files with 12 additions and 1 deletions

3
api.go
View File

@ -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 {

View File

@ -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)
}

View File

@ -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 + "\"}"