@@ -18,6 +18,7 @@ package compose
18
18
19
19
import (
20
20
"context"
21
+ "encoding/json"
21
22
"errors"
22
23
"fmt"
23
24
"os"
@@ -99,6 +100,9 @@ func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
99
100
Status : err .Error (),
100
101
}
101
102
}
103
+ if ui .Mode == ui .ModeJSON {
104
+ err = makeJSONError (err )
105
+ }
102
106
return err
103
107
}
104
108
}
@@ -154,6 +158,38 @@ func (o *ProjectOptions) WithServices(dockerCli command.Cli, fn ProjectServicesF
154
158
})
155
159
}
156
160
161
+ type jsonErrorData struct {
162
+ Error bool `json:"error,omitempty"`
163
+ Message string `json:"message,omitempty"`
164
+ }
165
+
166
+ func errorAsJSON (message string ) string {
167
+ errorMessage := & jsonErrorData {
168
+ Error : true ,
169
+ Message : message ,
170
+ }
171
+ marshal , err := json .Marshal (errorMessage )
172
+ if err == nil {
173
+ return string (marshal )
174
+ } else {
175
+ return message
176
+ }
177
+ }
178
+
179
+ func makeJSONError (err error ) error {
180
+ if err == nil {
181
+ return nil
182
+ }
183
+ var statusErr dockercli.StatusError
184
+ if errors .As (err , & statusErr ) {
185
+ return dockercli.StatusError {
186
+ StatusCode : statusErr .StatusCode ,
187
+ Status : errorAsJSON (statusErr .Status ),
188
+ }
189
+ }
190
+ return fmt .Errorf ("%s" , errorAsJSON (err .Error ()))
191
+ }
192
+
157
193
func (o * ProjectOptions ) addProjectFlags (f * pflag.FlagSet ) {
158
194
f .StringArrayVar (& o .Profiles , "profile" , []string {}, "Specify a profile to enable" )
159
195
f .StringVarP (& o .ProjectName , "project-name" , "p" , "" , "Project name" )
0 commit comments