@@ -17,6 +17,7 @@ package compile
17
17
18
18
import (
19
19
"context"
20
+ "errors"
20
21
"io"
21
22
"path/filepath"
22
23
"sort"
@@ -37,17 +38,14 @@ import (
37
38
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
38
39
paths "github.com/arduino/go-paths-helper"
39
40
properties "github.com/arduino/go-properties-orderedmap"
40
- "github.com/pkg/errors"
41
41
"github.com/segmentio/stats/v4"
42
42
"github.com/sirupsen/logrus"
43
- "google.golang.org/grpc/codes"
44
- "google.golang.org/grpc/status"
45
43
)
46
44
47
45
var tr = i18n .Tr
48
46
49
47
// Compile FIXMEDOC
50
- func Compile (ctx context.Context , req * rpc.CompileRequest , outStream , errStream io.Writer , debug bool ) (r * rpc.CompileResponse , e * status. Status ) {
48
+ func Compile (ctx context.Context , req * rpc.CompileRequest , outStream , errStream io.Writer , debug bool ) (r * rpc.CompileResponse , e error ) {
51
49
52
50
// There is a binding between the export binaries setting and the CLI flag to explicitly set it,
53
51
// since we want this binding to work also for the gRPC interface we must read it here in this
@@ -91,29 +89,29 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
91
89
92
90
pm := commands .GetPackageManager (req .GetInstance ().GetId ())
93
91
if pm == nil {
94
- return nil , status . New ( codes . InvalidArgument , tr ( "Invalid instance" ))
92
+ return nil , & commands. InvalidInstanceError {}
95
93
}
96
94
97
95
logrus .Tracef ("Compile %s for %s started" , req .GetSketchPath (), req .GetFqbn ())
98
96
if req .GetSketchPath () == "" {
99
- return nil , status . New ( codes . InvalidArgument , tr ( "Missing sketch path" ))
97
+ return nil , & commands. MissingSketchPathError {}
100
98
}
101
99
sketchPath := paths .New (req .GetSketchPath ())
102
100
sk , err := sketch .New (sketchPath )
103
101
if err != nil {
104
- return nil , status . Newf ( codes . NotFound , tr ( "Error opening sketch: %s" ), err )
102
+ return nil , & commands. SketchNotFoundError { Cause : err }
105
103
}
106
104
107
105
fqbnIn := req .GetFqbn ()
108
106
if fqbnIn == "" && sk != nil && sk .Metadata != nil {
109
107
fqbnIn = sk .Metadata .CPU .Fqbn
110
108
}
111
109
if fqbnIn == "" {
112
- return nil , status . New ( codes . InvalidArgument , tr ( "No FQBN (Fully Qualified Board Name) provided" ))
110
+ return nil , & commands. MissingFQBNError {}
113
111
}
114
112
fqbn , err := cores .ParseFQBN (fqbnIn )
115
113
if err != nil {
116
- return nil , status . Newf ( codes . InvalidArgument , tr ( "Invalid FQBN: %s" ), err )
114
+ return nil , & commands. InvalidFQBNError { Cause : err }
117
115
}
118
116
119
117
targetPlatform := pm .FindPlatform (& packagemanager.PlatformReference {
@@ -126,7 +124,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
126
124
// "\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+
127
125
// version.GetAppName()+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch)
128
126
// feedback.Error(errorMessage)
129
- return nil , status . New ( codes . NotFound , tr ("Platform not installed" ))
127
+ return nil , & commands. PlatformNotFound { Platform : targetPlatform . String (), Cause : errors . New ( tr ("platform not installed" ))}
130
128
}
131
129
132
130
builderCtx := & types.Context {}
@@ -149,7 +147,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
149
147
builderCtx .BuildPath = paths .New (req .GetBuildPath ())
150
148
}
151
149
if err = builderCtx .BuildPath .MkdirAll (); err != nil {
152
- return nil , status . Newf ( codes . PermissionDenied , tr ("Cannot create build directory: %s " ), err )
150
+ return nil , & commands. PermissionDeniedError { Message : tr ("Cannot create build directory" ), Cause : err }
153
151
}
154
152
builderCtx .CompilationDatabase = bldr .NewCompilationDatabase (
155
153
builderCtx .BuildPath .Join ("compile_commands.json" ),
@@ -179,7 +177,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
179
177
builderCtx .BuildCachePath = paths .New (req .GetBuildCachePath ())
180
178
err = builderCtx .BuildCachePath .MkdirAll ()
181
179
if err != nil {
182
- return nil , status . Newf ( codes . PermissionDenied , tr ("Cannot create build cache directory: %s " ), err )
180
+ return nil , & commands. PermissionDeniedError { Message : tr ("Cannot create build cache directory" ), Cause : err }
183
181
}
184
182
}
185
183
@@ -222,14 +220,22 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
222
220
223
221
// if --preprocess or --show-properties were passed, we can stop here
224
222
if req .GetShowProperties () {
225
- return r , status .Convert (builder .RunParseHardwareAndDumpBuildProperties (builderCtx ))
223
+ compileErr := builder .RunParseHardwareAndDumpBuildProperties (builderCtx )
224
+ if compileErr != nil {
225
+ compileErr = & commands.CompileFailedError {Message : err .Error ()}
226
+ }
227
+ return r , compileErr
226
228
} else if req .GetPreprocess () {
227
- return r , status .Convert (builder .RunPreprocess (builderCtx ))
229
+ compileErr := builder .RunPreprocess (builderCtx )
230
+ if compileErr != nil {
231
+ compileErr = & commands.CompileFailedError {Message : err .Error ()}
232
+ }
233
+ return r , compileErr
228
234
}
229
235
230
236
// if it's a regular build, go on...
231
237
if err := builder .RunBuilder (builderCtx ); err != nil {
232
- return r , status . Convert ( err )
238
+ return r , & commands. CompileFailedError { Message : err . Error ()}
233
239
}
234
240
235
241
// If the export directory is set we assume you want to export the binaries
@@ -251,17 +257,17 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
251
257
}
252
258
logrus .WithField ("path" , exportPath ).Trace ("Saving sketch to export path." )
253
259
if err := exportPath .MkdirAll (); err != nil {
254
- return r , status . New ( codes . PermissionDenied , errors . Wrap ( err , tr ("Error creating output dir" )). Error ())
260
+ return r , & commands. PermissionDeniedError { Message : tr ("Error creating output dir" ), Cause : err }
255
261
}
256
262
257
263
// Copy all "sketch.ino.*" artifacts to the export directory
258
264
baseName , ok := builderCtx .BuildProperties .GetOk ("build.project_name" ) // == "sketch.ino"
259
265
if ! ok {
260
- return r , status . New ( codes . Internal , tr ( "Missing ' build.project_name' build property" ))
266
+ return r , & commands. MissingPlatformPropertyError { Property : " build.project_name" }
261
267
}
262
268
buildFiles , err := builderCtx .BuildPath .ReadDir ()
263
269
if err != nil {
264
- return r , status . Newf ( codes . PermissionDenied , tr ("Error reading build directory: %s " ), err )
270
+ return r , & commands. PermissionDeniedError { Message : tr ("Error reading build directory" ), Cause : err }
265
271
}
266
272
buildFiles .FilterPrefix (baseName )
267
273
for _ , buildFile := range buildFiles {
@@ -271,7 +277,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
271
277
WithField ("dest" , exportedFile ).
272
278
Trace ("Copying artifact." )
273
279
if err = buildFile .CopyTo (exportedFile ); err != nil {
274
- return r , status . New ( codes . PermissionDenied , tr ("Error copying output file %[1]s: %[2]s " , buildFile , err ))
280
+ return r , & commands. PermissionDeniedError { Message : tr ("Error copying output file %s " , buildFile ), Cause : err }
275
281
}
276
282
}
277
283
}
@@ -280,7 +286,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
280
286
for _ , lib := range builderCtx .ImportedLibraries {
281
287
rpcLib , err := lib .ToRPCLibrary ()
282
288
if err != nil {
283
- return r , status . Newf ( codes . PermissionDenied , tr ("Error converting library %[1]s to rpc struct: %[2] s" , lib .Name , err ))
289
+ return r , & commands. PermissionDeniedError { Message : tr ("Error getting information for library % s" , lib .Name ), Cause : err }
284
290
}
285
291
importedLibs = append (importedLibs , rpcLib )
286
292
}
0 commit comments