Skip to content

Commit

Permalink
API handlers to return error containing error key
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Dec 2, 2024
1 parent 082cbd9 commit 136529b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
3 changes: 3 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,9 @@
"properties": {
"error": {
"type": "string"
},
"error_key": {
"type": "string"
}
},
"required": [
Expand Down
2 changes: 2 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7391,3 +7391,5 @@ components:
properties:
error:
type: string
error_key:
type: string
48 changes: 41 additions & 7 deletions pkg/api/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package api
import (
"context"
"encoding/json"
"errors"
"fmt"
imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"
"math/big"
"reflect"
"strconv"
"strings"
"unicode"

imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"

"github.com/go-faster/jx"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/boc"
Expand All @@ -22,15 +24,47 @@ import (
walletPkg "github.com/tonkeeper/opentonapi/pkg/wallet"
)

func toError(code int, err error) *oas.ErrorStatusCode {
if strings.HasPrefix(err.Error(), "failed to connect to") || strings.Contains(err.Error(), "host=") {
return &oas.ErrorStatusCode{StatusCode: code, Response: oas.Error{Error: "unknown error"}}
// Error helps to pass additional information about an error from private tonapi2 handlers.
type Error struct {
Code int
Message string
Key string
}

func (e Error) Error() string {
return e.Message
}

// censor removes sensitive information from the error message.
func censor(msg string) string {
if strings.HasPrefix(msg, "failed to connect to") || strings.Contains(msg, "host=") {
return "unknown error"
}
return msg
}

func optKey(s string) oas.OptString {
if s == "" {
return oas.OptString{}
}
return oas.NewOptString(s)
}

func toError(defaultCode int, err error) *oas.ErrorStatusCode {
var e Error
if errors.As(err, &e) {
return &oas.ErrorStatusCode{
StatusCode: e.Code,
Response: oas.Error{
Error: censor(e.Message),
ErrorKey: optKey(e.Key),
},
}
}
if s, ok := status.FromError(err); ok {
return &oas.ErrorStatusCode{StatusCode: code, Response: oas.Error{Error: s.Message()}}
return &oas.ErrorStatusCode{StatusCode: defaultCode, Response: oas.Error{Error: censor(s.Message())}}
}
msg := err.Error()
return &oas.ErrorStatusCode{StatusCode: code, Response: oas.Error{Error: msg}}
return &oas.ErrorStatusCode{StatusCode: defaultCode, Response: oas.Error{Error: censor(err.Error())}}
}

func anyToJSONRawMap(a any) map[string]jx.Raw { //todo: переписать этот ужас
Expand Down
19 changes: 18 additions & 1 deletion pkg/oas/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion pkg/oas/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 136529b

Please sign in to comment.