Make general error msg a struct for type safety

This commit is contained in:
Christian Rocha 2020-08-21 16:13:38 -04:00 committed by Christian Muehlhaeuser
commit 9db7c2e3ad
3 changed files with 14 additions and 13 deletions

View file

@ -431,7 +431,7 @@ func renderWithGlamour(m pagerModel, md string) tea.Cmd {
if debug {
log.Println("error rendering with Glamour:", err)
}
return errMsg(err)
return errMsg{err}
}
return contentRenderedMsg(s)
}
@ -440,7 +440,7 @@ func renderWithGlamour(m pagerModel, md string) tea.Cmd {
func saveDocumentNote(cc *charm.Client, id int, note string) tea.Cmd {
if cc == nil {
return func() tea.Msg {
return errMsg(errors.New("can't set note; no charm client"))
return errMsg{errors.New("can't set note; no charm client")}
}
}
return func() tea.Msg {
@ -448,7 +448,7 @@ func saveDocumentNote(cc *charm.Client, id int, note string) tea.Cmd {
if debug {
log.Println("error saving note:", err)
}
return errMsg(err)
return errMsg{err}
}
return noteSavedMsg(&charm.Markdown{ID: id, Note: note})
}
@ -480,7 +480,7 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
if debug {
log.Println("error stashing document:", err)
}
return errMsg(err)
return errMsg{err}
}
// We really just need to know the ID so we can operate on this newly

View file

@ -764,7 +764,7 @@ func loadRemoteMarkdown(cc *charm.Client, id int, t markdownType) tea.Cmd {
if debug {
log.Println("error loading remote markdown:", err)
}
return errMsg(err)
return errMsg{err}
}
return fetchedMarkdownMsg(&markdown{
@ -777,10 +777,10 @@ func loadRemoteMarkdown(cc *charm.Client, id int, t markdownType) tea.Cmd {
func loadLocalMarkdown(md *markdown) tea.Cmd {
return func() tea.Msg {
if md.markdownType != localMarkdown {
return errMsg(errors.New("could not load local file: not a local file"))
return errMsg{errors.New("could not load local file: not a local file")}
}
if md.localPath == "" {
return errMsg(errors.New("could not load file: missing path"))
return errMsg{errors.New("could not load file: missing path")}
}
data, err := ioutil.ReadFile(md.localPath)
@ -788,7 +788,7 @@ func loadLocalMarkdown(md *markdown) tea.Cmd {
if debug {
log.Println("error reading local markdown:", err)
}
return errMsg(err)
return errMsg{err}
}
md.Body = string(data)
return fetchedMarkdownMsg(md)
@ -799,7 +799,7 @@ func deleteStashedItem(cc *charm.Client, id int) tea.Cmd {
return func() tea.Msg {
err := cc.DeleteMarkdown(id)
if err != nil {
return errMsg(err)
return errMsg{err}
}
return deletedStashedItemMsg(id)
}

View file

@ -50,7 +50,7 @@ func NewProgram(style string, cfg Config) *tea.Program {
// MESSAGES
type errMsg error
type errMsg struct{ err error }
type newCharmClientMsg *charm.Client
type sshAuthErrMsg struct{}
type keygenFailedMsg struct{ err error }
@ -67,6 +67,7 @@ type gotNewsMsg []*charm.Markdown
type statusMessageTimeoutMsg applicationContext
type newsLoadErrMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() }
func (k keygenFailedMsg) Error() string { return k.err.Error() }
func (s stashLoadErrMsg) Error() string { return s.err.Error() }
func (s newsLoadErrMsg) Error() string { return s.err.Error() }
@ -407,7 +408,7 @@ func findLocalFiles() tea.Msg {
if debug {
log.Println("error finding local files:", err)
}
return errMsg(err)
return errMsg{err}
}
ch, err := gitcha.FindFiles(cwd, []string{"*.md"})
@ -440,7 +441,7 @@ func newCharmClient(identityFile *string) tea.Cmd {
return func() tea.Msg {
cfg, err := charm.ConfigFromEnv()
if err != nil {
return errMsg(err)
return errMsg{err}
}
if identityFile != nil {
@ -457,7 +458,7 @@ func newCharmClient(identityFile *string) tea.Cmd {
if debug {
log.Println("error creating new charm client:", err)
}
return errMsg(err)
return errMsg{err}
}
return newCharmClientMsg(cc)