Skip to content

Commit 5b50759

Browse files
committed
main: give more context when running an external results in an error
1 parent 05d2c14 commit 5b50759

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

builtins.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func loadBuiltins(target string) (path string, err error) {
201201
cmd.Dir = dir
202202
err = cmd.Run()
203203
if err != nil {
204-
return "", err
204+
return "", &commandError{"failed to build", srcpath, err}
205205
}
206206
}
207207

@@ -213,7 +213,7 @@ func loadBuiltins(target string) (path string, err error) {
213213
cmd.Dir = dir
214214
err = cmd.Run()
215215
if err != nil {
216-
return "", err
216+
return "", &commandError{"failed to make static library", arpath, err}
217217
}
218218

219219
return cacheStore(arpath, outfile, commands["clang"], srcs)

main.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ var commands = map[string]string{
2525
"clang": "clang-7",
2626
}
2727

28+
// commandError is an error type to wrap os/exec.Command errors. This provides
29+
// some more information regarding what went wrong while running a command.
30+
type commandError struct {
31+
Msg string
32+
File string
33+
Err error
34+
}
35+
36+
func (e *commandError) Error() string {
37+
return e.Msg + " " + e.File + ": " + e.Err.Error()
38+
}
39+
2840
type BuildConfig struct {
2941
opt string
3042
gc string
@@ -200,7 +212,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
200212
cmd.Dir = sourceDir()
201213
err := cmd.Run()
202214
if err != nil {
203-
return err
215+
return &commandError{"failed to build", path, err}
204216
}
205217
ldflags = append(ldflags, outpath)
206218
}
@@ -216,7 +228,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
216228
cmd.Dir = sourceDir()
217229
err := cmd.Run()
218230
if err != nil {
219-
return err
231+
return &commandError{"failed to build", path, err}
220232
}
221233
ldflags = append(ldflags, outpath)
222234
}
@@ -229,7 +241,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
229241
cmd.Dir = sourceDir()
230242
err = cmd.Run()
231243
if err != nil {
232-
return err
244+
return &commandError{"failed to link", executable, err}
233245
}
234246

235247
if config.printSizes == "short" || config.printSizes == "full" {
@@ -263,7 +275,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
263275
cmd.Stderr = os.Stderr
264276
err = cmd.Run()
265277
if err != nil {
266-
return err
278+
return &commandError{"failed to extract " + format + " from", executable, err}
267279
}
268280
}
269281
return action(tmppath)
@@ -325,7 +337,11 @@ func Flash(pkgName, target, port string, config *BuildConfig) error {
325337
cmd.Stdout = os.Stdout
326338
cmd.Stderr = os.Stderr
327339
cmd.Dir = sourceDir()
328-
return cmd.Run()
340+
err := cmd.Run()
341+
if err != nil {
342+
return &commandError{"failed to flash", tmppath, err}
343+
}
344+
return nil
329345
})
330346
}
331347

@@ -392,7 +408,11 @@ func FlashGDB(pkgName, target, port string, ocdOutput bool, config *BuildConfig)
392408
cmd.Stdin = os.Stdin
393409
cmd.Stdout = os.Stdout
394410
cmd.Stderr = os.Stderr
395-
return cmd.Run()
411+
err := cmd.Run()
412+
if err != nil {
413+
return &commandError{"failed to run gdb with", tmppath, err}
414+
}
415+
return nil
396416
})
397417
}
398418

@@ -415,8 +435,9 @@ func Run(pkgName, target string, config *BuildConfig) error {
415435
// Workaround for QEMU which always exits with an error.
416436
return nil
417437
}
438+
return &commandError{"failed to run compiled binary", tmppath, err}
418439
}
419-
return err
440+
return nil
420441
} else {
421442
// Run in an emulator.
422443
args := append(spec.Emulator[1:], tmppath)
@@ -429,8 +450,9 @@ func Run(pkgName, target string, config *BuildConfig) error {
429450
// Workaround for QEMU which always exits with an error.
430451
return nil
431452
}
453+
return &commandError{"failed to run emulator with", tmppath, err}
432454
}
433-
return err
455+
return nil
434456
}
435457
})
436458
}

0 commit comments

Comments
 (0)