@@ -3,14 +3,17 @@ package api
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
- imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"
8
8
"math/big"
9
9
"reflect"
10
10
"strconv"
11
11
"strings"
12
12
"unicode"
13
13
14
+ imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"
15
+ "github.com/tonkeeper/opentonapi/pkg/references"
16
+
14
17
"github.com/go-faster/jx"
15
18
"github.com/tonkeeper/tongo"
16
19
"github.com/tonkeeper/tongo/boc"
@@ -22,15 +25,47 @@ import (
22
25
walletPkg "github.com/tonkeeper/opentonapi/pkg/wallet"
23
26
)
24
27
25
- func toError (code int , err error ) * oas.ErrorStatusCode {
26
- if strings .HasPrefix (err .Error (), "failed to connect to" ) || strings .Contains (err .Error (), "host=" ) {
27
- return & oas.ErrorStatusCode {StatusCode : code , Response : oas.Error {Error : "unknown error" }}
28
+ // ErrorWithExtendedCode helps to pass additional information about an error.
29
+ type ErrorWithExtendedCode struct {
30
+ Code int
31
+ Message string
32
+ ExtendedCode references.ExtendedCode
33
+ }
34
+
35
+ func (e ErrorWithExtendedCode ) Error () string {
36
+ return e .Message
37
+ }
38
+
39
+ // censor removes sensitive information from the error message.
40
+ func censor (msg string ) string {
41
+ if strings .HasPrefix (msg , "failed to connect to" ) || strings .Contains (msg , "host=" ) {
42
+ return "unknown error"
43
+ }
44
+ return msg
45
+ }
46
+
47
+ func extendedCode (code references.ExtendedCode ) oas.OptInt64 {
48
+ if code == 0 {
49
+ return oas.OptInt64 {}
50
+ }
51
+ return oas .NewOptInt64 (int64 (code ))
52
+ }
53
+
54
+ func toError (defaultCode int , err error ) * oas.ErrorStatusCode {
55
+ var e ErrorWithExtendedCode
56
+ if errors .As (err , & e ) {
57
+ return & oas.ErrorStatusCode {
58
+ StatusCode : e .Code ,
59
+ Response : oas.Error {
60
+ Error : censor (e .Message ),
61
+ ErrorCode : extendedCode (e .ExtendedCode ),
62
+ },
63
+ }
28
64
}
29
65
if s , ok := status .FromError (err ); ok {
30
- return & oas.ErrorStatusCode {StatusCode : code , Response : oas.Error {Error : s .Message ()}}
66
+ return & oas.ErrorStatusCode {StatusCode : defaultCode , Response : oas.Error {Error : censor ( s .Message () )}}
31
67
}
32
- msg := err .Error ()
33
- return & oas.ErrorStatusCode {StatusCode : code , Response : oas.Error {Error : msg }}
68
+ return & oas.ErrorStatusCode {StatusCode : defaultCode , Response : oas.Error {Error : censor (err .Error ())}}
34
69
}
35
70
36
71
func anyToJSONRawMap (a any ) map [string ]jx.Raw { //todo: переписать этот ужас
0 commit comments