errors

Package errors provides simple error handling primitives. The traditional error handling idiom in Go is roughly akin to which when applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error. The e.Wrap function returns a new error that adds context to the original error by recording a stack trace at the point Wrap is called, together with the supplied message. For example If additional control is required, the e.WithStack and e.WithMessage functions destructure e.Wrap into its component operations: annotating an error with a stack trace and with a message, respectively. Using e.Wrap constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of e.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by e.Cause. e.Cause will recursively retrieve the topmost error that does not implement causer, which is assumed to be the original cause. For example: Although the causer interface is not exported by this package, it is considered a part of its stable public interface. All error values returned from this package implement fmt.Formatter and can be formatted by the fmt package. The following verbs are supported: New, Errorf, Wrap, and Wrapf record a stack trace at the point they are invoked. This information can be retrieved with the following interface: The returned e.StackTrace type is defined as The Frame type represents a call site in the stack trace. Frame supports the fmt.Formatter interface that can be used for printing information about the stack trace of this error. For example: Although the stackTracer interface is not exported by this package, it is considered a part of its stable public interface. See the documentation for Frame.Format for more details.

MIT License

Stars
1
Committers
2

errors

Package errors provides simple error handling primitives. Migrated from golib.

Based on github.com/pkg/errors, and fully compatible with github.com/pkg/errors.

Getting Started

Coder

package main

import (
	"fmt"
	"net/http"

	"github.com/shipengqi/errors"
)

type fakeCoder struct {
	code   int
	status int
	msg    string
	ref    string
}

func (d fakeCoder) Code() int         { return d.code }
func (d fakeCoder) String() string    { return d.msg }
func (d fakeCoder) Reference() string { return d.ref }
func (d fakeCoder) HTTPStatus() int {
	if d.status == 0 {
		return http.StatusInternalServerError
	}
	return d.status
}

type parseCoder struct {
	code int
}

func (d parseCoder) Code() int { return d.code }

func main() {
	// annotates err with a code.
	codeErr := errors.WithCode(fmt.Errorf("demo error"), 20010)

	// reports whether any error in err's contains the given code.
	fmt.Println(errors.IsCode(codeErr, 20010)) // true
	
	// returns an error annotating err with a code and a stack trace at the point WrapCodef is called.
	_ = errors.WrapCode(fmt.Errorf("demo error"), 20011)
	// returns an error annotating err with a code and a stack trace at the point WrapCodef is called, and the format specifier.
	_ = errors.WrapCodef(fmt.Errorf("wrap error"), 20012, "wrap %s", "demo")
	

	demoCoder := fakeCoder{
		code:   20013,
		status: http.StatusBadRequest,
		msg:    "bad request",
		ref:    "https://docs.example.com/codes",
	}
	
	// registers a Coder to the global cache.
	errors.Register(demoCoder)
	
	// parse any error into icoder interface, find the corresponding Coder from global cache.
	errors.ParseCoder(parseCoder{code: 20013})
}

Aggregate

package main

import (
	"fmt"
	
	"github.com/shipengqi/errors"
)

func main() {
	// Aggregate represents an object that contains multiple errors, but does not 
	// necessarily have singular semantic meaning
	var errs []error
	errs = append(errs, 
		errors.New("error 1"), 
		errors.New("error 2"), 
		errors.New("error 3"),
	)

	agge := errors.NewAggregate(errs)
	fmt.Println(agge.Error()) // [error 1, error 2, error 3]
}

Documentation

You can find the docs at go docs.

πŸ”‹ JetBrains OS licenses

errors had been being developed with GoLand under the free JetBrains Open Source license(s) granted by JetBrains s.r.o., hence I would like to express my thanks here.