common

package
Version: v0.0.0-...-26d5523 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2017 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//CodeSignFormater fromat error code to string
	CodeSignFormater = "S%05d"

	//CodeMissingClientID not found clientID error code
	CodeMissingClientID = iota
	//CodeMissingSign not found sign error code
	CodeMissingSign
	//CodeInvalidClientID invalid clientID error code
	CodeInvalidClientID
	//CodeInvalidSign invalid sign error code
	CodeInvalidSign

	//CodeReplayAttack replay attack error code
	CodeReplayAttack
	//CodeTimestampFormat timstamp format not use RCF3339
	CodeTimestampFormat
	//CodeRequestExpired request expired
	CodeRequestExpired
)

Variables

View Source
var ErrorFormater = func(b *BaseError) string {
	return fmt.Sprintf("{\"code\":\"%s\", \"msg\":\"%s\"}", b.GetCode(), b.msg)
}

ErrorFormater error formater type func(string, string)

View Source
var Pack = func(ctx *gin.Context, params ...interface{}) interface{} {
	flag := 0
	result := &BaseResult{
		Method: ctx.Request.Method,
	}
	for _, p := range params {
		if p == nil {
			continue
		}
		if e, ok := p.(IError); ok && flag&1 == 0 {
			result.Error = &ErrorResult{
				Code: e.GetCode(),
				Msg:  e.GetMsg(),
			}
		} else if e, ok := p.(error); ok && flag&1 == 0 {
			result.Error = &ErrorResult{
				Msg: e.Error(),
			}
		} else if flag&2 == 1 {
			result.Data = p
		}
	}
	return result
}

Pack pack data or error to map for result to client eg. Pack(ctx, data, err)

Pack(ctx, err)
Pack(ctx, data)

Functions

func Error

func Error(args ...interface{})

Error log info

func Info

func Info(args ...interface{})

Info log info

func NewError

func NewError(code int, msg string) error

NewError create a error

func SetLogger

func SetLogger(l ILogger)

SetLogger custom logger

func Warn

func Warn(args ...interface{})

Warn log info

Types

type BaseError

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

BaseError base error

func (*BaseError) Error

func (b *BaseError) Error() string

Error return format error string

func (*BaseError) GetCode

func (b *BaseError) GetCode() string

GetCode get error code and format it

func (*BaseError) GetMsg

func (b *BaseError) GetMsg() string

GetMsg get error msg

type BaseResult

type BaseResult struct {
	APIVersion string
	ID         string
	Method     string
	Error      interface{}
	Data       interface{}
}

BaseResult result formater

type Console

type Console struct{}

Console console log

func (Console) Error

func (Console) Error(args ...interface{})

Error log info

func (Console) Info

func (Console) Info(args ...interface{})

Info log info

func (Console) Warn

func (Console) Warn(args ...interface{})

Warn log info

type ErrorResult

type ErrorResult struct {
	Code string
	Msg  string
}

ErrorResult error formater in result json

type IError

type IError interface {
	GetCode() string
	GetMsg() string
}

IError custom error interface

type ILogger

type ILogger interface {
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
}

ILogger logger interface

type M

type M map[string]interface{}

M alias of map[string]interface{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL