Skip to content

Commit

Permalink
Handle stash failures
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Dec 24, 2020
1 parent 426f478 commit 617f098
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
8 changes: 2 additions & 6 deletions ui/pager.go
Expand Up @@ -49,10 +49,6 @@ var (

type contentRenderedMsg string
type noteSavedMsg *charm.Markdown
type stashSuccessMsg markdown
type stashErrMsg struct{ err error }

func (s stashErrMsg) Error() string { return s.err.Error() }

type pagerState int

Expand Down Expand Up @@ -316,8 +312,8 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) {
m.stashedDocument = &md
}

case stashErrMsg:
// TODO
case stashFailMsg:
delete(m.general.filesStashed, msg.markdown.localID)

case statusMessageTimeoutMsg:
m.state = pagerStateBrowse
Expand Down
8 changes: 7 additions & 1 deletion ui/stash.go
Expand Up @@ -572,7 +572,6 @@ func (m stashModel) update(msg tea.Msg) (stashModel, tea.Cmd) {
}
}

// Something was stashed. Add it to the stash listing.
case stashSuccessMsg:
md := markdown(msg)
m.addMarkdowns(&md)
Expand All @@ -582,6 +581,9 @@ func (m stashModel) update(msg tea.Msg) (stashModel, tea.Cmd) {
}
cmds = append(cmds, m.newStatusMessage("Stashed!"))

case stashFailMsg:
cmds = append(cmds, m.newStatusMessage("Couldn’t stash :("))

case statusMessageTimeoutMsg:
if applicationContext(msg) == stashContext {
m.hideStatusMessage()
Expand Down Expand Up @@ -727,6 +729,10 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd {

md := m.selectedMarkdown()

if !stashableDocTypes.Contains(md.markdownType) {
break
}

if _, alreadyStashed := m.general.filesStashed[md.localID]; alreadyStashed {
cmds = append(cmds, m.newStatusMessage("Already stashed"))
break
Expand Down
29 changes: 21 additions & 8 deletions ui/ui.go
Expand Up @@ -93,6 +93,13 @@ type stashLoadErrMsg struct{ err error }
type gotNewsMsg []*charm.Markdown
type statusMessageTimeoutMsg applicationContext
type newsLoadErrMsg struct{ err error }
type stashSuccessMsg markdown
type stashFailMsg struct {
err error
markdown markdown
}

//type stashErrMsg struct{ err error }

func (e errMsg) Error() string { return e.err.Error() }
func (e errMsg) Unwrap() error { return e.err }
Expand Down Expand Up @@ -422,9 +429,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, findNextLocalFile(m))

case stashSuccessMsg:
// Something was stashed. Update the stash listing but don't run an
// actual update on the stash since we don't want to trigger the status
// message and generally don't want any other effects.
// Something was stashed outside the file listing view. Update the
// stash listing but don't run an actual update on the stash since we
// don't want to trigger the status message and generally don't want
// any other effects.
if m.state == stateShowDocument {
md := markdown(msg)
m.stash.addMarkdowns(&md)
Expand All @@ -435,6 +443,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

case stashFailMsg:
delete(m.general.filesStashed, msg.markdown.localID)

case filteredMarkdownMsg:
if m.state == stateShowDocument {
newStashModel, cmd := m.stash.update(msg)
Expand Down Expand Up @@ -653,7 +664,7 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
if debug {
log.Println("error stashing document:", err)
}
return stashErrMsg{err}
return stashFailMsg{err, md}
}
}

Expand All @@ -669,21 +680,23 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
if debug {
log.Println("error loading document body for stashing:", err)
}
return stashErrMsg{err}
return stashFailMsg{err, md}
}
md.Body = string(data)

case NewsDoc:
newMD, err := loadMarkdownFromCharm(cc, md.ID, md.markdownType)
if err != nil {
return stashErrMsg{err}
return stashFailMsg{err, md}
}
md.Body = newMD.Body

default:
err := fmt.Errorf("user is attempting to stash an unsupported markdown type: %s", md.markdownType)
if debug {
log.Printf("user is attempting to stash an unsupported markdown type: %s", md.markdownType)
log.Println(err)
}
return stashFailMsg{err, md}
}
}

Expand All @@ -698,7 +711,7 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
if debug {
log.Println("error stashing document:", err)
}
return stashErrMsg{err}
return stashFailMsg{err, md}
}

// The server sends the whole stashed document back, but we really just
Expand Down

0 comments on commit 617f098

Please sign in to comment.