errorutil

package
v0.0.0-...-ddb7c7b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2015 License: Apache-2.0 Imports: 5 Imported by: 0

README

errorutil

Errorutil is a small go package to help show syntax errors in for example JSON documents.

It was forked from Camlistore to make a smaller dependency.

Example

An example of how to use the package to show errors when decoding with encoding/json.

if err = decoder.Decode(&objmap); err != nil {
        extra := ""

        // if it's a syntax error, add more information
        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)
}

License

This package is licesed under the Apache License, version 2.0. It was developed by Brad Fitzpatrick as part of the Camlistore project.

Documentation

Overview

Package errorutil helps make better error messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HighlightBytePosition

func HighlightBytePosition(f io.Reader, pos int64) (line, col int, highlight string)

HighlightBytePosition takes a reader and the location in bytes of a parse error (for instance, from json.SyntaxError.Offset) and returns the line, column, and pretty-printed context around the error with an arrow indicating the exact position of the syntax error.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL