func Wrap(cause error, args ...interface{}) *Error {
	if cause == nil {
		return nil
	}
	args = append(args, cause)
	err := newError(args...)
	updateCaller(err)
	return err
}
Wrap constructs an error the same way that New does, the only difference being that if the given
cause is nil, this function will return nil. This makes it quite handy in return lines at the end
of functions. Wrap conveys it's meaning a little more than New does when you are wrapping other
errors.
package errors
import (
	"bytes"
	"fmt"
	"io"
	"runtime"
	"sort"
	"strconv"
	"strings"
)
type Kind string
type Error struct {
	Kind Kind
	Message string
	Cause error
	Fields map[string]interface{}
	caller string
	file string
	line int
}
func (e *Error) Error() string {
	return e.format(false)
}
func (e *Error) Format(s fmt.State, c rune) {
	if c == 'v' && s.Flag('+') {
		io.WriteString(s, e.format(true))
		return
	}
	io.WriteString(s, e.format(false))
}
func (e *Error) WithFields(kvs ...interface{}) *Error {
	kvc := len(kvs)
	if kvc%2 != 0 {
		Fatal(New(fmt.Sprintf(
			"errors: invalid argument count for WithFields, expected even number of fields, got %d",
			kvc,
		)))
	}
	if e.Fields == nil {
		e.Fields = make(map[string]interface{})
	}
	for i := 0; i < kvc; i = i + 2 {
		key, ok := kvs[i].(string)
		if !ok {
			Fatal(New(fmt.Sprintf("errors: invalid type for key passed to WithFields at index %d", i)))
		}
		e.Fields[key] = kvs[i+1]
	}
	return e
}
func (e *Error) WithField(fieldKey string, fieldValue interface{}) *Error {
	if e.Fields == nil {
		e.Fields = make(map[string]interface{})
	}
	e.Fields[fieldKey] = fieldValue
	return e
}
func (e *Error) format(asStack bool) string {
	buf := bytes.Buffer{}
	e.formatAccumulator(&buf, asStack, false)
	return buf.String()
}
func (e *Error) formatAccumulator(buf *bytes.Buffer, asStack, isCause bool) {
	if asStack && !isCause {
		buf.WriteString("Error")
	}
	if e.caller != "" {
		pad(buf, ": ")
		buf.WriteString("[")
		buf.WriteString(e.caller)
		buf.WriteString("]")
	}
	if e.Message != "" {
		pad(buf, ": ")
		buf.WriteString(e.Message)
	}
	if e.Kind != "" {
		pad(buf, " ")
		buf.WriteString("(")
		buf.WriteString(string(e.Kind))
		buf.WriteString(")")
	}
	if asStack {
		buf.WriteString("\n")
		buf.WriteString("    ")
		buf.WriteString("File: \"")
		buf.WriteString(e.file)
		buf.WriteString("\", line ")
		buf.WriteString(strconv.Itoa(e.line))
		buf.WriteString("\n")
		if len(e.Fields) > 0 {
			buf.WriteString("    ")
			buf.WriteString("With fields:\n")
			fieldKeys := make([]string, 0, len(e.Fields))
			for k := range e.Fields {
				fieldKeys = append(fieldKeys, k)
			}
			sort.Strings(fieldKeys)
			for _, k := range fieldKeys {
				buf.WriteString("    ")
				buf.WriteString("- \"")
				buf.WriteString(k)
				buf.WriteString("\": ")
				buf.WriteString(fmt.Sprintf("%v", e.Fields[k]))
				buf.WriteString("\n")
			}
		}
	}
	if e.Cause != nil {
		if !asStack {
		} else {
			buf.WriteString("Caused by")
		}
		switch cause := e.Cause.(type) {
		case *Error:
			cause.formatAccumulator(buf, asStack, true)
		case error:
			pad(buf, ": ")
			buf.WriteString(cause.Error())
		}
	}
}
func pad(buf *bytes.Buffer, pad string) {
	if buf.Len() > 0 {
		buf.WriteString(pad)
	}
}
func New(args ...interface{}) *Error {
	err := newError(args...)
	updateCaller(err)
	return err
}
func newError(args ...interface{}) *Error {
	if len(args) == 0 {
		panic("errors: call to errors.New with no arguments")
	}
	err := &Error{}
	for _, arg := range args {
		switch v := arg.(type) {
		case Kind:
			err.Kind = v
		case string:
			err.Message = v
		case *Error:
			if v == nil {
				panic("errors: attempted to wrap nil *Error")
			}
			cv := *v
			err.Cause = &cv
		case error:
			err.Cause = v
		case map[string]interface{}:
			err.Fields = v
		default:
			panic(fmt.Sprintf("errors: bad call to errors.New: unknown type %T, value %v", arg, arg))
		}
	}
	return err
}