errs

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 8 Imported by: 0

README

errs-go

errs-go is simple error with stack trace and key-value attribute.

Usage

Create

new error instance

func someFunction() error {
    err := errs.New("parameter error")
}

with value

func someFunction() error {
    err := errs.New("parameter error").With("key", "value")
}

wrapping

func someFunction() error {
    err := errs.Wrap(errors.New("original error"))
}
Template

Declare the definition of error

const (
    ErrNoSession    errs.StringError = "ErrNoSession"
    ErrRetryTimeout errs.StringError = "ErrRetryTimeout"
)

func someFunction1() error {
    session := getSession()
    if session != nil {
        return ErrNoSession.New()
    }
}

func someFunction2() error {
    conn, err := connect()
    if err != nil {
        return ErrRetryTimeout.Wrap(err)
    }
}
StackTrace

Source:

package main

import (
	"fmt"

	"github.com/goccy/go-json"

	"github.com/rtkym/errs-go"
)

func something(value string) error {
	return errs.New("somthing error").With("key", value)
}

func main() {
	if err := something("hoge"); err != nil {
		b, _ := json.Marshal(err)
		fmt.Printf("%s\n", err)
		fmt.Printf("%+v\n", err)
		fmt.Println(string(b))
	}
}

Output: %s

somthing error

Output: %+v

somthing error
values={"key":"hoge"}
main.something
        ./main.go:11
main.main
        ./main.go:15
runtime.main
        runtime/proc.go:250
runtime.goexit
        runtime/asm_amd64.s:1571

Output: JSON

{
    "message": "somthing error",
    "values": {
        "key": "hoge"
    },
    "stackTrace": [
        {
            "func": "main.something",
            "file": "./main.go",
            "line": 11
        },
        {
            "func": "main.main",
            "file": "./main.go",
            "line": 15
        },
        {
            "func": "runtime.main",
            "file": "runtime/proc.go",
            "line": 250
        },
        {
            "func": "runtime.goexit",
            "file": "runtime/asm_amd64.s",
            "line": 1571
        }
    ]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

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

Error is an error interface with associated attributes.

func New

func New(msg string) *Error

New creates a new *Error.

func Wrap

func Wrap(cause error, messages ...interface{}) *Error

Wrap creates a new *Error. However, if the cause is *Error, cause is returned as is.

func (*Error) Error

func (x *Error) Error() string

Error returns error message

func (*Error) Format

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

Format returns: - %v, %s, %q: formated message - %+v: formated message with values and stack trace

func (*Error) MarshalJSON

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

MarshallJSON returns a JSON string of errs.Error. However, because the Cause is embedded in the Message, the type information is lost.

func (*Error) StackTrace

func (x *Error) StackTrace() StackTrace

StackTrace returns stack trace

func (*Error) UnmarshalJSON

func (x *Error) UnmarshalJSON(b []byte) error

UnmarshalJSON recovers Error objects from JSON strings. However, nested error structures are not restored.

func (*Error) Unwrap

func (x *Error) Unwrap() error

Unwrap returns cause

func (*Error) Values

func (x *Error) Values() map[string]interface{}

Values ​​returns a map of attributes set by With. Error attributes are overridden with the error attributes of the wrapped errs.Error.

func (*Error) With

func (x *Error) With(key string, value interface{}) *Error

With adds key and value to error attribute.

type StackFrame

type StackFrame struct {
	Func string `json:"func"`
	File string `json:"file"`
	Line int    `json:"line"`
}

func (StackFrame) Format

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

type StackTrace

type StackTrace []StackFrame

func (StackTrace) Format

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

type StringError

type StringError string

StringErrorは指定された文字列をエラーメッセージとして扱います.

func (StringError) Error

func (s StringError) Error() string

Error returns error message

func (StringError) New

func (s StringError) New() *Error

New creates *errs.Error that wraps StringError.

func (StringError) Wrap

func (s StringError) Wrap(cause error) *Error

Wrap creates *errs.Error that wraps StringError and cause.

Directories

Path Synopsis
examples
stacktrace command

Jump to

Keyboard shortcuts

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