mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Refactor a number of widgets to display client errors
Rather than swallowing or crashing, display appropriate errors
This commit is contained in:
parent
ff77a3deb0
commit
8835f532cc
@ -15,6 +15,7 @@ type Widget struct {
|
||||
|
||||
monitors []datadog.Monitor
|
||||
settings *Settings
|
||||
err error
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
@ -37,10 +38,12 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.err = nil
|
||||
monitors, monitorErr := widget.Monitors()
|
||||
|
||||
if monitorErr != nil {
|
||||
widget.monitors = nil
|
||||
widget.err = monitorErr
|
||||
widget.SetItemCount(0)
|
||||
widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, monitorErr.Error(), true })
|
||||
return
|
||||
@ -73,6 +76,12 @@ func (widget *Widget) content() (string, string, bool) {
|
||||
triggeredMonitors := widget.monitors
|
||||
var str string
|
||||
|
||||
title := widget.CommonSettings().Title
|
||||
|
||||
if widget.err != nil {
|
||||
return title, widget.err.Error(), true
|
||||
}
|
||||
|
||||
if len(triggeredMonitors) > 0 {
|
||||
str += fmt.Sprintf(
|
||||
" %s\n",
|
||||
@ -93,7 +102,7 @@ func (widget *Widget) content() (string, string, bool) {
|
||||
)
|
||||
}
|
||||
|
||||
return widget.CommonSettings().Title, str, false
|
||||
return title, str, false
|
||||
}
|
||||
|
||||
func (widget *Widget) openItem() {
|
||||
|
@ -31,6 +31,11 @@ func (widget *Widget) display() {
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
title := widget.settings.common.Title
|
||||
calEvents := widget.calEvents
|
||||
|
||||
if widget.err != nil {
|
||||
return title, widget.err.Error(), true
|
||||
}
|
||||
|
||||
if (calEvents == nil) || (len(calEvents) == 0) {
|
||||
return title, "No calendar events", false
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ type Widget struct {
|
||||
app *tview.Application
|
||||
calEvents []*CalEvent
|
||||
settings *Settings
|
||||
err error
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
@ -45,8 +46,10 @@ func (widget *Widget) Refresh() {
|
||||
func (widget *Widget) fetchAndDisplayEvents() {
|
||||
calEvents, err := widget.Fetch()
|
||||
if err != nil {
|
||||
widget.err = err
|
||||
widget.calEvents = []*CalEvent{}
|
||||
} else {
|
||||
widget.err = nil
|
||||
widget.calEvents = calEvents
|
||||
}
|
||||
|
||||
|
@ -9,13 +9,17 @@ func (widget *Widget) display() {
|
||||
}
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
title := widget.CommonSettings().Title
|
||||
if widget.err != nil {
|
||||
return title, widget.err.Error(), true
|
||||
}
|
||||
|
||||
project := widget.currentGerritProject()
|
||||
if project == nil {
|
||||
return widget.CommonSettings().Title, "Gerrit project data is unavailable", true
|
||||
return title, "Gerrit project data is unavailable", true
|
||||
}
|
||||
|
||||
title := fmt.Sprintf("%s- %s", widget.CommonSettings().Title, widget.title(project))
|
||||
title = fmt.Sprintf("%s- %s", widget.CommonSettings().Title, widget.title(project))
|
||||
|
||||
_, _, width, _ := widget.View.GetRect()
|
||||
str := widget.settings.common.SigilStr(len(widget.GerritProjects), widget.Idx, width) + "\n"
|
||||
|
@ -23,6 +23,7 @@ type Widget struct {
|
||||
|
||||
selected int
|
||||
settings *Settings
|
||||
err error
|
||||
}
|
||||
|
||||
var (
|
||||
@ -76,14 +77,16 @@ func (widget *Widget) Refresh() {
|
||||
}
|
||||
gerrit, err := glb.NewClient(gerritUrl, httpClient)
|
||||
if err != nil {
|
||||
widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, err.Error(), true })
|
||||
return
|
||||
}
|
||||
widget.gerrit = gerrit
|
||||
widget.GerritProjects = widget.buildProjectCollection(widget.settings.projects)
|
||||
|
||||
for _, project := range widget.GerritProjects {
|
||||
project.Refresh(widget.settings.username)
|
||||
widget.err = err
|
||||
widget.gerrit = nil
|
||||
widget.GerritProjects = nil
|
||||
} else {
|
||||
widget.err = nil
|
||||
widget.gerrit = gerrit
|
||||
widget.GerritProjects = widget.buildProjectCollection(widget.settings.projects)
|
||||
for _, project := range widget.GerritProjects {
|
||||
project.Refresh(widget.settings.username)
|
||||
}
|
||||
}
|
||||
|
||||
widget.display()
|
||||
|
@ -14,6 +14,7 @@ type Widget struct {
|
||||
|
||||
settings *Settings
|
||||
cells []*sheets.ValueRange
|
||||
err error
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
@ -29,7 +30,8 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
cells, _ := widget.Fetch()
|
||||
cells, err := widget.Fetch()
|
||||
widget.err = err
|
||||
widget.cells = cells
|
||||
|
||||
widget.Redraw(widget.content)
|
||||
@ -38,8 +40,13 @@ func (widget *Widget) Refresh() {
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
title := widget.CommonSettings().Title
|
||||
if widget.err != nil {
|
||||
return title, widget.err.Error(), true
|
||||
}
|
||||
|
||||
if widget.cells == nil {
|
||||
return widget.CommonSettings().Title, "error 1", false
|
||||
return title, "No cells", false
|
||||
}
|
||||
|
||||
res := ""
|
||||
@ -49,5 +56,5 @@ func (widget *Widget) content() (string, string, bool) {
|
||||
res += fmt.Sprintf("%s\t[%s]%s\n", cells[i], widget.settings.colors.values, widget.cells[i].Values[0][0])
|
||||
}
|
||||
|
||||
return widget.CommonSettings().Title, res, false
|
||||
return title, res, false
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ type Widget struct {
|
||||
|
||||
stories []Story
|
||||
settings *Settings
|
||||
err error
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
@ -43,25 +44,22 @@ func (widget *Widget) Refresh() {
|
||||
}
|
||||
|
||||
storyIds, err := GetStories(widget.settings.storyType)
|
||||
if storyIds == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, err.Error(), true })
|
||||
return
|
||||
}
|
||||
var stories []Story
|
||||
for idx := 0; idx < widget.settings.numberOfStories; idx++ {
|
||||
story, e := GetStory(storyIds[idx])
|
||||
if e == nil {
|
||||
stories = append(stories, story)
|
||||
widget.err = err
|
||||
widget.stories = nil
|
||||
widget.SetItemCount(0)
|
||||
} else {
|
||||
var stories []Story
|
||||
for idx := 0; idx < widget.settings.numberOfStories; idx++ {
|
||||
story, e := GetStory(storyIds[idx])
|
||||
if e == nil {
|
||||
stories = append(stories, story)
|
||||
}
|
||||
}
|
||||
widget.stories = stories
|
||||
widget.SetItemCount(len(stories))
|
||||
}
|
||||
|
||||
widget.stories = stories
|
||||
widget.SetItemCount(len(stories))
|
||||
|
||||
widget.Render()
|
||||
}
|
||||
|
||||
@ -74,6 +72,11 @@ func (widget *Widget) Render() {
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
title := fmt.Sprintf("%s - %s stories", widget.CommonSettings().Title, widget.settings.storyType)
|
||||
|
||||
if widget.err != nil {
|
||||
return title, widget.err.Error(), true
|
||||
}
|
||||
|
||||
stories := widget.stories
|
||||
if stories == nil || len(stories) == 0 {
|
||||
return title, "No stories to display", false
|
||||
|
@ -46,11 +46,12 @@ func (widget *Widget) Refresh() {
|
||||
if err != nil {
|
||||
widget.err = err
|
||||
widget.result = nil
|
||||
return
|
||||
widget.SetItemCount(0)
|
||||
} else {
|
||||
widget.err = nil
|
||||
widget.result = searchResult
|
||||
widget.SetItemCount(len(searchResult.Issues))
|
||||
}
|
||||
widget.err = nil
|
||||
widget.result = searchResult
|
||||
widget.SetItemCount(len(searchResult.Issues))
|
||||
widget.Render()
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
widget.SetDisplayFunction(widget.Refresh)
|
||||
widget.View.SetWordWrap(true)
|
||||
widget.View.SetWrap(settings.wrapText)
|
||||
|
||||
@ -61,7 +61,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
// Refresh is only called once on start-up. Its job is to display the
|
||||
// text files that first time. After that, the watcher takes over
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.display()
|
||||
widget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
func (widget *Widget) HelpText() string {
|
||||
@ -70,10 +70,6 @@ func (widget *Widget) HelpText() string {
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) display() {
|
||||
widget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
title := fmt.Sprintf("[green]%s[white]", widget.CurrentSource())
|
||||
|
||||
@ -142,7 +138,7 @@ func (widget *Widget) watchForFileChanges() {
|
||||
for {
|
||||
select {
|
||||
case <-watch.Event:
|
||||
widget.display()
|
||||
widget.Refresh()
|
||||
case err := <-watch.Error:
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
@ -35,7 +35,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
widget.SetDisplayFunction(widget.Refresh)
|
||||
|
||||
widget.client = NewClient(settings)
|
||||
|
||||
@ -52,7 +52,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
|
||||
// Refresh is called on the interval and refreshes the data
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.display()
|
||||
widget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
func (widget *Widget) HelpText() string {
|
||||
@ -61,10 +61,6 @@ func (widget *Widget) HelpText() string {
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) display() {
|
||||
widget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
widget.client.screenName = widget.CurrentSource()
|
||||
tweets := widget.client.Tweets()
|
||||
|
@ -36,13 +36,9 @@ func (widget *Widget) Refresh() {
|
||||
|
||||
teams, err := Fetch(widget.settings.apiID, widget.settings.apiKey)
|
||||
|
||||
if err != nil {
|
||||
widget.err = err
|
||||
widget.teams = nil
|
||||
} else {
|
||||
widget.err = nil
|
||||
widget.teams = teams
|
||||
}
|
||||
widget.err = err
|
||||
widget.teams = teams
|
||||
|
||||
widget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,6 @@ package zendesk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
@ -16,6 +15,7 @@ type Widget struct {
|
||||
|
||||
result *TicketArray
|
||||
settings *Settings
|
||||
err error
|
||||
}
|
||||
|
||||
// NewWidget creates a new instance of a widget
|
||||
@ -41,12 +41,8 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
func (widget *Widget) Refresh() {
|
||||
ticketArray, err := widget.newTickets()
|
||||
ticketArray.Count = len(ticketArray.Tickets)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
widget.result = ticketArray
|
||||
}
|
||||
|
||||
widget.err = err
|
||||
widget.result = ticketArray
|
||||
widget.Render()
|
||||
}
|
||||
|
||||
@ -58,6 +54,10 @@ func (widget *Widget) Render() {
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
title := fmt.Sprintf("%s (%d)", widget.CommonSettings().Title, widget.result.Count)
|
||||
if widget.err != nil {
|
||||
return title, widget.err.Error(), true
|
||||
}
|
||||
|
||||
items := widget.result.Tickets
|
||||
if len(items) == 0 {
|
||||
return title, "No unassigned tickets in queue - woop!!", false
|
||||
|
Loading…
x
Reference in New Issue
Block a user