Skip to content

Commit 51ed016

Browse files
committed
Expose error code and message.
1 parent 01ffdab commit 51ed016

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

internal/protocol/basic.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,17 @@ func (r *BasicResp) Err() error {
2727
if r.State {
2828
return nil
2929
}
30-
errCode := findNonZero(
30+
return errors.Get(util.NonZero(
3131
r.ErrorCode.Int(),
3232
r.ErrorCode2,
3333
r.ErrorCode3,
3434
r.ErrorCode4,
3535
r.ErrorCode5,
36-
)
37-
return errors.Get(errCode)
38-
}
39-
40-
func findNonZero(code ...int) int {
41-
for _, c := range code {
42-
if c != 0 {
43-
return c
44-
}
45-
}
46-
return 0
36+
), util.NonEmptyString(
37+
r.ErrorMessage,
38+
r.ErrorMessage2,
39+
r.ErrorMessage3,
40+
))
4741
}
4842

4943
// StandardResp is the response for all JSON/JSONP APIs with "data" field.

internal/protocol/qrcode.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package protocol
33
import (
44
"encoding/json"
55

6+
"github.com/deadblue/elevengo/internal/util"
67
"github.com/deadblue/elevengo/lowlevel/errors"
78
)
89

@@ -21,7 +22,9 @@ func (r *QrcodeBaseResp) Err() error {
2122
if r.State != 0 {
2223
return nil
2324
}
24-
return errors.Get(r.ErrorCode1)
25+
return errors.Get(r.ErrorCode1, util.NonEmptyString(
26+
r.ErrorMessage1, r.ErrorMessage2,
27+
))
2528
}
2629

2730
func (r *QrcodeBaseResp) Extract(v any) error {

internal/util/value.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package util
2+
3+
func NonZero(number ...int) int {
4+
for _, n := range number {
5+
if n != 0 {
6+
return n
7+
}
8+
}
9+
return 0
10+
}
11+
12+
func NonEmptyString(str ...string) string {
13+
for _, s := range str {
14+
if s != "" {
15+
return s
16+
}
17+
}
18+
return ""
19+
}

lowlevel/errors/get.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package errors
22

3+
import "fmt"
4+
35
var errorsMap = map[int]error{
46
// Normal errors
57
99: ErrNotLogin,
68
911: ErrCaptchaRequired,
79
990001: ErrNotLogin,
810
// Offline errors
9-
10004: ErrOfflineInvalidLink,
11+
CodeOfflineIllegalLink: ErrOfflineInvalidLink,
1012
// File errors
1113
20004: ErrExist,
1214
20022: ErrInvalidOperation,
@@ -29,9 +31,21 @@ var errorsMap = map[int]error{
2931
CodeOfflineTaskExists: nil,
3032
}
3133

32-
func Get(code int) error {
34+
type ApiError struct {
35+
Code int
36+
Message string
37+
}
38+
39+
func (e *ApiError) Error() string {
40+
return fmt.Sprintf("(%d)%s", e.Code, e.Message)
41+
}
42+
43+
func Get(code int, message string) error {
3344
if err, found := errorsMap[code]; found {
3445
return err
3546
}
36-
return ErrUnexpected
47+
return &ApiError{
48+
Code: code,
49+
Message: message,
50+
}
3751
}

0 commit comments

Comments
 (0)