@@ -17,7 +17,6 @@ package compile
1717
1818import (
1919 "context"
20- "fmt"
2120 "io"
2221 "path/filepath"
2322 "sort"
@@ -41,12 +40,14 @@ import (
4140 "github.com/pkg/errors"
4241 "github.com/segmentio/stats/v4"
4342 "github.com/sirupsen/logrus"
43+ "google.golang.org/grpc/codes"
44+ "google.golang.org/grpc/status"
4445)
4546
4647var tr = i18n .Tr
4748
4849// Compile FIXMEDOC
49- func Compile (ctx context.Context , req * rpc.CompileRequest , outStream , errStream io.Writer , debug bool ) (r * rpc.CompileResponse , e error ) {
50+ func Compile (ctx context.Context , req * rpc.CompileRequest , outStream , errStream io.Writer , debug bool ) (r * rpc.CompileResponse , e * status. Status ) {
5051
5152 // There is a binding between the export binaries setting and the CLI flag to explicitly set it,
5253 // since we want this binding to work also for the gRPC interface we must read it here in this
@@ -90,29 +91,29 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
9091
9192 pm := commands .GetPackageManager (req .GetInstance ().GetId ())
9293 if pm == nil {
93- return nil , errors .New (tr ("invalid instance" ))
94+ return nil , status .New (codes . InvalidArgument , tr ("Invalid instance" ))
9495 }
9596
9697 logrus .Tracef ("Compile %s for %s started" , req .GetSketchPath (), req .GetFqbn ())
9798 if req .GetSketchPath () == "" {
98- return nil , fmt . Errorf ( tr ("missing sketchPath " ))
99+ return nil , status . New ( codes . InvalidArgument , tr ("Missing sketch path " ))
99100 }
100101 sketchPath := paths .New (req .GetSketchPath ())
101102 sk , err := sketch .New (sketchPath )
102103 if err != nil {
103- return nil , fmt . Errorf ( tr ("opening sketch: %s" ), err )
104+ return nil , status . Newf ( codes . NotFound , tr ("Error opening sketch: %s" ), err )
104105 }
105106
106107 fqbnIn := req .GetFqbn ()
107108 if fqbnIn == "" && sk != nil && sk .Metadata != nil {
108109 fqbnIn = sk .Metadata .CPU .Fqbn
109110 }
110111 if fqbnIn == "" {
111- return nil , fmt . Errorf ( tr ("no FQBN provided" ))
112+ return nil , status . New ( codes . InvalidArgument , tr ("No FQBN (Fully Qualified Board Name) provided" ))
112113 }
113114 fqbn , err := cores .ParseFQBN (fqbnIn )
114115 if err != nil {
115- return nil , fmt . Errorf ( tr ("incorrect FQBN: %s" ), err )
116+ return nil , status . Newf ( codes . InvalidArgument , tr ("Invalid FQBN: %s" ), err )
116117 }
117118
118119 targetPlatform := pm .FindPlatform (& packagemanager.PlatformReference {
@@ -125,7 +126,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
125126 // "\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+
126127 // version.GetAppName()+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch)
127128 // feedback.Error(errorMessage)
128- return nil , fmt . Errorf ( tr ("platform not installed" ))
129+ return nil , status . New ( codes . NotFound , tr ("Platform not installed" ))
129130 }
130131
131132 builderCtx := & types.Context {}
@@ -148,7 +149,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
148149 builderCtx .BuildPath = paths .New (req .GetBuildPath ())
149150 }
150151 if err = builderCtx .BuildPath .MkdirAll (); err != nil {
151- return nil , fmt . Errorf ( tr ("cannot create build directory: %s" ), err )
152+ return nil , status . Newf ( codes . PermissionDenied , tr ("Cannot create build directory: %s" ), err )
152153 }
153154 builderCtx .CompilationDatabase = bldr .NewCompilationDatabase (
154155 builderCtx .BuildPath .Join ("compile_commands.json" ),
@@ -178,7 +179,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
178179 builderCtx .BuildCachePath = paths .New (req .GetBuildCachePath ())
179180 err = builderCtx .BuildCachePath .MkdirAll ()
180181 if err != nil {
181- return nil , fmt . Errorf ( tr ("cannot create build cache directory: %s" ), err )
182+ return nil , status . Newf ( codes . PermissionDenied , tr ("Cannot create build cache directory: %s" ), err )
182183 }
183184 }
184185
@@ -221,14 +222,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
221222
222223 // if --preprocess or --show-properties were passed, we can stop here
223224 if req .GetShowProperties () {
224- return r , builder .RunParseHardwareAndDumpBuildProperties (builderCtx )
225+ return r , status . Convert ( builder .RunParseHardwareAndDumpBuildProperties (builderCtx ) )
225226 } else if req .GetPreprocess () {
226- return r , builder .RunPreprocess (builderCtx )
227+ return r , status . Convert ( builder .RunPreprocess (builderCtx ) )
227228 }
228229
229230 // if it's a regular build, go on...
230231 if err := builder .RunBuilder (builderCtx ); err != nil {
231- return r , err
232+ return r , status . Convert ( err )
232233 }
233234
234235 // If the export directory is set we assume you want to export the binaries
@@ -250,17 +251,17 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
250251 }
251252 logrus .WithField ("path" , exportPath ).Trace ("Saving sketch to export path." )
252253 if err := exportPath .MkdirAll (); err != nil {
253- return r , errors .Wrap (err , tr ("creating output dir" ))
254+ return r , status . New ( codes . PermissionDenied , errors .Wrap (err , tr ("Error creating output dir" )). Error ( ))
254255 }
255256
256257 // Copy all "sketch.ino.*" artifacts to the export directory
257258 baseName , ok := builderCtx .BuildProperties .GetOk ("build.project_name" ) // == "sketch.ino"
258259 if ! ok {
259- return r , errors .New (tr ("missing 'build.project_name' build property" ))
260+ return r , status .New (codes . Internal , tr ("Missing 'build.project_name' build property" ))
260261 }
261262 buildFiles , err := builderCtx .BuildPath .ReadDir ()
262263 if err != nil {
263- return r , errors . Errorf ( tr ("reading build directory: %s" ), err )
264+ return r , status . Newf ( codes . PermissionDenied , tr ("Error reading build directory: %s" ), err )
264265 }
265266 buildFiles .FilterPrefix (baseName )
266267 for _ , buildFile := range buildFiles {
@@ -270,7 +271,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
270271 WithField ("dest" , exportedFile ).
271272 Trace ("Copying artifact." )
272273 if err = buildFile .CopyTo (exportedFile ); err != nil {
273- return r , errors . Wrapf ( err , tr ("copying output file %s" ) , buildFile )
274+ return r , status . New ( codes . PermissionDenied , tr ("Error copying output file %[1]s: %[2]s" , buildFile , err ) )
274275 }
275276 }
276277 }
@@ -279,7 +280,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
279280 for _ , lib := range builderCtx .ImportedLibraries {
280281 rpcLib , err := lib .ToRPCLibrary ()
281282 if err != nil {
282- return r , fmt . Errorf ( tr ("converting library %[1]s to rpc struct: %[2]w" ) , lib .Name , err )
283+ return r , status . Newf ( codes . PermissionDenied , tr ("Error converting library %[1]s to rpc struct: %[2]s" , lib .Name , err ) )
283284 }
284285 importedLibs = append (importedLibs , rpcLib )
285286 }
0 commit comments