@@ -40,10 +40,12 @@ import (
40
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"
43
45
)
44
46
45
47
// Compile FIXMEDOC
46
- func Compile (ctx context.Context , req * rpc.CompileRequest , outStream , errStream io.Writer , debug bool ) (r * rpc.CompileResponse , e error ) {
48
+ func Compile (ctx context.Context , req * rpc.CompileRequest , outStream , errStream io.Writer , debug bool ) (r * rpc.CompileResponse , e * status. Status ) {
47
49
48
50
// There is a binding between the export binaries setting and the CLI flag to explicitly set it,
49
51
// since we want this binding to work also for the gRPC interface we must read it here in this
@@ -87,29 +89,29 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
87
89
88
90
pm := commands .GetPackageManager (req .GetInstance ().GetId ())
89
91
if pm == nil {
90
- return nil , errors .New ("invalid instance" )
92
+ return nil , status .New (codes . InvalidArgument , "invalid instance" )
91
93
}
92
94
93
95
logrus .Tracef ("Compile %s for %s started" , req .GetSketchPath (), req .GetFqbn ())
94
96
if req .GetSketchPath () == "" {
95
- return nil , fmt . Errorf ( "missing sketchPath" )
97
+ return nil , status . New ( codes . InvalidArgument , "missing sketchPath" )
96
98
}
97
99
sketchPath := paths .New (req .GetSketchPath ())
98
100
sketch , err := sketches .NewSketchFromPath (sketchPath )
99
101
if err != nil {
100
- return nil , fmt . Errorf ( "opening sketch: %s" , err )
102
+ return nil , status . Newf ( codes . NotFound , "opening sketch: %s" , err )
101
103
}
102
104
103
105
fqbnIn := req .GetFqbn ()
104
106
if fqbnIn == "" && sketch != nil && sketch .Metadata != nil {
105
107
fqbnIn = sketch .Metadata .CPU .Fqbn
106
108
}
107
109
if fqbnIn == "" {
108
- return nil , fmt . Errorf ( "no FQBN provided" )
110
+ return nil , status . New ( codes . InvalidArgument , "no FQBN provided" )
109
111
}
110
112
fqbn , err := cores .ParseFQBN (fqbnIn )
111
113
if err != nil {
112
- return nil , fmt . Errorf ( "incorrect FQBN: %s" , err )
114
+ return nil , status . Newf ( codes . InvalidArgument , "incorrect FQBN: %s" , err )
113
115
}
114
116
115
117
targetPlatform := pm .FindPlatform (& packagemanager.PlatformReference {
@@ -122,7 +124,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
122
124
// "\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+
123
125
// version.GetAppName()+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch)
124
126
// feedback.Error(errorMessage)
125
- return nil , fmt . Errorf ( "platform not installed" )
127
+ return nil , status . New ( codes . NotFound , "platform not installed" )
126
128
}
127
129
128
130
builderCtx := & types.Context {}
@@ -145,7 +147,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
145
147
builderCtx .BuildPath = paths .New (req .GetBuildPath ())
146
148
}
147
149
if err = builderCtx .BuildPath .MkdirAll (); err != nil {
148
- return nil , fmt . Errorf ( "cannot create build directory: %s" , err )
150
+ return nil , status . Newf ( codes . PermissionDenied , "cannot create build directory: %s" , err )
149
151
}
150
152
builderCtx .CompilationDatabase = bldr .NewCompilationDatabase (
151
153
builderCtx .BuildPath .Join ("compile_commands.json" ),
@@ -175,7 +177,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
175
177
builderCtx .BuildCachePath = paths .New (req .GetBuildCachePath ())
176
178
err = builderCtx .BuildCachePath .MkdirAll ()
177
179
if err != nil {
178
- return nil , fmt . Errorf ( "cannot create build cache directory: %s" , err )
180
+ return nil , status . Newf ( codes . PermissionDenied , "cannot create build cache directory: %s" , err )
179
181
}
180
182
}
181
183
@@ -218,14 +220,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
218
220
219
221
// if --preprocess or --show-properties were passed, we can stop here
220
222
if req .GetShowProperties () {
221
- return r , builder .RunParseHardwareAndDumpBuildProperties (builderCtx )
223
+ return r , status . Convert ( builder .RunParseHardwareAndDumpBuildProperties (builderCtx ) )
222
224
} else if req .GetPreprocess () {
223
- return r , builder .RunPreprocess (builderCtx )
225
+ return r , status . Convert ( builder .RunPreprocess (builderCtx ) )
224
226
}
225
227
226
228
// if it's a regular build, go on...
227
229
if err := builder .RunBuilder (builderCtx ); err != nil {
228
- return r , err
230
+ return r , status . Convert ( err )
229
231
}
230
232
231
233
// If the export directory is set we assume you want to export the binaries
@@ -247,17 +249,17 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
247
249
}
248
250
logrus .WithField ("path" , exportPath ).Trace ("Saving sketch to export path." )
249
251
if err := exportPath .MkdirAll (); err != nil {
250
- return r , errors .Wrap (err , "creating output dir" )
252
+ return r , status . New ( codes . PermissionDenied , errors .Wrap (err , "creating output dir" ). Error () )
251
253
}
252
254
253
255
// Copy all "sketch.ino.*" artifacts to the export directory
254
256
baseName , ok := builderCtx .BuildProperties .GetOk ("build.project_name" ) // == "sketch.ino"
255
257
if ! ok {
256
- return r , errors .New ("missing 'build.project_name' build property" )
258
+ return r , status .New (codes . Internal , "missing 'build.project_name' build property" )
257
259
}
258
260
buildFiles , err := builderCtx .BuildPath .ReadDir ()
259
261
if err != nil {
260
- return r , errors . Errorf ( "reading build directory: %s" , err )
262
+ return r , status . Newf ( codes . PermissionDenied , "reading build directory: %s" , err )
261
263
}
262
264
buildFiles .FilterPrefix (baseName )
263
265
for _ , buildFile := range buildFiles {
@@ -267,7 +269,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
267
269
WithField ("dest" , exportedFile ).
268
270
Trace ("Copying artifact." )
269
271
if err = buildFile .CopyTo (exportedFile ); err != nil {
270
- return r , errors .Wrapf (err , "copying output file %s" , buildFile )
272
+ return r , status . New ( codes . PermissionDenied , errors .Wrapf (err , "copying output file %s" , buildFile ). Error () )
271
273
}
272
274
}
273
275
}
@@ -276,7 +278,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
276
278
for _ , lib := range builderCtx .ImportedLibraries {
277
279
rpcLib , err := lib .ToRPCLibrary ()
278
280
if err != nil {
279
- return r , fmt .Errorf ("converting library %s to rpc struct: %w" , lib .Name , err )
281
+ return r , status . Newf ( codes . PermissionDenied , fmt .Errorf ("converting library %s to rpc struct: %w" , lib .Name , err ). Error () )
280
282
}
281
283
importedLibs = append (importedLibs , rpcLib )
282
284
}
0 commit comments