json error handling

This commit is contained in:
Nox Sluijtman 2022-09-01 12:41:48 +02:00
parent e3092e91df
commit 9fb7771281

12
main.go
View file

@ -45,12 +45,11 @@ func parseFlags() {
flag.Parse() flag.Parse()
} }
func isError(err error) bool { func handleError(err error) {
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
os.Exit(1) os.Exit(1)
} }
return (err != nil)
} }
func readJson() { func readJson() {
@ -66,13 +65,14 @@ func readJson() {
fmt.Println("Opening file:", path) fmt.Println("Opening file:", path)
var file, err = os.Open(path) var file, err = os.Open(path)
if isError(err) { handleError(err)
return
}
defer file.Close() defer file.Close()
byteValue, _ := ioutil.ReadAll(file) byteValue, _ := ioutil.ReadAll(file)
json.Unmarshal(byteValue, &char)
err = json.Unmarshal(byteValue, &char)
handleError(err)
} }
func initMaps() { func initMaps() {