Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 746 Bytes

README.md

File metadata and controls

40 lines (30 loc) · 746 Bytes

errs

The errs package is meant to be used in combination with the default errors package. It adds error types that can be used in combination e.g. to have a call stack in the error chain.

Usage

package main

import (
	"encoding/json"
	"errors"
	"fmt"

	"github.com/cosygreen/errs"
	"github.com/tehsphinx/cstack"
)

func main() {
	err1 := createErr()
	err2 := wrapErr()

	stackStr := errs.MarshalStack(err1)
	stackSlice := errs.StackFrames(err2)

	jsonBts, _ := json.Marshal(stackSlice)
	fmt.Println(stackStr)
	fmt.Println(string(jsonBts))
}

func createErr() error {
	return errs.New("new error with stack")
}

func wrapErr() error {
	err := errors.New("wrapped with stack: error without stack")
	return errs.WithStack(err)
}