1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Fix JSON parsing

Issue #600 points out Jenkins module panics occasionally
I noticed that a number of places panic because of JSON parsing
Centralize JSON parsing, have it return an error, and have widgets
report the error
This commit is contained in:
Sean Smith
2019-09-07 15:10:37 -04:00
parent 32884e5de4
commit 36fbad54d8
8 changed files with 73 additions and 162 deletions

View File

@@ -1,13 +1,11 @@
package travisci
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/wtfutil/wtf/utils"
)
var TRAVIS_HOSTS = map[bool]string{
@@ -25,7 +23,10 @@ func BuildsFor(settings *Settings) (*Builds, error) {
return builds, err
}
parseJson(&builds, resp.Body)
err = utils.ParseJson(&builds, resp.Body)
if err != nil {
return builds, err
}
return builds, nil
}
@@ -67,20 +68,3 @@ func travisBuildRequest(settings *Settings) (*http.Response, error) {
return resp, nil
}
func parseJson(obj interface{}, text io.Reader) {
jsonStream, err := ioutil.ReadAll(text)
if err != nil {
panic(err)
}
decoder := json.NewDecoder(bytes.NewReader(jsonStream))
for {
if err := decoder.Decode(obj); err == io.EOF {
break
} else if err != nil {
panic(err)
}
}
}