Skip to content

Commit

Permalink
fix: consistenly format error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Jun 15, 2024
1 parent b60fc0d commit f4f02e0
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 48 deletions.
6 changes: 1 addition & 5 deletions internal/tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ func NavigateTo(kind Kind, opts ...NavigateOption) tea.Cmd {
return CmdHandler(NewNavigationMsg(kind, opts...))
}

func ReportError(err error, msg string, args ...any) tea.Cmd {
return CmdHandler(NewErrorMsg(err, msg, args...))
}

func ReportInfo(msg string, args ...any) tea.Cmd {
return CmdHandler(InfoMsg(fmt.Sprintf(msg, args...)))
}
Expand All @@ -27,6 +23,6 @@ func OpenVim(path string) tea.Cmd {
// messages get queued up?
c := exec.Command("vim", path)
return tea.ExecProcess(c, func(err error) tea.Msg {
return NewErrorMsg(err, "opening vim")
return ReportError(fmt.Errorf("opening vim: %w", err))()
})
}
9 changes: 9 additions & 0 deletions internal/tui/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tui

import tea "github.com/charmbracelet/bubbletea"

func ReportError(err error) tea.Cmd {
return CmdHandler(ErrorMsg(err))
}

type ErrorMsg error
4 changes: 2 additions & 2 deletions internal/tui/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,13 @@ func (h *Helpers) CreateTasks(cmd string, fn task.Func, ids ...resource.ID) tea.
case 1:
task, err := fn(ids[0])
if err != nil {
return ReportError(err, "creating task")
return ReportError(fmt.Errorf("creating task: %w", err))
}
return NewNavigationMsg(TaskKind, WithParent(task))
default:
group, err := h.TaskService.CreateGroup(cmd, fn, ids...)
if err != nil {
return ReportError(err, "creating task group")
return ReportError(fmt.Errorf("creating task group: %w", err))
}
return NewNavigationMsg(TaskGroupKind, WithParent(group))
}
Expand Down
23 changes: 0 additions & 23 deletions internal/tui/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
// NavigationMsg is an instruction to navigate to a page.
type NavigationMsg struct {
Page Page
Tab string
}

func NewNavigationMsg(kind Kind, opts ...NavigateOption) NavigationMsg {
Expand All @@ -27,30 +26,8 @@ func WithParent(parent resource.Resource) NavigateOption {
}
}

func WithTab(tab string) NavigateOption {
return func(msg *NavigationMsg) {
msg.Tab = tab
}
}

type SetActiveTabMsg string

type InfoMsg string

type ErrorMsg struct {
Error error
Message string
Args []any
}

func NewErrorMsg(err error, msg string, args ...any) ErrorMsg {
return ErrorMsg{
Error: err,
Message: msg,
Args: args,
}
}

