Skip to content

Commit 0f0fd0f

Browse files
committed
fix Wrap caller depth and fix document
1 parent 93dfd39 commit 0f0fd0f

3 files changed

Lines changed: 28 additions & 30 deletions

File tree

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,22 @@ func main() {
3939
}
4040
```
4141

42-
* Analyze errors position information
42+
* Analyze errors position information of Error()
4343
```text
4444
Output:
4545
["test",["errors_test.go:90#errors.TestAs"],["errors_test.go:95#errors.TestAs",123,456]]
4646
4747
Decode:
48-
ErrData[0] -- the input of errors.New()
49-
ErrData[1:] -- position information
50-
ErrData[1][0] -- the position information when first calling 'As'
51-
ErrData[1][1:] -- the args when first calling 'As'
48+
["error code", ["runtime stack of New"], ["runtime stack of As", "args of As"...]]
49+
the first one is error code, the second is New, the others are As's called.
5250
```
5351

5452

5553
### Error handling suggestions
56-
57-
*) Prioritize handling errors before handling normal logic, as errors are less likely to be ignored and make the program more robust;
58-
*) Unless the error handling result is clearly defined, errors should always be returned to the caller;
59-
*) If it is not possible to return to the caller, user prompts or logs should be provided instead of discarding errors to fully understand what has happened in the program;
60-
*) Normal logic should not be written in if conditions to ensure good text indentation and reading of the code;
54+
* Prioritize handling errors before handling normal logic, where errors are less likely to be ignored and make the program more robust;
55+
* Unless the internal handling result of the error is specified, the error should be returned to the caller;
56+
* If there is no need to return to the caller, a log should be recorded instead of discarding the error
57+
* Normal logic should be avoided from being placed in 'if' as much as possible for easier indentation reading.
6158
```text
6259
// Suggest
6360
rows, err := db.Query(...)

README_zh.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ func main() {
4242
}
4343
```
4444

45-
* 定位信息解析
45+
* Error()定位信息解析
4646
```text
47-
Output
47+
输出
4848
["test",["errors_test.go:90#errors.TestAs"],["errors_test.go:95#errors.TestAs",123,456]]
4949
50-
Decode:
51-
ErrData[0] -- errors.New()输入的值
52-
ErrData[1:] -- 其他一维数组定位栈
53-
ErrData[1][0] -- 第一次调用时的位置信息,必有项
54-
ErrData[1][1:] -- 第一次调用时用户输入的参数, 用于手工记录必要的引起错误参数信息
50+
解释:
51+
["错误码", ["New记录的堆栈"], ["As方法记录的堆栈", "As方法记录的参数"...]]
52+
第一个位置是错误天,第二个位置是New的记录,其他位置是As的记录
53+
5554
```
5655

5756
* 读取信息用于自定义格式
@@ -69,8 +68,8 @@ for _, s := range stack{
6968
### 错误处理建议
7069

7170
* 优先处理错误,再处理正常逻辑, 此时错误将不容易被忽略而使程序更健壮;
72-
* 除非明确了错误处理结果,错误应总是向上一级返回给调用者
73-
* 若不能向上一级返回, 应给出用户提示或记录日志,而不是丢弃错误以便完整知道系统发生了什么。
71+
* 除非明确了错误的内部处理结果,否则错误应返回给调用者
72+
* 若不需向调用者返回, 应当记录日志,而不是丢弃错误
7473
* 正常逻辑尽可能不放在if中,以便于缩进阅读。
7574
```text
7675
// 建议写法

errors.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// Error recorder
1+
// Error recorder with As
2+
//
23
// recoding one code, and recording the As caller static with caller, argument information for every As func is called.
34
//
45
// the data static format like this:
5-
// ["error code", ["where stack of first caller ", "As args"...], ["where stack of second caller ", "As args"...]...]
6+
// ["error code", ["runtime stack of New"], ["runtime stack of As", "args of As"...], ["runtime statick of As"]...]
7+
// the first one is error code, the second is New, the others are func As been called.
68
//
79
// # Example
810
//
@@ -202,11 +204,18 @@ func caller(depth int) string {
202204
return fmt.Sprintf("%s:%d#%s", fileName, line, funcName)
203205
}
204206

205-
// Return the code of make.
207+
// Return the code of New or Parse.
206208
func (e *errImpl) Code() string {
207209
return e.data[0].(string)
208210
}
209211

212+
// Copy and return the stack array
213+
func (e *errImpl) Stack() []interface{} {
214+
stack := make([]interface{}, len(e.data)-1)
215+
copy(stack, e.data[1:])
216+
return stack
217+
}
218+
210219
// Implement the error interface of go package
211220
func (e *errImpl) Error() string {
212221
data, err := json.Marshal(e.data)
@@ -221,19 +230,12 @@ func (e *errImpl) MarshalJSON() ([]byte, error) {
221230
return json.Marshal(e.data)
222231
}
223232

224-
// Record the stack when call, and return a new error with new stack.
233+
// Record caller stack and return a new error interface.
225234
func (e *errImpl) As(args ...interface{}) Error {
226235
return as(3, e, args...)
227236
}
228237

229-
func (e *errImpl) Stack() []interface{} {
230-
stack := make([]interface{}, len(e.data)-1)
231-
copy(stack, e.data[1:])
232-
return stack
233-
}
234-
235238
// Compare to another error
236-
// It should be established with err1.Code() == err2.Code().
237239
func (e *errImpl) Equal(l error) bool {
238240
return equal(e, l)
239241
}

0 commit comments

Comments
 (0)