Skip to content

Commit ab382aa

Browse files
authored
Merge pull request #108
Fix watchdog crashes on transient network errors
2 parents ac31556 + 8f27b22 commit ab382aa

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

go.sum

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoF
1818
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
1919
github.com/charmbracelet/log v0.4.2 h1:hYt8Qj6a8yLnvR+h7MwsJv/XvmBJXiueUcI3cIxsyig=
2020
github.com/charmbracelet/log v0.4.2/go.mod h1:qifHGX/tc7eluv2R6pWIpyHDDrrb/AG71Pf2ysQu5nw=
21-
github.com/charmbracelet/x/ansi v0.10.1 h1:rL3Koar5XvX0pHGfovN03f5cxLbCF2YvLeyz7D2jVDQ=
22-
github.com/charmbracelet/x/ansi v0.10.1/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=
2321
github.com/charmbracelet/x/ansi v0.10.2 h1:ith2ArZS0CJG30cIUfID1LXN7ZFXRCww6RUvAPA+Pzw=
2422
github.com/charmbracelet/x/ansi v0.10.2/go.mod h1:HbLdJjQH4UH4AqA2HpRWuWNluRE6zxJH/yteYEYCFa8=
2523
github.com/charmbracelet/x/cellbuf v0.0.13 h1:/KBBKHuVRbq1lYx5BzEHBAFBP8VcQzJejZ/IA3iR28k=
@@ -49,12 +47,6 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
4947
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
5048
github.com/dofusdude/ankabuffer v0.1.0 h1:yMLJu3iN0JBjBS/Hjh/VtwUHwUJSGx4rEixt9+28+HE=
5149
github.com/dofusdude/ankabuffer v0.1.0/go.mod h1:H84vCl3zg8ibH+h6mFGvYxrLEIBOpnjYVi3WJXBFbNg=
52-
github.com/dofusdude/dodumap v0.6.7 h1:bO6QyxiIsQXr0aO4DMHsdXBL1eV8itJiJ1KEw03gYR8=
53-
github.com/dofusdude/dodumap v0.6.7/go.mod h1:51KG2eMd02UJnXErOubAukVftYuJproDHqJcbIHSzIE=
54-
github.com/dofusdude/dodumap v0.6.8 h1:nj+hIfBYdjnOtOfr1/eZQ5EU9GT2eamWUO47OCPeBxc=
55-
github.com/dofusdude/dodumap v0.6.8/go.mod h1:51KG2eMd02UJnXErOubAukVftYuJproDHqJcbIHSzIE=
56-
github.com/dofusdude/dodumap v0.6.9 h1:KzTtCgLHMIP/UpncD/eEp2YwJdX4MZ5y9Zkig6If+j4=
57-
github.com/dofusdude/dodumap v0.6.9/go.mod h1:51KG2eMd02UJnXErOubAukVftYuJproDHqJcbIHSzIE=
5850
github.com/dofusdude/dodumap v0.6.10 h1:3KI9aB8aaSS/wNkcbkT567+RjRfSToLjG8zD8NhZIcs=
5951
github.com/dofusdude/dodumap v0.6.10/go.mod h1:51KG2eMd02UJnXErOubAukVftYuJproDHqJcbIHSzIE=
6052
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
@@ -96,8 +88,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
9688
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
9789
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
9890
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
99-
github.com/mattn/go-runewidth v0.0.17 h1:78v8ZlW0bP43XfmAfPsdXcoNCelfMHsDmd/pkENfrjQ=
100-
github.com/mattn/go-runewidth v0.0.17/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
10191
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
10292
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
10393
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
@@ -126,7 +116,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
126116
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
127117
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
128118
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
129-
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
130119
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
131120
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
132121
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,12 @@ func versionCommand(ccmd *cobra.Command, args []string) {
289289
}
290290

291291
cytrusPrefix := "6.0_"
292-
version := GetLatestLauncherVersion(gameRelease)
292+
version, err := GetLatestLauncherVersion(gameRelease)
293+
if err != nil {
294+
close(feedbacks)
295+
manifestWg.Wait()
296+
log.Fatal(err)
297+
}
293298
if !strings.HasPrefix(version, cytrusPrefix) {
294299
version = fmt.Sprintf("%s%s", cytrusPrefix, version)
295300
}

