Skip to content

Commit

Permalink
Add better contextual help in parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
twavv committed May 7, 2021
1 parent af344e4 commit ac79d7d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/codex/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ var codexUploadCmd = &cobra.Command{
if parseErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to parse codex (%d issues):\n", len(parseErr.Errors))
for _, e := range parseErr.Errors {
_, _ = fmt.Fprintf(os.Stderr, " - %s\n (%s", red(e.Message), faint(e.Error))
_, _ = fmt.Fprintf(os.Stderr, "- %s\n (%s", red(e.Message), blue(e.Error))
if e.SourcePosition != "" {
_, _ = fmt.Fprintf(os.Stderr, " at %s", cyan(e.SourcePosition))
}

_, _ = fmt.Fprintf(os.Stderr, ")\n")

for _, line := range e.SourceInfo.SourceContext.Lines {
_, _ = fmt.Fprintf(os.Stderr, " %s %s\n", faint(">"), line)
}
}
os.Exit(1)
}
Expand All @@ -84,4 +89,5 @@ var (
red = color.New(color.FgRed, color.Bold).SprintFunc()
cyan = color.New(color.FgCyan).SprintFunc()
faint = color.New(color.Faint).SprintFunc()
blue = color.New(color.FgBlue).SprintfFunc()
)
5 changes: 5 additions & 0 deletions internal/api/upload_codex.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type CodexParseError struct {
Error string `json:"error"`
Message string `json:"message"`
SourcePosition string `json:"sourcePosition"`
SourceInfo struct{
SourceContext struct {
Lines []string `json:"lines"`
} `json:"sourceContext"`
} `json:"sourceInfo"`
}

type CodexParseFailedError struct {
Expand Down

0 comments on commit ac79d7d

Please sign in to comment.