@@ -36,23 +36,25 @@ import (
36
36
properties "github.com/arduino/go-properties-orderedmap"
37
37
"github.com/pkg/errors"
38
38
"github.com/sirupsen/logrus"
39
+ "google.golang.org/grpc/codes"
40
+ "google.golang.org/grpc/status"
39
41
)
40
42
41
43
// Upload FIXMEDOC
42
- func Upload (ctx context.Context , req * rpc.UploadRequest , outStream io.Writer , errStream io.Writer ) (* rpc.UploadResponse , error ) {
44
+ func Upload (ctx context.Context , req * rpc.UploadRequest , outStream io.Writer , errStream io.Writer ) (* rpc.UploadResponse , * status. Status ) {
43
45
logrus .Tracef ("Upload %s on %s started" , req .GetSketchPath (), req .GetFqbn ())
44
46
45
47
// TODO: make a generic function to extract sketch from request
46
48
// and remove duplication in commands/compile.go
47
49
sketchPath := paths .New (req .GetSketchPath ())
48
50
sketch , err := sketches .NewSketchFromPath (sketchPath )
49
51
if err != nil && req .GetImportDir () == "" && req .GetImportFile () == "" {
50
- return nil , fmt . Errorf ( "opening sketch : %s" , err )
52
+ return nil , status . Newf ( codes . InvalidArgument , "Sketch not found : %s" , err )
51
53
}
52
54
53
55
pm := commands .GetPackageManager (req .GetInstance ().GetId ())
54
56
55
- err = runProgramAction (
57
+ return & rpc. UploadResponse {}, runProgramAction (
56
58
pm ,
57
59
sketch ,
58
60
req .GetImportFile (),
@@ -67,18 +69,14 @@ func Upload(ctx context.Context, req *rpc.UploadRequest, outStream io.Writer, er
67
69
errStream ,
68
70
req .GetDryRun (),
69
71
)
70
- if err != nil {
71
- return nil , err
72
- }
73
- return & rpc.UploadResponse {}, nil
74
72
}
75
73
76
74
// UsingProgrammer FIXMEDOC
77
- func UsingProgrammer (ctx context.Context , req * rpc.UploadUsingProgrammerRequest , outStream io.Writer , errStream io.Writer ) (* rpc.UploadUsingProgrammerResponse , error ) {
75
+ func UsingProgrammer (ctx context.Context , req * rpc.UploadUsingProgrammerRequest , outStream io.Writer , errStream io.Writer ) (* rpc.UploadUsingProgrammerResponse , * status. Status ) {
78
76
logrus .Tracef ("Upload using programmer %s on %s started" , req .GetSketchPath (), req .GetFqbn ())
79
77
80
78
if req .GetProgrammer () == "" {
81
- return nil , errors .New ("programmer not specified" )
79
+ return nil , status .New (codes . InvalidArgument , "Programmer not specified" )
82
80
}
83
81
_ , err := Upload (ctx , & rpc.UploadRequest {
84
82
Instance : req .GetInstance (),
@@ -100,17 +98,17 @@ func runProgramAction(pm *packagemanager.PackageManager,
100
98
programmerID string ,
101
99
verbose , verify , burnBootloader bool ,
102
100
outStream , errStream io.Writer ,
103
- dryRun bool ) error {
101
+ dryRun bool ) * status. Status {
104
102
105
103
if burnBootloader && programmerID == "" {
106
- return fmt . Errorf ( "no programmer specified for burning bootloader " )
104
+ return status . New ( codes . InvalidArgument , "Programmer not specified" )
107
105
}
108
106
109
107
// FIXME: make a specification on how a port is specified via command line
110
108
if port == "" && sketch != nil && sketch .Metadata != nil {
111
109
deviceURI , err := url .Parse (sketch .Metadata .CPU .Port )
112
110
if err != nil {
113
- return fmt . Errorf ( "invalid Device URL format: %s" , err )
111
+ return status . Newf ( codes . InvalidArgument , "Invalid Device URL format: %s" , err )
114
112
}
115
113
if deviceURI .Scheme == "serial" {
116
114
port = deviceURI .Host + deviceURI .Path
@@ -122,18 +120,18 @@ func runProgramAction(pm *packagemanager.PackageManager,
122
120
fqbnIn = sketch .Metadata .CPU .Fqbn
123
121
}
124
122
if fqbnIn == "" {
125
- return fmt . Errorf ( "no Fully Qualified Board Name provided" )
123
+ return status . New ( codes . InvalidArgument , "No FQBN ( Fully Qualified Board Name) provided" )
126
124
}
127
125
fqbn , err := cores .ParseFQBN (fqbnIn )
128
126
if err != nil {
129
- return fmt .Errorf ( "incorrect FQBN: %s" , err )
127
+ return status . Newf ( codes . InvalidArgument , fmt .Sprintf ( "Invalid FQBN: %s" , err ) )
130
128
}
131
129
logrus .WithField ("fqbn" , fqbn ).Tracef ("Detected FQBN" )
132
130
133
131
// Find target board and board properties
134
132
_ , boardPlatform , board , boardProperties , buildPlatform , err := pm .ResolveFQBN (fqbn )
135
133
if err != nil {
136
- return fmt . Errorf ( "incorrect FQBN: %s" , err )
134
+ return status . Newf ( codes . InvalidArgument , "Could not resolve FQBN: %s" , err )
137
135
}
138
136
logrus .
139
137
WithField ("boardPlatform" , boardPlatform ).
@@ -150,7 +148,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
150
148
programmer = buildPlatform .Programmers [programmerID ]
151
149
}
152
150
if programmer == nil {
153
- return fmt . Errorf ( "programmer '%s' not available" , programmerID )
151
+ return status . Newf ( codes . InvalidArgument , "Programmer '%s' not available" , programmerID )
154
152
}
155
153
}
156
154
@@ -175,7 +173,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
175
173
if t , ok := props .GetOk (toolProperty ); ok {
176
174
uploadToolID = t
177
175
} else {
178
- return fmt . Errorf ( "cannot get programmer tool: undefined '%s' property" , toolProperty )
176
+ return status . Newf ( codes . FailedPrecondition , "Cannot get programmer tool: undefined '%s' property" , toolProperty )
179
177
}
180
178
}
181
179
@@ -191,7 +189,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
191
189
Trace ("Upload tool" )
192
190
193
191
if split := strings .Split (uploadToolID , ":" ); len (split ) > 2 {
194
- return fmt . Errorf ( "invalid 'upload.tool' property: %s" , uploadToolID )
192
+ return status . Newf ( codes . FailedPrecondition , "Invalid 'upload.tool' property: %s" , uploadToolID )
195
193
} else if len (split ) == 2 {
196
194
uploadToolID = split [1 ]
197
195
uploadToolPlatform = pm .GetInstalledPlatformRelease (
@@ -230,7 +228,10 @@ func runProgramAction(pm *packagemanager.PackageManager,
230
228
}
231
229
232
230
if ! uploadProperties .ContainsKey ("upload.protocol" ) && programmer == nil {
233
- return fmt .Errorf ("a programmer is required to upload for this board" )
231
+ err , _ := status .
232
+ Newf (codes .InvalidArgument , "A programmer is required to upload on this board" ).
233
+ WithDetails (& rpc.ProgrammerIsRequiredForUploadError {})
234
+ return err
234
235
}
235
236
236
237
// Set properties for verbose upload
@@ -278,13 +279,13 @@ func runProgramAction(pm *packagemanager.PackageManager,
278
279
if ! burnBootloader {
279
280
importPath , sketchName , err := determineBuildPathAndSketchName (importFile , importDir , sketch , fqbn )
280
281
if err != nil {
281
- return errors . Errorf ( "retrieving build artifacts: %s" , err )
282
+ return status . Newf ( codes . Internal , "Error finding build artifacts: %s" , err )
282
283
}
283
284
if ! importPath .Exist () {
284
- return fmt . Errorf ( "compiled sketch not found in %s" , importPath )
285
+ return status . Newf ( codes . Internal , "Compiled sketch not found in %s" , importPath )
285
286
}
286
287
if ! importPath .IsDir () {
287
- return fmt . Errorf ( "expected compiled sketch in directory %s, but is a file instead" , importPath )
288
+ return status . Newf ( codes . Internal , "Expected compiled sketch in directory %s, but is a file instead" , importPath )
288
289
}
289
290
uploadProperties .SetPath ("build.path" , importPath )
290
291
uploadProperties .Set ("build.project_name" , sketchName )
@@ -368,18 +369,18 @@ func runProgramAction(pm *packagemanager.PackageManager,
368
369
// Run recipes for upload
369
370
if burnBootloader {
370
371
if err := runTool ("erase.pattern" , uploadProperties , outStream , errStream , verbose , dryRun ); err != nil {
371
- return fmt . Errorf ( " chip erase error : %s" , err )
372
+ return status . Newf ( codes . Internal , "Failed chip erase: %s" , err )
372
373
}
373
374
if err := runTool ("bootloader.pattern" , uploadProperties , outStream , errStream , verbose , dryRun ); err != nil {
374
- return fmt . Errorf ( " burn bootloader error : %s" , err )
375
+ return status . Newf ( codes . Internal , "Failed to burn bootloader: %s" , err )
375
376
}
376
377
} else if programmer != nil {
377
378
if err := runTool ("program.pattern" , uploadProperties , outStream , errStream , verbose , dryRun ); err != nil {
378
- return fmt . Errorf ( "programming error : %s" , err )
379
+ return status . Newf ( codes . Internal , "Failed programming : %s" , err )
379
380
}
380
381
} else {
381
382
if err := runTool ("upload.pattern" , uploadProperties , outStream , errStream , verbose , dryRun ); err != nil {
382
- return fmt . Errorf ( "uploading error : %s" , err )
383
+ return status . Newf ( codes . Internal , "Failed uploading : %s" , err )
383
384
}
384
385
}
385
386
0 commit comments