diff --git a/README.md b/README.md index 5f61810..abd05f2 100644 --- a/README.md +++ b/README.md @@ -390,31 +390,14 @@ import "github.com/duke-git/lancet/validator" - [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsStrongPassword) - [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl) - [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword) -### 14. error helpers - -- Contain functions to handle errors -- Usage: import "github.com/duke-git/lancet/xerror" +### 14. xerror +xerror package implements helpers for errors. ```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/errors" -) - -func main() { - x := Unwrap(strconv.Atoi("42")) // Unwrap if err is nil then it returns a valid value otherwise it panics -} -``` - -- Function list: - -```go -Unwrap[T any](val T, err error) //if err is nil then it returns a valid value otherwise it panics +import "github.com/duke-git/lancet/xerror" ``` +#### Function list: +- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#Unwrap) ## How to Contribute diff --git a/README_zh-CN.md b/README_zh-CN.md index 341f4da..0973c77 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -392,31 +392,15 @@ import "github.com/duke-git/lancet/validator" - [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsUrl) - [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword) -### error helpers - -- 错误处理函数 -- Usage: import "github.com/duke-git/lancet/xerror" +validator.md#IsWeakPassword) +### 14. xerror +xerror包实现一些错误处理函数 ```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/errors" -) - -func main() { - x := Unwrap(strconv.Atoi("42")) -} -``` - -- Function list: - -```go -Unwrap[T any](val T, err error) //如果err是nil,返回有效的val值。否则, panics +import "github.com/duke-git/lancet/xerror" ``` +#### 函数列表: +- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap) ## 如何贡献代码 diff --git a/docs/xerror.md b/docs/xerror.md new file mode 100644 index 0000000..64e364e --- /dev/null +++ b/docs/xerror.md @@ -0,0 +1,57 @@ +# Xerror +Package xerror implements helpers for errors. + +
+ +## Source: + +- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go) + +
+ +## Usage: +```go +import ( + "github.com/duke-git/lancet/xerror" +) +``` + +
+ +## Index +- [Unwrap](#Unwrap) + +
+ +## Documentation + + + +### Unwrap +

Unwrap if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.

+ +Signature: + +```go +func Unwrap[T any](val T, err error) T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/xerror" +) + +func main() { + _, err := strconv.Atoi("4o2") + defer func() { + v := recover() + fmt.Println(err.Error()) // err.Error() == v.(*strconv.NumError).Error() + }() + + xerror.Unwrap(strconv.Atoi("4o2")) +} +``` diff --git a/docs/xerror_zh-CN.md b/docs/xerror_zh-CN.md new file mode 100644 index 0000000..09f1a3d --- /dev/null +++ b/docs/xerror_zh-CN.md @@ -0,0 +1,57 @@ +# Xerror +xerror错误处理逻辑封装 + +
+ +## 源码: + +- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go) + +
+ +## 用法: +```go +import ( + "github.com/duke-git/lancet/xerror" +) +``` + +
+ +## 目录 +- [Unwrap](#Unwrap) + +
+ +## 文档 + + + +### Unwrap +

如果err为nil则展开,则它返回一个有效值。 如果err不是nil则Unwrap使用err发生恐慌。

+ +函数签名: + +```go +func Unwrap[T any](val T, err error) T +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/xerror" +) + +func main() { + _, err := strconv.Atoi("4o2") + defer func() { + v := recover() + fmt.Println(err.Error()) // err.Error() == v.(*strconv.NumError).Error() + }() + + xerror.Unwrap(strconv.Atoi("4o2")) +} +```