errorformatter

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 13 Imported by: 4 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

errorformator

format go err with httpcode、errorcode、message struct

业务架构图

@startuml
!define error rectangle #lightgreen
!define errorCodeLib  rectangle #Implementation
!include <archimate/Archimate>
error "应用抛出错误A" as errorA
error "应用抛出错误B" as errorB
archimate #Strategy "错误码自动生成包" as generatorErrorCode  <<technology-device>> 
rectangle "错误码管理服务" as errorManager #orange
errorA -down-> generatorErrorCode
errorB -down->generatorErrorCode
generatorErrorCode -down->errorManager
Rel_Composition_Down(errorB,errorManager,"格式化错误")
Rel_Composition_Down(errorA,errorManager,"格式化错误")
@enduml

时序图

@startuml
participant error as "应用错误"
collections generatorErrorCode as "错误码自动生成包"
database errorManager as "错误码管理服务"
queue mq as "消息队列"
participant back as "离线服务" 

error -> generatorErrorCode : 委托生成错误码
generatorErrorCode-->error:返回错误码
generatorErrorCode->mq: 详细错误信息
mq->back: 处理错误详细信息
back->back: 处理错误
back->errorManager: 存储错误详细信息
error->errorManager:查询提示友好的错误信息
errorManager-->error: 返回错误提示
@enduml

错误码生成器uml

@startuml
namespace errorformatter {
    interface Causer  {
        + Cause() error

    }
    class CodeInfo << (S,Aquamarine) >> {
        + Code string
        + File string
        + Package string
        + Function string
        + Line string
        + Msg string
        + Cause *CodeInfo

    }
    interface ErrorChain  {
        + Run(fn <font color=blue>func</font>() error) ErrorChain
        + SetError(err error) ErrorChain
        + Error() error

    }
    class ErrorCode << (S,Aquamarine) >> {
        - cause error

        + HttpStatus int
        + Code string
        + Msg string
        + CodeInfo *CodeInfo

        + Error() string
        + Cause() error
        + ParseMsg(msg string) bool
        + TraceInfo() []*CodeInfo

    }
    class Formatter << (S,Aquamarine) >> {
        + Include []string
        + Exclude []string
        + HttpStatus <font color=blue>func</font>(string, string) (int, bool)
        + PCs <font color=blue>func</font>(error, []uintptr) int
        + Cause <font color=blue>func</font>(error) error
        + Chan <font color=blue>chan</font> *ErrorCode

        + Msg(msg string, args ...int) *ErrorCode
        + GenerateError(httpStatus int, businessCode string, msg string) error
        + WrapError(err error) *ErrorCode
        + Frames(frames *runtime.Frames) *CodeInfo
        + FuncName2CodeInfo(file string, fullFuncName string, line int) *CodeInfo
        + SendToChain(errorCode *ErrorCode) error

    }
    class GithubComPkgErrors << (S,Aquamarine) >> {
        + PCs(err error, pc []uintptr) int
        + Cause(err error) error

    }
    interface GithubComPkgErrorsStackTracer  {
        + StackTrace() errors.StackTrace

    }
    class chain << (S,Aquamarine) >> {
        - err error

        + Error() error
        + SetError(err error) ErrorChain
        + Run(fn <font color=blue>func</font>() error) ErrorChain

    }
}

"errorformatter.Causer" <|-- "errorformatter.ErrorCode"
"errorformatter.ErrorChain" <|-- "errorformatter.chain"

@enduml

软件执行流程图

@startuml

start
  :启动服务;
  :初始化错误自动生成包;
  :初始化详细错误存在接口;
repeat: 执行代码;
if(发生错误) then(抛出错误)
break
endif
repeat while (更多代码?)
if(有错误)then(错误处理)
:错误冒泡;
:生成错误码,并格式化错误;
:同步错误详细信息到错误管理服务;
:从错误管理服务获取预定义的错误提示;
else 
endif
:输出(output);
stop
@enduml

Documentation

Index

Constants

View Source
const (
	SEPARATOR       = '#'
	WITH_CALL_CHAIN = false
	SKIP            = 2
	FORMAT_TPL      = "%c%d:%s%c%s"
)

Variables

View Source
var ErrorMapFile = "errorMapFile.json"
View Source
var Exclude []string
View Source
var FuncHttpStatusMap map[string]int
View Source
var Include []string
View Source
var PackageHttpstatusMap map[string]int

Functions

func GetErrorChain

