Skip to content

Commit

Permalink
update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zachariahmiller committed May 31, 2024
1 parent 0d8f531 commit 98d44da
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
8 changes: 2 additions & 6 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ var runCmd = &cobra.Command{
Run: func(_ *cobra.Command, args []string) {
var tasksFile types.TasksFile

if _, err := os.Stat(config.TaskFileLocation); os.IsNotExist(err) {
message.Fatalf(err, "file does not exist: %s", config.TaskFileLocation)
}

err := utils.ReadYaml(config.TaskFileLocation, &tasksFile)
if err != nil {
message.Fatalf(err, "Cannot unmarshal %s", config.TaskFileLocation)
message.Fatalf(err, "Failed to open file: %s", err.Error())
}

// ensure vars are uppercase
Expand Down Expand Up @@ -194,7 +190,7 @@ func loadTasksFromLocalIncludes(includeFileLocation string) types.TasksFile {
}
err := utils.ReadYaml(fullPath, &includedTasksFile)
if err != nil {
message.Fatalf(err, "Cannot unmarshal %s", fullPath)
message.Fatalf(err, "Failed to load file: %s", err.Error())
}
return includedTasksFile
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *Runner) importTasks(includes []map[string]string, dir string, setVariab
}

if err := utils.ReadYaml(includePath, &tasksFile); err != nil {
return fmt.Errorf("unable to read included file %s: %w", includePath, err)
return fmt.Errorf("unable to read included file: %w", err)
}

// prefix task names and actions with the includes key
Expand Down
11 changes: 8 additions & 3 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ func FormatEnvVar(name, value string) string {
func ReadYaml(path string, destConfig any) error {
file, err := os.ReadFile(path)
if err != nil {
return err
return fmt.Errorf("cannot %s", err.Error())
}

return goyaml.Unmarshal(file, destConfig)
err = goyaml.Unmarshal(file, destConfig)
if err != nil {
errStr := err.Error()
lines := strings.SplitN(errStr, "\n", 2)
return fmt.Errorf("cannot unmarshal %s: %s", path, lines[0])
}
return nil
}

// MakeTempDir creates a temp directory with the maru- prefix.
Expand Down

0 comments on commit 98d44da

Please sign in to comment.