Improve error handling in message viewer

Still not great but at least it tells you when something went wrong
This commit is contained in:
Drew DeVault 2019-03-31 14:32:26 -04:00
parent bbdf9df75e
commit f9262e4b06

View file

@ -22,6 +22,7 @@ import (
type MessageViewer struct { type MessageViewer struct {
conf *config.AercConfig conf *config.AercConfig
err error
filter *exec.Cmd filter *exec.Cmd
msg *types.MessageInfo msg *types.MessageInfo
pager *exec.Cmd pager *exec.Cmd
@ -97,17 +98,19 @@ func NewMessageViewer(conf *config.AercConfig, store *lib.MessageStore,
pager *exec.Cmd pager *exec.Cmd
pipe io.WriteCloser pipe io.WriteCloser
pagerin io.WriteCloser pagerin io.WriteCloser
term *Terminal
viewer *MessageViewer
) )
cmd, err := shlex.Split(conf.Viewer.Pager) cmd, err := shlex.Split(conf.Viewer.Pager)
if err != nil { if err != nil {
panic(err) // TODO: something useful goto handle_error
} }
pager = exec.Command(cmd[0], cmd[1:]...) pager = exec.Command(cmd[0], cmd[1:]...)
for _, f := range conf.Filters { for _, f := range conf.Filters {
cmd, err := shlex.Split(f.Command) cmd, err = shlex.Split(f.Command)
if err != nil { if err != nil {
panic(err) // TODO: Something useful goto handle_error
} }
mime := msg.BodyStructure.MIMEType + "/" + msg.BodyStructure.MIMESubType mime := msg.BodyStructure.MIMEType + "/" + msg.BodyStructure.MIMESubType
switch f.FilterType { switch f.FilterType {
@ -125,14 +128,14 @@ func NewMessageViewer(conf *config.AercConfig, store *lib.MessageStore,
pipe, _ = pager.StdinPipe() pipe, _ = pager.StdinPipe()
} }
term, _ := NewTerminal(pager) term, _ = NewTerminal(pager)
// TODO: configure multipart view. I left a spot for it in the grid // TODO: configure multipart view. I left a spot for it in the grid
body.AddChild(term).At(0, 0).Span(1, 2) body.AddChild(term).At(0, 0).Span(1, 2)
grid.AddChild(headers).At(0, 0) grid.AddChild(headers).At(0, 0)
grid.AddChild(body).At(1, 0) grid.AddChild(body).At(1, 0)
viewer := &MessageViewer{ viewer = &MessageViewer{
filter: filter, filter: filter,
grid: grid, grid: grid,
msg: msg, msg: msg,
@ -152,6 +155,14 @@ func NewMessageViewer(conf *config.AercConfig, store *lib.MessageStore,
} }
return viewer return viewer
handle_error:
viewer = &MessageViewer{
err: err,
grid: grid,
msg: msg,
}
return viewer
} }
func (mv *MessageViewer) attemptCopy() { func (mv *MessageViewer) attemptCopy() {
@ -167,20 +178,25 @@ func (mv *MessageViewer) attemptCopy() {
go func() { go func() {
_, err := io.Copy(mv.pagerin, stdout) _, err := io.Copy(mv.pagerin, stdout)
if err != nil { if err != nil {
io.WriteString(mv.sink, err.Error()) mv.err = err
mv.Invalidate()
} }
mv.pagerin.Close()
stdout.Close()
}() }()
} }
go func() { go func() {
entity, err := message.New(header, mv.source) entity, err := message.New(header, mv.source)
if err != nil { if err != nil {
io.WriteString(mv.sink, err.Error()) mv.err = err
mv.Invalidate()
return return
} }
reader := mail.NewReader(entity) reader := mail.NewReader(entity)
part, err := reader.NextPart() part, err := reader.NextPart()
if err != nil { if err != nil {
io.WriteString(mv.sink, err.Error()) mv.err = err
mv.Invalidate()
return return
} }
io.Copy(mv.sink, part.Body) io.Copy(mv.sink, part.Body)
@ -190,6 +206,11 @@ func (mv *MessageViewer) attemptCopy() {
} }
func (mv *MessageViewer) Draw(ctx *ui.Context) { func (mv *MessageViewer) Draw(ctx *ui.Context) {
if mv.err != nil {
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
ctx.Printf(0, 0, tcell.StyleDefault, "%s", mv.err.Error())
return
}
mv.grid.Draw(ctx) mv.grid.Draw(ctx)
} }
@ -204,12 +225,17 @@ func (mv *MessageViewer) OnInvalidate(fn func(d ui.Drawable)) {
} }
func (mv *MessageViewer) Event(event tcell.Event) bool { func (mv *MessageViewer) Event(event tcell.Event) bool {
if mv.term != nil {
return mv.term.Event(event) return mv.term.Event(event)
} }
return false
}
func (mv *MessageViewer) Focus(focus bool) { func (mv *MessageViewer) Focus(focus bool) {
if mv.term != nil {
mv.term.Focus(focus) mv.term.Focus(focus)
} }
}
type HeaderView struct { type HeaderView struct {
onInvalidate func(d ui.Drawable) onInvalidate func(d ui.Drawable)