xerror

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2020 License: MIT Imports: 4 Imported by: 23

README

xerror

错误码处理包:包含基础的错误处理

Usage

基本用法
package examples

import (
	"fmt"

	"github.com/save95/xerror/xcode"

	"github.com/save95/xerror"
)

func ExampleNew() {
	err := xerror.New("whoops")
	fmt.Println(err)
	fmt.Println(err.HttpStatus())
	fmt.Println(err.ErrorCode())

	// Output: whoops
	// 500
	// 500
}

func ExampleWithCode() {
	code := 1001
	err := xerror.WithCode(code, "server error message")
	fmt.Println(err)
	fmt.Println(err.HttpStatus())
	fmt.Println(err.ErrorCode())

	// Output:
	// server error message
	// 500
	// 1001
}

func ExampleWithXCode() {
	err := xerror.WithXCode(xcode.InternalServerError)
	fmt.Println(err)

	// Output:
	// 内部服务错误
}

更多用法请访问 错误码基本使用场景

进阶用法

License

MIT License

Documentation

Overview

github.com/pkg/errors 包兼容函数

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendCodes

func AppendCodes(codes ...xcode.XCode)

func Errorf

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

func IsErrorCode

func IsErrorCode(err error, code int) bool

func IsXCode

func IsXCode(err error, code xcode.XCode) bool

func LoadCodes

func LoadCodes(codes ...xcode.XCode)

func WithMessage

func WithMessage(err error, message string) error

func WithMessagef

func WithMessagef(err error, format string, args ...interface{}) error

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Types

type Error

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

func (*Error) Error

func (e *Error) Error() string

func (*Error) ErrorCode

func (e *Error) ErrorCode() int

func (*Error) GetFields

func (e *Error) GetFields() []interface{}

func (*Error) HttpStatus

func (e *Error) HttpStatus() int

func (*Error) String

func (e *Error) String() string

func (*Error) ToMessage

func (e *Error) ToMessage(config *ecode.Config) string

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) WithFields

func (e *Error) WithFields(field ...interface{})

type XError

type XError interface {
	// 获得错误的消息
	// 该消息为其包裹的 `XCode` 的 `String()` 的返回值;
	// 如果没有包裹 `XCode`,则返回固定字符串 `error`
	String() string
	// 获得 error 栈消息
	Error() string
	// 解包获得 error
	Unwrap() error
	// 获得错误的 HTTP STATUS
	HttpStatus() int
	// 获得错误码
	ErrorCode() int
	// 将错误转成消息
	ToMessage(config *ecode.Config) string
}

func New

func New(message string) XError

func WithCode

func WithCode(code int, message string) XError

func WithCodef

func WithCodef(code int, format string, args ...interface{}) XError

func WithXCode

func WithXCode(code xcode.XCode) XError

func WithXCodeMessage

func WithXCodeMessage(code xcode.XCode, message string) XError

func WithXCodeMessagef

func WithXCodeMessagef(code xcode.XCode, format string, args ...interface{}) XError

func Wrap

func Wrap(err error, message string) XError

func WrapWithCode

func WrapWithCode(err error, code int) XError

func WrapWithXCode

func WrapWithXCode(err error, code xcode.XCode) XError

type XFields

type XFields interface {
	// 支持属性
	WithFields(fields ...interface{})
	// 获得属性
	GetFields() []interface{}
}

type XLoadCodes

type XLoadCodes interface {
	// 加载自定义 code
	// 该方法可以覆写系统预置的错误码
	LoadCodes(codes ...xcode.XCode)
	// 追加自定义 code
	// 如果已存在,则直接跳过;如果需要覆写,请使用 `LoadCodes`
	AppendCodes(codes ...xcode.XCode)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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