Skip to content

Commit

Permalink
Show line with syntax error when parsing JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Dec 20, 2012
1 parent f919c01 commit 0c49dc7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"camlistore.org/pkg/errorutil"
"encoding/json"
"fmt"
"github.com/miekg/dns"
"io/ioutil"
"log"
Expand Down Expand Up @@ -102,7 +104,20 @@ func readZoneFile(zoneName, fileName string) (*Zone, error) {
if err == nil {
var objmap map[string]interface{}
decoder := json.NewDecoder(fh)
err := decoder.Decode(&objmap)
if err = decoder.Decode(&objmap); err != nil {
extra := ""
if serr, ok := err.(*json.SyntaxError); ok {
if _, serr := fh.Seek(0, os.SEEK_SET); serr != nil {
log.Fatalf("seek error: %v", serr)
}
line, col, highlight := errorutil.HighlightBytePosition(fh, serr.Offset)
extra = fmt.Sprintf(":\nError at line %d, column %d (file offset %d):\n%s",
line, col, serr.Offset, highlight)
}
return nil, fmt.Errorf("error parsing JSON object in config file %s%s\n%v",
fh.Name(), extra, err)
}

if err != nil {
panic(err)
}
Expand Down

0 comments on commit 0c49dc7

Please sign in to comment.