update.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,29 +136,56 @@ func DownloadMountImageWorker(manifest *ankabuffer.Manifest, bin int, fragment s
136136
feedbackWg.Wait()
137137
}
138138

139-
func GetLatestLauncherVersion(release string) string {
139+
func GetLatestLauncherVersion(release string) (string, error) {
140140
versionResponse, err := http.Get("https://cytrus.cdn.ankama.com/cytrus.json")
141141
if err != nil {
142-
log.Fatal(err)
142+
return "", fmt.Errorf("failed to fetch cytrus.json: %w", err)
143+
}
144+
defer versionResponse.Body.Close()
145+
146+
// Check HTTP status code
147+
if versionResponse.StatusCode != http.StatusOK {
148+
return "", fmt.Errorf("cytrus.json returned HTTP %d", versionResponse.StatusCode)
143149
}
144150

145151
versionBody, err := io.ReadAll(versionResponse.Body)
146152
if err != nil {
147-
log.Fatal(err)
153+
return "", fmt.Errorf("failed to read cytrus.json response: %w", err)
148154
}
149155

150156
var versionJson map[string]interface{}
151157
err = json.Unmarshal(versionBody, &versionJson)
152158
if err != nil {
153-
log.Fatal(err)
159+
return "", fmt.Errorf("failed to parse cytrus.json: %w", err)
154160
}
155161

156-
games := versionJson["games"].(map[string]interface{})
157-
dofus := games["dofus"].(map[string]interface{})
158-
platform := dofus["platforms"].(map[string]interface{})
159-
windows := platform["windows"].(map[string]interface{})
162+
// Safe type assertions with error checking
163+
games, ok := versionJson["games"].(map[string]interface{})
164+
if !ok {
165+
return "", fmt.Errorf("cytrus.json: 'games' field not found or invalid type")
166+
}
167+
168+
dofus, ok := games["dofus"].(map[string]interface{})
169+
if !ok {
170+
return "", fmt.Errorf("cytrus.json: 'games.dofus' field not found or invalid type")
171+
}
160172

161-
return windows[release].(string)
173+
platforms, ok := dofus["platforms"].(map[string]interface{})
174+
if !ok {
175+
return "", fmt.Errorf("cytrus.json: 'platforms' field not found or invalid type")
176+
}
177+
178+
windows, ok := platforms["windows"].(map[string]interface{})
179+
if !ok {
180+
return "", fmt.Errorf("cytrus.json: 'windows' platform not found or invalid type")
181+
}
182+
183+
version, ok := windows[release].(string)
184+
if !ok {
185+
return "", fmt.Errorf("cytrus.json: version for release '%s' not found or invalid type", release)
186+
}
187+
188+
return version, nil
162189
}
163190

164191
func touchFileIfNotExists(fileName string) error {
@@ -304,7 +331,11 @@ func Download(releaseChannel string, version string, dir string, clean bool, ful
304331
if manifestPath == "" || clean {
305332
cytrusPrefix := "6.0_"
306333
if version == "latest" {
307-
version = GetLatestLauncherVersion(releaseChannel)
334+
var err error
335+
version, err = GetLatestLauncherVersion(releaseChannel)
336+
if err != nil {
337+
return err
338+
}
308339
} else {
309340
// ATT: prefix changes with cytrus updates
310341
if !strings.HasPrefix(version, cytrusPrefix) {

watchdog.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ func VersionChanged(dir string, gameVersion string, versionFilePath string, cust
4646
versionFile.Main = "-"
4747
}
4848

49-
serverVersion := GetLatestLauncherVersion(gameVersion)
49+
serverVersion, err := GetLatestLauncherVersion(gameVersion)
50+
if err != nil {
51+
return false, "", "", fmt.Errorf("failed to get latest version: %w", err)
52+
}
5053
serverVersion = serverVersion[4:] // removing updater version
5154

5255
var versionChanged bool
@@ -105,6 +108,7 @@ func watchdogTick(endTimer chan bool, dir string, gameRelease string, versionFil
105108
changed, oldVersion, newVersion, err := VersionChanged(dir, gameRelease, versionFilePath, customBodyPath, volatile, initialHook)
106109

107110
if err != nil {
111+
// Log the error but don't terminate - allows watchdog to recover from transient failures
108112
log.Error(err)
109113
} else {
110114
if changed {

0 commit comments

Comments
 (0)