flaw

package module
v0.0.0-...-8efffb4 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: MIT Imports: 15 Imported by: 6

README

flaw

Documentation License Build Status Coverage Go Report Card

A flaw is Golang is a replacement of errors package

Installation

Make sure you have a working Go environment. Go version 1.13.x is supported.

See the install instructions for Go.

To install flaw, simply run:

$ go get github.com/phogolabs/flaw

Getting Started

Wrapping an error:

err := flaw.Wrap(sql.ErrNoRows).
	WithMessage("user does not exist").
	WithCode(404)

If you print the error with fmt.Println(err) you will receive the following output:

code: 404 message: user does not exist cause: sql: no rows in result set

If you want more detailed information, you can use fmt.Printf("%+v", err) formatting:

    code: 404
 message: user does not exist
   cause: sql: no rows in result set
   stack:
 --- /Users/ralch/go/src/github.com/phogolabs/flaw/cmd/main.go:19 (main)
 --- /usr/local/Cellar/go/1.13.1/libexec/src/runtime/proc.go:203 (main)
 --- /usr/local/Cellar/go/1.13.1/libexec/src/runtime/asm_amd64.s:1357 (goexit)

Collecting multiple errors:

errs := flaw.ErrorCollector{}
errs = append(errs, fmt.Errorf("insufficient funds"))
errs = append(errs, fmt.Errorf("maximum allowance reached"))

Then you can print the error fmt.Println(errs):

[insufficient funds, maximum allowance reached]

You can print the result with better formatting fmt.Printf("%+v", errs):

 --- insufficient funds
 --- maximum allowance reached

Contributing

We are open for any contributions. Just fork the project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the error's cause

func Code

func Code(err error) int

Code returns the code from an error

func Details

func Details(err error) []string

Details returns the error's details

func Message

func Message(err error) string

Message returns the error's message

func Status

func Status(err error) int

Status returns the status from an error

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents a wrapped error

func Errorf

func Errorf(msg string, data ...interface{}) *Error

Errorf creates a new error

func Wrap

func Wrap(err error, frames ...StackFrame) *Error

Wrap wraps an error

func (*Error) Cause

func (x *Error) Cause() error

Cause returns the underlying error

func (*Error) Code

func (x *Error) Code() int

Code returns the error code

func (*Error) Context

func (x *Error) Context() Map

Context returns the error's context

func (*Error) Details

func (x *Error) Details() []string

Details returns the error details

func (*Error) Error

func (x *Error) Error() string

Error returns the error message

func (*Error) Format

func (x *Error) Format(state fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%m    error message
%d    error details
%c    error code
%r    error reason
%v    code: %d message: %s details: %d reason: %w

Format accepts flags that alter the printing of some verbs, as follows:

%+s   stack trace
%+v   equivalent

func (*Error) GRPCStatus

func (x *Error) GRPCStatus() *status.Status

GRPCStatus returns the grpc status of this error

func (*Error) MarshalJSON

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

MarshalJSON marshals the error as json

func (*Error) MarshalXML

func (x *Error) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the error as xml

func (*Error) Message

func (x *Error) Message() string

Message returns the error message

func (*Error) StackTrace

func (x *Error) StackTrace() StackTrace

StackTrace returns the stack trace where the error occurred

func (*Error) Status

func (x *Error) Status() int

Status returns the error status

func (*Error) Unwrap

func (x *Error) Unwrap() error

Unwrap unwraps the underlying error

func (Error) WithCode

func (x Error) WithCode(code int) *Error

WithCode creates an error copy with given status

func (Error) WithContext

func (x Error) WithContext(context Map) *Error

WithContext creates an error copy with given map

func (Error) WithDetails

func (x Error) WithDetails(text string, details ...string) *Error

WithDetails creates an error copy with given details

func (Error) WithError

func (x Error) WithError(err error) *Error

WithError creates an error copy with given error wrapped

func (Error) WithMessage

func (x Error) WithMessage(text string) *Error

WithMessage creates an error copy with given message

func (Error) WithStatus

func (x Error) WithStatus(status int) *Error

WithStatus creates an error copy with given status

func (*Error) Wrap

func (x *Error) Wrap(err error)

Wrap wraps the given error

type ErrorCollector

type ErrorCollector []error

ErrorCollector is a slice of errors

func (ErrorCollector) As

func (errs ErrorCollector) As(err interface{}) bool

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.

func (ErrorCollector) Error

func (errs ErrorCollector) Error() string

Error returns the error message

func (ErrorCollector) Format

func (errs ErrorCollector) Format(state fmt.State, verb rune)

Format the error as string

func (ErrorCollector) Is

func (errs ErrorCollector) Is(target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

func (ErrorCollector) MarshalJSON

func (errs ErrorCollector) MarshalJSON() ([]byte, error)

MarshalJSON marshals the error as json

func (ErrorCollector) Unwrap

func (errs ErrorCollector) Unwrap() error

Unwrap unwraps the underlying error it's only one

func (*ErrorCollector) Wrap

func (errs *ErrorCollector) Wrap(err error)

Wrap appends an error to the slice

type ErrorConstant

type ErrorConstant string

ErrorConstant represents an error that can create a constant / sentinel error such as io.EOF

func (ErrorConstant) Error

func (x ErrorConstant) Error() string

Error returns the error message

func (ErrorConstant) Format

func (x ErrorConstant) Format(state fmt.State, verb rune)

Format formats the error

type Map

type Map = map[string]interface{}

Map is an alias to map[string]interface{}

func Context

func Context(err error) Map

Context returns the error's context

type StackFrame

type StackFrame runtime.Frame

StackFrame represents a program counter inside a stack frame. For historical reasons if StackFrame is interpreted as a uintptr its value represents the program counter + 1.

func (StackFrame) Format

func (frame StackFrame) Format(state fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   source file full path
%+v   equivalent to %+s:%d (%n)

func (StackFrame) MarshalText

func (frame StackFrame) MarshalText() ([]byte, error)

MarshalText formats a stacktrace StackFrame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.

type StackTrace

type StackTrace []StackFrame

StackTrace is stack of StackFrames from innermost (newest) to outermost (oldest).

func NewStackTrace

func NewStackTrace() StackTrace

NewStackTrace creates a new StackTrace

func NewStackTraceAt

func NewStackTraceAt(n int) StackTrace

NewStackTraceAt creates a new stack trace at given position

func (StackTrace) Format

func (stack StackTrace) Format(state fmt.State, verb rune)

Format formats the stack of StackFrames according to the fmt.Formatter interface.

%s	lists source files for each StackFrame in the stack
%v	lists the source file and line number for each StackFrame in the stack

Format accepts flags that alter the printing of some verbs, as follows:

%+v   Prints filename, function, and line number for each StackFrame in the stack.

Directories

Path Synopsis
fake
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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