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