func GetErrorChain() <-chan *ErrorCode

GetErrorChain export errorChain

func GetErrorType

func GetErrorType(err error) string

func GetFuncHttpStatus

func GetFuncHttpStatus(packageName string, funcName string) (int, bool)

GetFuncHttpStatus 配置每个方法返回错误时,http 默认状态码

func ModuleName

func ModuleName(goModelfile string) (modName string, err error)

ModuleName help function, get mod package name from go.mod

func SaveCodeInfo

func SaveCodeInfo()

SaveCodeInfo 存储错误码信息

func Str2Int

func Str2Int(s string, out *int) (err error)

Str2Int help function for string to int conversion

Types

type Causer

type Causer interface {
	Cause() error
}

type CodeInfo

type CodeInfo struct {
	Code     string    `json:"code"`
	File     string    `json:"file"`
	Package  string    `json:"package"`
	Function string    `json:"function"`
	Line     string    `json:"line"`
	Msg      string    `json:"msg"`
	Cause    *CodeInfo `json:"cause"`
}

type ErrorChain

type ErrorChain interface {
	Run(fn func() error) ErrorChain
	SetError(err error) ErrorChain
	Error() error
}

ErrorChain 链式调用

func NewErrorChain

func NewErrorChain() ErrorChain

NewErrorChain nee ErrorChain

type ErrorCode

type ErrorCode struct {
	HttpStatus int    `json:"-"`
	Code       string `json:"code"`
	Msg        string `json:"msg"`

	CodeInfo *CodeInfo
	// contains filtered or unexported fields
}

func (*ErrorCode) Cause

func (e *ErrorCode) Cause() error

func (*ErrorCode) Error

func (e *ErrorCode) Error() string

func (*ErrorCode) ParseMsg

func (e *ErrorCode) ParseMsg(msg string) (ok bool)

ParseMsg parse string to *ErrorCode

func (*ErrorCode) TraceInfo

func (e *ErrorCode) TraceInfo() (traceList []*CodeInfo)

type Formatter

type Formatter struct {
	Include    []string
	Exclude    []string
	HttpStatus func(packageName string, funcName string) (int, bool)
	PCs        func(err error, pc []uintptr) (n int)
	Cause      func(err error) (tagetErr error)
	Chan       chan<- *ErrorCode
}

func GetErrFormatter

func GetErrFormatter() *Formatter

GetErrFormatter is a error formatter

func InitErrFormatter

func InitErrFormatter() *Formatter

func New

func New(
	include []string,
	exclude []string,
	httpStatus func(packageName string, funcName string) (int, bool),
	pcs func(err error, pc []uintptr) (n int),
	cause func(err error) (tagetErr error),
	ch chan<- *ErrorCode,
) (formatter *Formatter)

func (*Formatter) Frames

func (formatter *Formatter) Frames(frames *runtime.Frames) (codeInfo *CodeInfo)

Frames generate *CodeInfo from frames

func (*Formatter) FuncName2CodeInfo

func (formatter *Formatter) FuncName2CodeInfo(file string, fullFuncName string, line int) (codeInfo *CodeInfo)

FuncName2CodeInfo generate *CodeInfo from full function name

func (*Formatter) GenerateError

func (formatter *Formatter) GenerateError(httpStatus int, businessCode string, msg string) (err error)

func (*Formatter) Msg

func (formatter *Formatter) Msg(msg string, args ...int) (err *ErrorCode)

Msg generate *ErrorCode from msg

func (*Formatter) SendToChain

func (formatter *Formatter) SendToChain(errorCode *ErrorCode) (err error)

FuncName2CodeInfo generate *CodeInfo from full function name

func (*Formatter) WrapError

func (formatter *Formatter) WrapError(err error) (newErr *ErrorCode)

Error generate *ErrorCode from error

type GithubComPkgErrors

type GithubComPkgErrors struct{}

GithubComPkgErrors github.com/pkg/errors package implementation

func (*GithubComPkgErrors) Cause

func (pkgErrors *GithubComPkgErrors) Cause(err error) error

Cause implementation (*Formatter).Cause function

func (*GithubComPkgErrors) PCs

func (pkgErrors *GithubComPkgErrors) PCs(err error, pc []uintptr) (n int)

PCs implementation (*Formatter).PCs function

type GithubComPkgErrorsStackTracer

type GithubComPkgErrorsStackTracer interface {
	StackTrace() errors.StackTrace
}

Jump to

Keyboard shortcuts

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