errors

package module
v0.0.0-...-edfbba0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2021 License: Apache-2.0 Imports: 9 Imported by: 131

README

errors

For replacement offical package of errors

for more detials, see the example

main.go
package main

import (
    "fmt"

    "github.com/gogap/errors"
)

var (
    ErrParseTest  = errors.TN("GOGAP", 10001, "hello {{.param1}}")
    ErrParseTest2 = errors.TN("GOGAP", 10002, "test error")
    ErrStackTest  = errors.TN("GOGAP", 10003, "call stack test")
)

func main() {

    e1 := ErrParseTest.New(errors.Params{"param1": "world"}).WithContext("key", "value")

    e1.Append("I am append errors")

    fmt.Println("always equal while errors append:", ErrParseTest.IsEqual(e1))

    fmt.Println("ErrParseTest = ErrParseTest2 :", ErrParseTest2.IsEqual(e1))

    data, _ := e1.(errors.ErrCode).Marshal()

    fmt.Println(string(data))

    fmt.Println(e1.Error())

    stack3Error := call_3()

    fmt.Println(stack3Error.(errors.ErrCode).StackTrace())

    fmt.Println(e1.(errors.ErrCode).FullError())
}

func call_1() error {
    return call_2()
}
func call_2() error {
    return call_3()
}
func call_3() error {
    return ErrStackTest.New()
}

example output
$ go run main.go

always equal while errors append: true
ErrParseTest = ErrParseTest2 : false
{"id":"6F93654","namespace":"GOGAP","code":10001,"message":"hello world; I am append errors."}

hello world; I am append errors.

github.com/gogap/errors/example/main.go:45 call_3
github.com/gogap/errors/example/main.go:31 main
/usr/local/go/src/runtime/proc.go:188      main
/usr/local/go/src/runtime/asm_amd64.s:1998 goexit

Id: GOGAP#10001:6F93654
Error:
hello world; I am append errors.
Context:
{"key":"value"}
StackTrace:
github.com/gogap/errors/example/main.go:17 main
/usr/local/go/src/runtime/proc.go:188      main
/usr/local/go/src/runtime/asm_amd64.s:1998 goexit

Documentation

Index

Constants

View Source
const (
	ErrcodeNamespace      = "ERRCODE"
	DefaultErrorNamespace = "ERR"
)
View Source
const (
	ErrcodeParseTmplError = 1
	ErrcodeExecTmpleError = 2
)

Variables

This section is empty.

Functions

func Errorf

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

func IsErrCode

func IsErrCode(err error) bool

func New

func New(text string) error

Types

type ErrCode

type ErrCode interface {
	Id() string
	Code() uint64
	Namespace() string
	Error() string
	StackTrace() string
	Context() ErrorContext
	FullError() error
	Append(err ...interface{}) ErrCode
	WithMessage(extraMsg string) ErrCode
	WithContext(k string, v interface{}) ErrCode
	Marshal() ([]byte, error)
}

func NewErrorCode

func NewErrorCode(id string, code uint64, namespace string, message string, stackTrace string, context map[string]interface{}) ErrCode

func Unmarshal

func Unmarshal(data []byte) ErrCode

type ErrCodeTemplate

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

func T

func T(code uint64, template string) ErrCodeTemplate

func TN

func TN(namespace string, code uint64, template string) ErrCodeTemplate

func (*ErrCodeTemplate) IsEqual

func (p *ErrCodeTemplate) IsEqual(err error) bool

func (*ErrCodeTemplate) New

func (p *ErrCodeTemplate) New(v ...Params) (err ErrCode)

type Error

type Error struct {
	ID        string `json:"id"`
	Namespace string `json:"namespace"`
	Code      uint64 `json:"code"`
	Message   string `json:"message"`
}

type ErrorContext

type ErrorContext map[string]interface{}

func (ErrorContext) String

func (p ErrorContext) String() string

type Params

type Params map[string]interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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