Skip to content

Commit

Permalink
[feat] package nazaerrors: 增加Wrap函数,用于封装error
Browse files Browse the repository at this point in the history
  • Loading branch information
q191201771 committed Jan 17, 2021
1 parent e96ce43 commit 7588a6d
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.9.x
- 1.13.x
- tip

before_install:
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,29 @@ pkg/ ...... 源码包
|-- bininfo/ ...... 将编译时源码的git版本信息(当前commit log的sha值和commit message),编译时间,Go版本,平台打入程序中
|-- nazalog/ ...... 日志库
|-- assert/ ...... 提供了单元测试时的断言功能,减少一些模板代码
|-- nazaerrors/ ...... error相关
|-- fake/ ...... 实现一些常用的接口,hook一些不方便测试的代码
|-- taskpool/ ...... 非阻塞协程池,协程数量可动态增长,可配置最大协程并发数量,可手动释放空闲的协程
|-- defertaskthread ...... 执行延时任务
|-- bele/ ...... 大小端转换操作
|-- nazabits/ ...... 位操作
|-- bitrate/ ...... 计算带宽
|-- ratelimit/ ...... 限流器,令牌桶,漏桶
|-- connection/ ...... 对net.Conn接口的二次封装
|-- nazanet/ ...... socket操作相关
|-- nazahttp/ ...... http操作
|-- circularqueue ...... 底层基于切片实现的固定容量大小的FIFO的环形队列
|-- lru ...... LRU缓存
|-- fake/ ...... 实现一些常用的接口,hook一些不方便测试的代码
|-- lru/ ...... LRU缓存
|-- consistenthash/ ...... 一致性哈希
|-- nazajson/ ...... json操作
|-- nazahttp/ ...... http操作
|-- unique/ ...... 对象唯一ID
|-- connection/ ...... 对net.Conn接口的二次封装
|-- nazareflect/ ...... 利用反射做的一些操作
|-- filebatch/ ...... 文件批处理操作
|-- nazamd5/ ...... md5操作
|-- nazaatomic/ ...... 原子操作
|-- snowflake/ ...... 分布式唯一性ID生成器
|-- slicebytepool/ ...... []byte内存池
|-- nazastring/ ...... string和[]byte相关的操作
|-- ratelimit/ ...... 限流器,令牌桶,漏桶
|-- nazamd5/ ...... md5操作
|-- unique/ ...... 对象唯一ID
|-- snowflake/ ...... 分布式唯一性ID生成器
|-- nazareflect/ ...... 利用反射做的一些操作
|-- filebatch/ ...... 文件批处理操作
|-- ic/ ...... 将整型切片压缩成二进制字节切片
playground/ ...... Go实验代码片段
demo/ ...... 示例相关的代码
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/q191201771/naza

go 1.12
go 1.13
1 change: 1 addition & 0 deletions pkg/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func New(conn net.Conn, modOptions ...ModOption) Connection {
go c.runWriteLoop()
}

nazalog.Debugf("naza connection New. net.Conn=%p, naza.Connection=%p", conn, c)
return c
}

Expand Down
40 changes: 40 additions & 0 deletions pkg/nazaerrors/wrap113.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef ([email protected])

// +build go1.13

package nazaerrors

import (
"errors"
"fmt"
"path/filepath"
"runtime"
)

func Wrap(err error) error {
if err == nil {
return nil
}

_, file, line, _ := runtime.Caller(1)
s := filepath.Base(file)
return fmt.Errorf("%w(%s:%d)", err, s, line)
}

func Unwrap(err error) error {
return errors.Unwrap(err)
}

func Is(err, target error) bool {
return errors.Is(err, target)
}

func As(err error, target interface{}) bool {
return errors.As(err, target)
}
30 changes: 30 additions & 0 deletions pkg/nazaerrors/wrap113_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef ([email protected])

// +build go1.13

package nazaerrors

import (
"errors"
"io"
"testing"

"github.com/q191201771/naza/pkg/assert"

"github.com/q191201771/naza/pkg/nazalog"
)

func TestWrap(t *testing.T) {
err := Wrap(io.EOF)
nazalog.Debugf("%+v", err)
assert.Equal(t, true, errors.Is(err, io.EOF))
err = Wrap(err)
nazalog.Debugf("%+v", err)
assert.Equal(t, true, errors.Is(err, io.EOF))
}

0 comments on commit 7588a6d

Please sign in to comment.