exp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2021 License: MIT Imports: 2 Imported by: 0

README

exp

介绍

ngro 扩展的错误类型接口

使用说明
  1. 创建 exp

    // New 创建一个Code为819(DEFAULT_ERROR_CODE)的常规错误
    func New(message string) Exp 
    
    // NewF 创建一个Code为819(DEFAULT_ERROR_CODE)的常规错误
    func NewF(format string, args ...interface{}) Exp 
    
    // Code 创建一个包含自定义错误代码的错误
    func Code(code int, message string) Exp 
    
    // CodeF 创建一个包含自定义错误代码的错误
    func CodeF(code int, format string, args ...interface{}) Exp 
    
    // Wrap 创建一个包含自定义错误的错误,并包含引发此错误的原始错误
    func Wrap(cause error, code int, message string) Exp 
    
    // WrapF 创建一个包含自定义错误的错误,并包含引发此错误的原始错误
    func WrapF(cause error, code int, format string, args ...interface{}) Exp 
    
  2. 使用 exp

    e := New(`exception`)
    
    e.Error()		// exception
    e.Code()		// 819
    
    e2 := Wrap(errors.New(`cause`), 888, `exception`)
    e2.Cause()		// {cause}
    
    
  3. 错误类型判断和堆栈信息

    // IsExp 校验 error 是否是对 Exp 的实现
    func IsExp(err error) bool
    
    // CallStack 获取当前调用堆栈
    // offset 指定从当前调用起需要忽略的堆栈
    func CallStack(offset ...int) []string
    

Documentation

Overview

Package exp 定义 ngro 扩展的错误类型接口 与内置error的不同点

	1、在于多提供错误代码 code,用于后续的帮助文档
 2、通过原始 error,创建 exp 来包装原始错误

Index

Constants

View Source
const (
	// DEFAULT_ERROR_CODE ��默认错误代码
	DEFAULT_ERROR_CODE = 819
)

Variables

This section is empty.

Functions

func CallStack

func CallStack(offset ...int) []string

CallStack 获取当前调用堆栈 offset 指定从当前调用起需要忽略的堆栈

func IsExp

func IsExp(err error) bool

IsExp 校验 error 是否是对 Exp 的实现 即判断错误是否是 ngro 扩展的自定义错误,以处理后续对Code处理的依赖

Types

type Exp

type Exp interface {
	// Error 继承原始 error 接口,方便转换到 error
	Error() string
	// Code 返回错误关联的错误代码
	Code() int
	// Cause 返回引发错误的原始 error
	Cause() error
}

Exp 描述 ngro 扩展的错误接口

func Code

func Code(code int, message string) Exp

Code 创建一个包含自定义错误代码的错误

func CodeF

func CodeF(code int, format string, args ...interface{}) Exp

CodeF 创建一个包含自定义错误代码的错误

func New

func New(message string) Exp

New 创建一个Code为819(DEFAULT_ERROR_CODE)的常规错误 如需要自定义错误代码,请使用 exp.Code

func NewF

func NewF(format string, args ...interface{}) Exp

NewF 创建一个Code为819(DEFAULT_ERROR_CODE)的常规错误 如需要自定义错误代码,请使用 exp.CodeF

func Wrap

func Wrap(cause error, code int, message string) Exp

Wrap 创建一个包含自定义错误的错误,并包含引发此错误的原始错误

func WrapF

func WrapF(cause error, code int, format string, args ...interface{}) Exp

WrapF 创建一个包含自定义错误的错误,并包含引发此错误的原始错误

Jump to

Keyboard shortcuts

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