From 9b024156a509656a5ddcffa5dfa960195cd3a2ba Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 3 Oct 2015 12:28:25 -0700 Subject: [PATCH] Move to new changes in pkg/quick with json parse error formatting Thanks to Anis Elleuch for really good work. --- vendor.json | 4 +- .../minio/minio/pkg/quick/errorutil.go | 96 +++++++++++++++++++ .../github.com/minio/minio/pkg/quick/quick.go | 15 ++- 3 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 vendor/github.com/minio/minio/pkg/quick/errorutil.go diff --git a/vendor.json b/vendor.json index 56e76c29..7ea12a1c 100755 --- a/vendor.json +++ b/vendor.json @@ -62,8 +62,8 @@ "canonical": "github.com/minio/minio/pkg/quick", "comment": "", "local": "vendor/github.com/minio/minio/pkg/quick", - "revision": "daa089fb06b05ad7a9ce945e51362d349ffaf662", - "revisionTime": "2015-09-29T10:17:19-07:00" + "revision": "09dc360e06ece15f14df0e15e77d42652b329f05", + "revisionTime": "2015-10-03T12:26:04-07:00" }, { "canonical": "github.com/minio/pb", diff --git a/vendor/github.com/minio/minio/pkg/quick/errorutil.go b/vendor/github.com/minio/minio/pkg/quick/errorutil.go new file mode 100644 index 00000000..b67582c4 --- /dev/null +++ b/vendor/github.com/minio/minio/pkg/quick/errorutil.go @@ -0,0 +1,96 @@ +/* + * Quick - Quick key value store for config files and persistent state files + * + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package quick + +import ( + "bufio" + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + + "github.com/olekukonko/ts" +) + +const errorFmt = "%5d: %s <-- " + +// FormatJSONSyntaxError generates a pretty printed json syntax error since +// golang doesn't provide an easy way to report the location of the error +func FormatJSONSyntaxError(data io.Reader, sErr *json.SyntaxError) error { + if sErr == nil { + return nil + } + + var readLine bytes.Buffer + + bio := bufio.NewReader(data) + errLine := int64(1) + readBytes := int64(0) + + // termWidth is set to a default one to use when we are + // not able to calculate terminal width via OS syscalls + termWidth := 25 + + // errorShift is the length of the minimum needed place for + // error msg accessoires, like <--, etc.. We calculate it + // dynamically to avoid an eventual bug after modifying errorFmt + errorShift := len(fmt.Sprintf(errorFmt, 1, "")) + + if termSize, err := ts.GetSize(); err == nil { + termWidth = termSize.Col() + } + + for { + b, err := bio.ReadByte() + if err != nil { + if err != io.EOF { + return err + } + break + } + readBytes++ + if readBytes > sErr.Offset { + break + } + switch b { + case '\n': + readLine.Reset() + errLine++ + case '\t': + readLine.WriteByte(' ') + case '\r': + break + default: + readLine.WriteByte(b) + } + } + + lineLen := readLine.Len() + idx := lineLen - termWidth + errorShift + if idx < 0 || idx > lineLen-1 { + idx = 0 + } + + errorStr := fmt.Sprintf("JSON syntax error at line %d, col %d : %s.\n", + errLine, readLine.Len(), sErr) + errorStr += fmt.Sprintf(errorFmt, errLine, readLine.String()[idx:]) + + return errors.New(errorStr) +} diff --git a/vendor/github.com/minio/minio/pkg/quick/quick.go b/vendor/github.com/minio/minio/pkg/quick/quick.go index c0b34193..3aa6c6b2 100644 --- a/vendor/github.com/minio/minio/pkg/quick/quick.go +++ b/vendor/github.com/minio/minio/pkg/quick/quick.go @@ -19,6 +19,7 @@ package quick import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -144,7 +145,12 @@ func Load(filename string, data interface{}) (Config, *probe.Error) { err = json.Unmarshal(fileData, &data) if err != nil { - return nil, probe.NewError(err) + switch err := err.(type) { + case *json.SyntaxError: + return nil, probe.NewError(FormatJSONSyntaxError(bytes.NewReader(fileData), err)) + default: + return nil, probe.NewError(err) + } } config, perr := New(data) @@ -182,7 +188,12 @@ func (d *config) Load(filename string) *probe.Error { err = json.Unmarshal(fileData, d.data) if err != nil { - return probe.NewError(err) + switch err := err.(type) { + case *json.SyntaxError: + return probe.NewError(FormatJSONSyntaxError(bytes.NewReader(fileData), err)) + default: + return probe.NewError(err) + } } if err := CheckData(d.data); err != nil {