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