@@ -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 (),
@@ -66,18 +68,14 @@ func Upload(ctx context.Context, req *rpc.UploadRequest, outStream io.Writer, er
66
68
outStream ,
67
69
errStream ,
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 (),
@@ -98,17 +96,17 @@ func runProgramAction(pm *packagemanager.PackageManager,
98
96
importFile , importDir , fqbnIn , port string ,
99
97
programmerID string ,
100
98
verbose , verify , burnBootloader bool ,
101
- outStream , errStream io.Writer ) error {
99
+ outStream , errStream io.Writer ) * status. Status {
102
100
103
101
if burnBootloader && programmerID == "" {
104
- return fmt . Errorf ( "no programmer specified for burning bootloader " )
102
+ return status . New ( codes . InvalidArgument , "Programmer not specified" )
105
103
}
106
104
107
105
// FIXME: make a specification on how a port is specified via command line
108
106
if port == "" && sketch != nil && sketch .Metadata != nil {
109
107
deviceURI , err := url .Parse (sketch .Metadata .CPU .Port )
110
108
if err != nil {
111
- return fmt . Errorf ( "invalid Device URL format: %s" , err )
109
+ return status . Newf ( codes . InvalidArgument , "Invalid Device URL format: %s" , err )
112
110
}
113
111
if deviceURI .Scheme == "serial" {
114
112
port = deviceURI .Host + deviceURI .Path
@@ -120,18 +118,18 @@ func runProgramAction(pm *packagemanager.PackageManager,
120
118
fqbnIn = sketch .Metadata .CPU .Fqbn
121
119
}
122
120
if fqbnIn == "" {
123
- return fmt . Errorf ( "no Fully Qualified Board Name provided" )
121
+ return status . New ( codes . InvalidArgument , "No FQBN ( Fully Qualified Board Name) provided" )
124
122
}
125
123
fqbn , err := cores .ParseFQBN (fqbnIn )
126
124
if err != nil {
127
- return fmt .Errorf ( "incorrect FQBN: %s" , err )
125
+ return status . Newf ( codes . InvalidArgument , fmt .Sprintf ( "Invalid FQBN: %s" , err ) )
128
126
}
129
127
logrus .WithField ("fqbn" , fqbn ).Tracef ("Detected FQBN" )
130
128
131
129
// Find target board and board properties
132
130
_ , boardPlatform , board , boardProperties , buildPlatform , err := pm .ResolveFQBN (fqbn )
133
131
if err != nil {
134
- return fmt . Errorf ( "incorrect FQBN: %s" , err )
132
+ return status . Newf ( codes . InvalidArgument , "Could not resolve FQBN: %s" , err )
135
133
}
136
134
logrus .
137
135
WithField ("boardPlatform" , boardPlatform ).
@@ -148,7 +146,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
148
146
programmer = buildPlatform .Programmers [programmerID ]
149
147
}
150
148
if programmer == nil {
151
- return fmt . Errorf ( "programmer '%s' not available" , programmerID )
149
+ return status . Newf ( codes . InvalidArgument , "Programmer '%s' not available" , programmerID )
152
150
}
153
151
}
154
152
@@ -173,7 +171,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
173
171
if t , ok := props .GetOk (toolProperty ); ok {
174
172
uploadToolID = t
175
173
} else {
176
- return fmt . Errorf ( "cannot get programmer tool: undefined '%s' property" , toolProperty )
174
+ return status . Newf ( codes . FailedPrecondition , "Cannot get programmer tool: undefined '%s' property" , toolProperty )
177
175
}
178
176
}
179
177
@@ -189,7 +187,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
189
187
Trace ("Upload tool" )
190
188
191
189
if split := strings .Split (uploadToolID , ":" ); len (split ) > 2 {
192
- return fmt . Errorf ( "invalid 'upload.tool' property: %s" , uploadToolID )
190
+ return status . Newf ( codes . FailedPrecondition , "Invalid 'upload.tool' property: %s" , uploadToolID )
193
191
} else if len (split ) == 2 {
194
192
uploadToolID = split [1 ]
195
193
uploadToolPlatform = pm .GetInstalledPlatformRelease (
@@ -228,7 +226,10 @@ func runProgramAction(pm *packagemanager.PackageManager,
228
226
}
229
227
230
228
if ! uploadProperties .ContainsKey ("upload.protocol" ) && programmer == nil {
231
- return fmt .Errorf ("a programmer is required to upload for this board" )
229
+ err , _ := status .
230
+ Newf (codes .InvalidArgument , "A programmer is required to upload on this board" ).
231
+ WithDetails (& rpc.ProgrammerIsRequiredForUploadError {})
232
+ return err
232
233
}
233
234
234
235
// Set properties for verbose upload
@@ -276,13 +277,13 @@ func runProgramAction(pm *packagemanager.PackageManager,
276
277
if ! burnBootloader {
277
278
importPath , sketchName , err := determineBuildPathAndSketchName (importFile , importDir , sketch , fqbn )
278
279
if err != nil {
279
- return errors . Errorf ( "retrieving build artifacts: %s" , err )
280
+ return status . Newf ( codes . Internal , "Error finding build artifacts: %s" , err )
280
281
}
281
282
if ! importPath .Exist () {
282
- return fmt . Errorf ( "compiled sketch not found in %s" , importPath )
283
+ return status . Newf ( codes . Internal , "Compiled sketch not found in %s" , importPath )
283
284
}
284
285
if ! importPath .IsDir () {
285
- return fmt . Errorf ( "expected compiled sketch in directory %s, but is a file instead" , importPath )
286
+ return status . Newf ( codes . Internal , "Expected compiled sketch in directory %s, but is a file instead" , importPath )
286
287
}
287
288
uploadProperties .SetPath ("build.path" , importPath )
288
289
uploadProperties .Set ("build.project_name" , sketchName )
@@ -359,18 +360,18 @@ func runProgramAction(pm *packagemanager.PackageManager,
359
360
// Run recipes for upload
360
361
if burnBootloader {
361
362
if err := runTool ("erase.pattern" , uploadProperties , outStream , errStream , verbose ); err != nil {
362
- return fmt . Errorf ( " chip erase error : %s" , err )
363
+ return status . Newf ( codes . Internal , "Failed chip erase: %s" , err )
363
364
}
364
365
if err := runTool ("bootloader.pattern" , uploadProperties , outStream , errStream , verbose ); err != nil {
365
- return fmt . Errorf ( " burn bootloader error : %s" , err )
366
+ return status . Newf ( codes . Internal , "Failed to burn bootloader: %s" , err )
366
367
}
367
368
} else if programmer != nil {
368
369
if err := runTool ("program.pattern" , uploadProperties , outStream , errStream , verbose ); err != nil {
369
- return fmt . Errorf ( "programming error : %s" , err )
370
+ return status . Newf ( codes . Internal , "Failed programming : %s" , err )
370
371
}
371
372
} else {
372
373
if err := runTool ("upload.pattern" , uploadProperties , outStream , errStream , verbose ); err != nil {
373
- return fmt . Errorf ( "uploading error : %s" , err )
374
+ return status . Newf ( codes . Internal , "Failed uploading : %s" , err )
374
375
}
375
376
}
376
377
0 commit comments