Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error handling #80

Merged
merged 4 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ui/cmd/greeting/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type Model struct {
conversation []*Message
user *spotify.PrivateUser
spotifyClient *spotify.Client
err error
}

func NewModel() *Model {
Expand Down
3 changes: 1 addition & 2 deletions ui/cmd/greeting/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package greeting

import (
"errors"
"log"

"github.com/JunNishimura/Chatify/auth"
"github.com/JunNishimura/Chatify/config"
Expand Down Expand Up @@ -60,7 +59,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case deviceMsg:
m.phase = completePhase
case errMsg:
log.Println(msg.err)
m.err = msg.err
}

m.textInput, inputCmd = m.textInput.Update(msg)
Expand Down
3 changes: 3 additions & 0 deletions ui/cmd/greeting/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
)

func (m *Model) View() string {
if m.err != nil {
return style.ErrorView(m.window.Width, m.window.Height)
}
return lipgloss.Place(m.window.Width, m.window.Height, lipgloss.Center, lipgloss.Center,
lipgloss.JoinVertical(
lipgloss.Center,
Expand Down
1 change: 1 addition & 0 deletions ui/cmd/hey/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Model struct {
functions []openai.FunctionDefinition
availableGenres []string
recommendItems []list.Item
err error
}

func NewModel() *Model {
Expand Down
3 changes: 1 addition & 2 deletions ui/cmd/hey/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package hey
import (
"encoding/json"
"fmt"
"log"
"strings"

"github.com/JunNishimura/Chatify/ai/functions"
Expand Down Expand Up @@ -119,7 +118,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.recommendItems = msg.items
m.list = newListModel(m.recommendItems, m.getViewWidth(), m.getViewHeight())
case errMsg:
log.Println(msg.err)
m.err = msg.err
}

m.textInput, inputCmd = m.textInput.Update(msg)
Expand Down
6 changes: 4 additions & 2 deletions ui/cmd/hey/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
)

func (m *Model) View() string {
var s string
if m.err != nil {
return style.ErrorView(m.window.Width, m.window.Height)
}

// window size adjustmen
var s string
if m.state == chatView {
s += lipgloss.Place(m.window.Width, m.window.Height, lipgloss.Center, lipgloss.Center,
lipgloss.JoinHorizontal(
Expand Down
20 changes: 20 additions & 0 deletions ui/style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
const (
White = "#ffffff"
Gray = "#777777"
Red = "#ff4444"
HighlightColor = "#1DB954"
SemiHighlightColor = "#146542"
BgColor = "#191414"
Expand Down Expand Up @@ -95,3 +96,22 @@ func AsciiArt() lipgloss.Style {
return lipgloss.NewStyle().
Foreground(lipgloss.Color(HighlightColor))
}

const errorMessage = "unexpected error happens.\n\nplease report at\n\nhttps://github.com/JunNishimura/Chatify/issues"

func ErrorView(windowWidth, windowHeight int) string {
return lipgloss.Place(windowWidth, windowHeight, lipgloss.Center, lipgloss.Center,
lipgloss.NewStyle().
Width(60).
Height(10).
BorderStyle(lipgloss.ThickBorder()).
BorderForeground(lipgloss.Color(HighlightColor)).
Background(lipgloss.AdaptiveColor{Dark: BgColor, Light: BgColor}).
Render(lipgloss.Place(50, 10, lipgloss.Center, lipgloss.Center,
lipgloss.NewStyle().
Width(50).
Align(lipgloss.Center).
Foreground(lipgloss.Color(Red)).
Background(lipgloss.Color(BgColor)).
Render(errorMessage))))
}