errorx

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: GPL-3.0 Imports: 8 Imported by: 2

README

errorx

This package takes great inspiration from goware/errorx which gives a feature-rich Golang error interface implementation inspired by Postgres error message style guide http://www.postgresql.org/docs/devel/static/error-style-guide.html

features

  • Error codes
  • Verbosity levels
  • File and line on which the error occures (Debug+ verbosity level). Not 100% accurate, but close enough: shows file/line where errorx is rendered to string/JSON
  • error Stack traces (on verbosity level Trace)
  • Nested errors (both regular Golang error and Errorx)
  • Everything Golang error has - it's a drop-in replacement, because it implements error interface
  • Everything Golang errors package provides
  • JSON errors you can just write to your webhandler

docs

http://godoc.org/github.com/neumachen/errorx

example output

json, nested error, verbosity: Trace
{
  "error_code": 10,
  "error_message": "error message",
  "error_details": ["error details", "error hint"],
  "cause": {
    "error_code": 200,
    "error_message": "wrapped error message",
    "error_details": ["wrapped error details", "wrapped error hint"]
  },
  "stack": [
    {
      "file": "errorx_test.go",
      "line": 175,
      "function": "github.com/goware/errorx_test.TestJsonErrorEmbedding"
    },
    {
      "file": "testing.go",
      "line": 447,
      "function": "testing.tRunner"
    },
    {
      "file": "asm_amd64.s",
      "line": 2232,
      "function": "runtime.goexit"
    }
  ]
}
string (via .Error()), verbosity: Debug
errorx_test.go:28: error 10: error message | error details; error hint

Documentation

Overview

Package errorx provides errors that have stack-traces.

This is particularly useful when you want to understand the state of execution when an error was returned unexpectedly.

It provides the type *Error which implements the standard golang error interface, so you can use this library interchangably with code that is expecting a normal error return.

Index

Constants

This section is empty.

Variables

View Source
var MaxStackDepth = 50

MaxStackDepth is the maximum number of stackframes on any error.

Functions

func Is

func Is(e error, original error) bool

Is detects whether the error is equal to a given error. Errors are considered equal by this function if they are the same object, or if they both contain the same error inside an errors.Error.

Types

type Error

type Error struct {
	Cause error `json:"cause"`
	// contains filtered or unexported fields
}

Error is a more feature rich implementation of error interface inspired by PostgreSQL error style guide. It can be used wherever the builtin error interface is expected.

func Errorf

func Errorf(format string, a ...interface{}) *Error

Errorf creates a new error with the given message. You can use it as a drop-in replacement for fmt.Errorf() to provide descriptive errors in return values.

func New

func New(e interface{}) *Error

New makes an Error from the given value. If that value is already an error then it will be used directly, if not, it will be passed to fmt.Errorf("%v"). The stacktrace will point to the line of code that called New.

func ParsePanic

func ParsePanic(text string) (*Error, error)

ParsePanic parses the panic information from the given text and returns an Error reference.

func Wrap

func Wrap(e interface{}, skip int) *Error

Wrap makes an Error from the given value. If that value is already an error then it will be used directly, if not, it will be passed to fmt.Errorf("%v"). The skip parameter indicates how far up the stack to start the stacktrace. 0 is from the current call, 1 from its caller, etc.

func WrapPrefix

func WrapPrefix(e interface{}, prefix string, skip int) *Error

WrapPrefix makes an Error from the given value. If that value is already an error then it will be used directly, if not, it will be passed to fmt.Errorf("%v"). The prefix parameter is used to add a prefix to the error message when calling Error(). The skip parameter indicates how far up the stack to start the stacktrace. 0 is from the current call, 1 from its caller, etc.

func (*Error) Error

func (e *Error) Error() string

Error returns the underlying error's message.

func (*Error) ErrorStack

func (e *Error) ErrorStack() string

ErrorStack returns a string that contains both the error message and the callstack.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (*Error) Stack

func (e *Error) Stack() []byte

Stack returns the callstack formatted the same way that go does in runtime/debug.Stack()

func (*Error) StackFrames

func (e *Error) StackFrames() []StackFrame

StackFrames returns an array of frames containing information about the stack.

func (*Error) TypeName

func (e *Error) TypeName() string

TypeName returns the type this error. e.g. *errors.stringError.

type StackFrame

type StackFrame struct {
	File           string  `json:"file"`            // The path to the file containing this ProgramCounter
	LineNumber     int     `json:"line_number"`     // The line number in that file
	Name           string  `json:"name"`            // The name of the function that contains this ProgramCounter
	Package        string  `json:"package"`         // The package that contains this function
	ProgramCounter uintptr `json:"program_counter"` // The underlying ProgramCounter
}

StackFrame contains information about a stack frame in a call stack.

func NewStackFrame

func NewStackFrame(pc uintptr) (frame StackFrame)

NewStackFrame populates a StackFrame object from the program counter.

func (*StackFrame) Func

func (frame *StackFrame) Func() *runtime.Func

Func returns the function that contained this frame.

func (*StackFrame) SourceLine

func (frame *StackFrame) SourceLine() (string, error)

SourceLine gets the line of code (from File and Line) of the original source if possible.

func (*StackFrame) String

func (frame *StackFrame) String() string

String returns the formatted stack frame, similar to how Go does in runtime/debug.Stack().

Jump to

Keyboard shortcuts

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