mirror of https://github.com/docker/compose.git
Format errors as JSON when in JSON progress mode.
Signed-off-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
5a6e1a7e2e
commit
8f7cd00481
|
@ -18,6 +18,7 @@ package compose
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -109,6 +110,9 @@ func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
|
|||
Status: err.Error(),
|
||||
}
|
||||
}
|
||||
if ui.Mode == ui.ModeJSON {
|
||||
err = makeJSONError(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -165,6 +169,38 @@ func (o *ProjectOptions) WithServices(dockerCli command.Cli, fn ProjectServicesF
|
|||
})
|
||||
}
|
||||
|
||||
type jsonErrorData struct {
|
||||
Error bool `json:"error,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func errorAsJSON(message string) string {
|
||||
errorMessage := &jsonErrorData{
|
||||
Error: true,
|
||||
Message: message,
|
||||
}
|
||||
marshal, err := json.Marshal(errorMessage)
|
||||
if err == nil {
|
||||
return string(marshal)
|
||||
} else {
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
||||
func makeJSONError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var statusErr dockercli.StatusError
|
||||
if errors.As(err, &statusErr) {
|
||||
return dockercli.StatusError{
|
||||
StatusCode: statusErr.StatusCode,
|
||||
Status: errorAsJSON(statusErr.Status),
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s", errorAsJSON(err.Error()))
|
||||
}
|
||||
|
||||
func (o *ProjectOptions) addProjectFlags(f *pflag.FlagSet) {
|
||||
f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
|
||||
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
|
||||
|
|
Loading…
Reference in New Issue