errorx

package module
v0.0.0-...-6c55405 Latest Latest
Warning

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

Go to latest
Published: May 26, 2015 License: MIT Imports: 6 Imported by: 2

README

Build Status GoDoc

errorx

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/goware/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

Index

Constants

View Source
const (
	Info = iota
	Verbose
	Debug
	Trace
)

Variables

This section is empty.

Functions

func SetVerbosity

func SetVerbosity(v int)

SetVerbosity changes global verbosity setting

Types

type Errorx

type Errorx struct {
	Code    int      `json:"error_code,omitempty"`
	Message string   `json:"error_message"`
	Details []string `json:"error_details,omitempty"`
	Cause   error    `json:"cause,omitempty"`
	Stack   Stack    `json:"stack,omitempty"`
}

Errorx is a more feature rich implementation of error interface inspired by PostgreSQL error style guide

func New

func New(code int, ErrorMsg ...string) *Errorx

New returns an error with error code and error messages provided in function params

func (Errorx) Error

func (e Errorx) Error() string

Error returns a string representation of errorx. It includes at least error code and message. Error details and hint are provided depending on verbosity level set

func (Errorx) ErrorCode

func (e Errorx) ErrorCode() int

ErrorCode returns Errorx error code value. It's intended primarily to allow easy error comparison / matching

func (Errorx) Json

func (e Errorx) Json() ([]byte, error)

Json returns a json representation (as []byte) of errorx and error if marshaling fails

func (*Errorx) Wrap

func (e *Errorx) Wrap(err error)

Wrap wraps error in Errorx for nested errors

type Stack

type Stack []StackFrame

Stack is a slice of StackFrames representing a stack trace

func (Stack) String

func (s Stack) String() string

String generates a string representation of a stack trace generated by getTrace

type StackFrame

type StackFrame struct {
	File     string `json:"file,omitempty"`
	Line     int    `json:"line,omitempty"`
	Function string `json:"function,omitempty"`
}

StackFrame represents a single frame of a stack trace

Jump to

Keyboard shortcuts

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