// FilterFocusReqMsg is a request to focus the filter widget. FilterFocusAckMsg
// should be sent in response to ackowledge the request.
type FilterFocusReqMsg struct{}
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/module/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func ReloadModules(firsttime bool, modules tui.ModuleService) tea.Cmd {
return func() tea.Msg {
added, removed, err := modules.Reload()
if err != nil {
return tui.NewErrorMsg(err, "reloading modules")
return tui.ReportError(fmt.Errorf("reloading modules: %w", err))()
}
if firsttime {
return tui.InfoMsg(
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/module/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (m list) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, keys.Common.Plan):
workspaceIDs, err := m.pruneModulesWithoutCurrentWorkspace()
if err != nil {
return m, tui.ReportError(err, "")
return m, tui.ReportError(err)
}
fn := func(workspaceID resource.ID) (*task.Task, error) {
return m.RunService.Plan(workspaceID, createRunOptions)
Expand All @@ -128,7 +128,7 @@ func (m list) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, keys.Common.Apply):
workspaceIDs, err := m.pruneModulesWithoutCurrentWorkspace()
if err != nil {
return m, tui.ReportError(err, "")
return m, tui.ReportError(err)
}
fn := func(workspaceID resource.ID) (*task.Task, error) {
return m.RunService.ApplyOnly(workspaceID, createRunOptions)
Expand Down
4 changes: 3 additions & 1 deletion internal/tui/split/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package split

import (
"fmt"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -132,7 +134,7 @@ func (m Model[R]) Update(msg tea.Msg) (Model[R], tea.Cmd) {
// Create model
model, err := m.maker.Make(row.Value, m.previewWidth(), m.previewHeight())
if err != nil {
return m, tui.ReportError(err, "making model for preview")
return m, tui.ReportError(fmt.Errorf("making model for preview: %w", err))
}
// Cache newly created model
m.cache.Put(row.ID, model)
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/task/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (m groupModel) Init() tea.Cmd {
var cmds []tea.Cmd
if len(m.group.CreateErrors) > 0 {
err := fmt.Errorf("failed to create %d tasks: see logs", len(m.group.CreateErrors))
cmds = append(cmds, tui.ReportError(err, ""))
cmds = append(cmds, tui.ReportError(err))
}
cmds = append(cmds, func() tea.Msg {
// Seed table with task group's tasks
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/task/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (m List) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, keys.Common.Apply):
runIDs, err := m.pruneApplyableTasks()
if err != nil {
return m, tui.ReportError(err, "applying tasks")
return m, tui.ReportError(fmt.Errorf("applying tasks: %w", err))
}
return m, tui.YesNoPrompt(
fmt.Sprintf("Apply %d plans?", len(runIDs)),
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/task/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
if err := m.viewport.AppendContent(msg.output, msg.eof); err != nil {
return m, tui.ReportError(err, "")
return m, tui.ReportError(err)
}
if !msg.eof {
cmds = append(cmds, m.getOutput)
Expand Down Expand Up @@ -321,7 +321,7 @@ func (m model) getOutput() tea.Msg {
if err == io.EOF {
msg.eof = true
} else if err != nil {
return tui.NewErrorMsg(err, "reading task output")
return tui.ReportError(errors.New("reading task output"))()
}
msg.output = string(m.buf[:n])
return msg
Expand Down
8 changes: 2 additions & 6 deletions internal/tui/top/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tui.NavigationMsg:
created, err := m.setCurrent(msg.Page)
if err != nil {
return m, tui.ReportError(err, "setting current page")
return m, tui.ReportError(fmt.Errorf("setting current page: %w", err))
}
if created {
cmds = append(cmds, m.currentModel().Init())
}
case tui.ErrorMsg:
if msg.Error != nil {
err := msg.Error
msg := fmt.Sprintf(msg.Message, msg.Args...)
m.err = fmt.Errorf("%s: %w", msg, err)
}
m.err = error(msg)
case tui.InfoMsg:
m.info = string(msg)
case tea.WindowSizeMsg:
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (m list) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if row, ok := m.table.CurrentRow(); ok {
return m, func() tea.Msg {
if err := m.svc.SelectWorkspace(row.Value.ModuleID(), row.Value.ID); err != nil {
return tui.NewErrorMsg(err, "setting current workspace")
return tui.ReportError(fmt.Errorf("setting current workspace: %w", err))()
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/tui/workspace/resource_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m resourceList) Init() tea.Cmd {
return func() tea.Msg {
state, err := m.states.Get(m.workspace.GetID())
if err != nil {
return tui.ReportError(err, "initializing state model")
return tui.ReportError(fmt.Errorf("initializing state model: %w", err))
}
return initState(state)
}
Expand All @@ -111,7 +111,7 @@ func (m resourceList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case reloadedMsg:
m.reloading = false
if msg.err != nil {
return m, tui.ReportError(msg.err, "reloading state failed")
return m, tui.ReportError(fmt.Errorf("reloading state failed: %w", msg.err))
}
return m, tui.ReportInfo("reloading finished")
case tea.KeyMsg:
Expand All @@ -122,7 +122,7 @@ func (m resourceList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case key.Matches(msg, resourcesKeys.Reload):
if m.reloading {
return m, tui.ReportError(errors.New("reloading in progress"), "")
return m, tui.ReportError(errors.New("reloading in progress"))
}
m.reloading = true
return m, func() tea.Msg {
Expand Down

0 comments on commit f4f02e0

Please sign in to comment.