Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit c66357b

Browse files
committed
Handle JSON errors more gracefully, add output log
1 parent c57c86b commit c66357b

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

bgl/bgl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (b *BGLData) HandleMatchResultUpload(result ResultSubmission) (int, error)
268268
return 0, err
269269
}
270270

271-
if res.StatusCode != 201 {
271+
if res.StatusCode != 200 {
272272
log.Println("Submission Error Code", res.StatusCode)
273273
return 0, errors.New(string(body))
274274
}

main.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package main
22

33
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
48
"fyne.io/fyne/v2"
59
"fyne.io/fyne/v2/app"
10+
"fyne.io/fyne/v2/dialog"
611
"github.com/achhabra2/kqb-json-viewer/icons"
712
"github.com/achhabra2/kqb-json-viewer/stats"
813
)
914

1015
func main() {
11-
names := stats.ListStatFiles()
12-
data := stats.ReadJson(names[0])
16+
setupLogs()
17+
1318
mainIcon := fyne.NewStaticResource("logo.png", icons.Logo)
1419

1520
a := app.NewWithID("com.kqb-json-viewer.app")
@@ -19,13 +24,33 @@ func main() {
1924

2025
w := a.NewWindow("KQB JSON Viewer")
2126

22-
kqbApp := KQBApp{
23-
files: names,
24-
selectedData: data,
25-
a: a,
26-
w: w,
27+
names := stats.ListStatFiles()
28+
data, err := stats.ReadJson(names[0])
29+
if err != nil {
30+
dialog := dialog.NewError(err, w)
31+
dialog.SetOnClosed(func() { os.Exit(1) })
32+
dialog.Show()
33+
w.Resize(fyne.NewSize(500, 900))
34+
w.CenterOnScreen()
35+
w.ShowAndRun()
36+
} else {
37+
kqbApp := KQBApp{
38+
files: names,
39+
selectedData: data,
40+
a: a,
41+
w: w,
42+
}
43+
44+
kqbApp.ShowMainWindow()
2745
}
2846

29-
kqbApp.ShowMainWindow()
47+
}
48+
49+
func setupLogs() {
50+
f, err := os.OpenFile("./kqb-json-viewer-output.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
51+
if err != nil {
52+
fmt.Printf("error opening file: %v", err)
53+
}
3054

55+
log.SetOutput(f)
3156
}

stats/graphs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func TestGraph(t *testing.T) {
10-
data := ReadJson("../fixtures/sample.json")
10+
data, _ := ReadJson("../fixtures/sample.json")
1111
img := PlotStats(data)
1212

1313
// // Save the image.

stats/stats.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,23 @@ func sortFiles(files []os.FileInfo) []os.FileInfo {
6060
return files
6161
}
6262

63-
func ReadJson(file string) StatsJSON {
63+
func ReadJson(file string) (StatsJSON, error) {
6464
var statsJSON StatsJSON
6565
data, err := ioutil.ReadFile(file)
66+
_, fileName := filepath.Split(file)
6667
if err != nil {
67-
log.Fatal("Could not read json file", err)
68+
jsonError := fmt.Errorf("could not read json file\n (%v): %v", fileName, err)
69+
log.Println(jsonError.Error())
70+
return statsJSON, jsonError
6871
}
6972
err = json.Unmarshal(data, &statsJSON)
7073
if err != nil {
71-
log.Fatal("Could not parse json file", err)
74+
marshalError := fmt.Errorf("could not properly decode json file\n (%v): %v", fileName, err)
75+
log.Println(marshalError.Error())
76+
return statsJSON, marshalError
7277
}
7378

74-
return statsJSON
79+
return statsJSON, nil
7580
}
7681

7782
func OpenStatDirectory() {

stats/stats_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestListStats(t *testing.T) {
1717

1818
func TestAdvancedStats(t *testing.T) {
1919
names := ListStatFiles()
20-
data := ReadJson(names[0])
20+
data, _ := ReadJson(names[0])
2121
output := data.AdvancedStats()
2222
t.Log(output)
2323
}

ui.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (k *KQBApp) ShowMainWindow() {
9090

9191
combo := widget.NewSelect(trimmed, func(value string) {
9292
log.Println("Select file", value)
93-
k.selectedData = stats.ReadJson(trimmedMap[value])
93+
k.LoadStatsFile(trimmedMap[value])
9494
k.selectedFile = value
9595
if k.u.BGLToken != "" {
9696
k.u.data = k.selectedData
@@ -111,7 +111,7 @@ func (k *KQBApp) ShowMainWindow() {
111111
k.fileDropDown = combo
112112

113113
advancedStatsButton := widget.NewButtonWithIcon("Adv. Stats", theme.FileImageIcon(), func() {
114-
k.selectedData = stats.ReadJson(trimmedMap[combo.Selected])
114+
k.LoadStatsFile(trimmedMap[combo.Selected])
115115
k.ShowAdvancedStats()
116116
})
117117

@@ -476,6 +476,16 @@ func (k *KQBApp) ResetUploader() {
476476
k.w.Resize(fyne.NewSize(500, 900))
477477
}
478478

479+
func (k *KQBApp) LoadStatsFile(file string) {
480+
newData, err := stats.ReadJson(file)
481+
if err != nil {
482+
dialog := dialog.NewError(err, k.w)
483+
dialog.Show()
484+
return
485+
}
486+
k.selectedData = newData
487+
}
488+
479489
func getTimeString(file string) string {
480490
fInfo, _ := os.Open(file)
481491
info, _ := fInfo.Stat()

0 commit comments

Comments
 (0)