@@ -40,10 +40,12 @@ import (
4040 "github.com/pkg/errors"
4141 "github.com/segmentio/stats/v4"
4242 "github.com/sirupsen/logrus"
43+ "google.golang.org/grpc/codes"
44+ "google.golang.org/grpc/status"
4345)
4446
4547// 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 ) {
4749
4850 // There is a binding between the export binaries setting and the CLI flag to explicitly set it,
4951 // 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
8789
8890 pm := commands .GetPackageManager (req .GetInstance ().GetId ())
8991 if pm == nil {
90- return nil , errors .New ("invalid instance" )
92+ return nil , status .New (codes . InvalidArgument , "invalid instance" )
9193 }
9294
9395 logrus .Tracef ("Compile %s for %s started" , req .GetSketchPath (), req .GetFqbn ())
9496 if req .GetSketchPath () == "" {
95- return nil , fmt . Errorf ( "missing sketchPath" )
97+ return nil , status . New ( codes . InvalidArgument , "missing sketchPath" )
9698 }
9799 sketchPath := paths .New (req .GetSketchPath ())
98100 sketch , err := sketches .NewSketchFromPath (sketchPath )
99101 if err != nil {
100- return nil , fmt . Errorf ( "opening sketch: %s" , err )
102+ return nil , status . Newf ( codes . NotFound , "opening sketch: %s" , err )
101103 }
102104
103105 fqbnIn := req .GetFqbn ()
104106 if fqbnIn == "" && sketch != nil && sketch .Metadata != nil {
105107 fqbnIn = sketch .Metadata .CPU .Fqbn
106108 }
107109 if fqbnIn == "" {
108- return nil , fmt . Errorf ( "no FQBN provided" )
110+ return nil , status . New ( codes . InvalidArgument , "no FQBN provided" )
109111 }
110112 fqbn , err := cores .ParseFQBN (fqbnIn )
111113 if err != nil {
112- return nil , fmt . Errorf ( "incorrect FQBN: %s" , err )
114+ return nil , status . Newf ( codes . InvalidArgument , "incorrect FQBN: %s" , err )
113115 }
114116
115117 targetPlatform := pm .FindPlatform (& packagemanager.PlatformReference {
@@ -122,7 +124,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
122124 // "\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+
123125 // version.GetAppName()+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch)
124126 // feedback.Error(errorMessage)
125- return nil , fmt . Errorf ( "platform not installed" )
127+ return nil , status . New ( codes . NotFound , "platform not installed" )
126128 }
127129
128130 builderCtx := & types.Context {}
@@ -145,7 +147,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
145147 builderCtx .BuildPath = paths .New (req .GetBuildPath ())
146148 }
147149 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 )
149151 }
150152 builderCtx .CompilationDatabase = bldr .NewCompilationDatabase (
151153 builderCtx .BuildPath .Join ("compile_commands.json" ),
@@ -175,7 +177,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
175177 builderCtx .BuildCachePath = paths .New (req .GetBuildCachePath ())
176178 err = builderCtx .BuildCachePath .MkdirAll ()
177179 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 )
179181 }
180182 }
181183
@@ -218,14 +220,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
218220
219221 // if --preprocess or --show-properties were passed, we can stop here
220222 if req .GetShowProperties () {
221- return r , builder .RunParseHardwareAndDumpBuildProperties (builderCtx )
223+ return r , status . Convert ( builder .RunParseHardwareAndDumpBuildProperties (builderCtx ) )
222224 } else if req .GetPreprocess () {
223- return r , builder .RunPreprocess (builderCtx )
225+ return r , status . Convert ( builder .RunPreprocess (builderCtx ) )
224226 }
225227
226228 // if it's a regular build, go on...
227229 if err := builder .RunBuilder (builderCtx ); err != nil {
228- return r , err
230+ return r , status . Convert ( err )
229231 }
230232
231233 // 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
247249 }
248250 logrus .WithField ("path" , exportPath ).Trace ("Saving sketch to export path." )
249251 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 () )
251253 }
252254
253255 // Copy all "sketch.ino.*" artifacts to the export directory
254256 baseName , ok := builderCtx .BuildProperties .GetOk ("build.project_name" ) // == "sketch.ino"
255257 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" )
257259 }
258260 buildFiles , err := builderCtx .BuildPath .ReadDir ()
259261 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 )
261263 }
262264 buildFiles .FilterPrefix (baseName )
263265 for _ , buildFile := range buildFiles {
@@ -267,7 +269,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
267269 WithField ("dest" , exportedFile ).
268270 Trace ("Copying artifact." )
269271 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 () )
271273 }
272274 }
273275 }
@@ -276,7 +278,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
276278 for _ , lib := range builderCtx .ImportedLibraries {
277279 rpcLib , err := lib .ToRPCLibrary ()
278280 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 () )
280282 }
281283 importedLibs = append (importedLibs , rpcLib )
282284 }
0 commit comments