-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] package nazaerrors: 增加Wrap函数,用于封装error
- Loading branch information
1 parent
e96ce43
commit 7588a6d
Showing
6 changed files
with
85 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |