Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API handlers to return error containing error key #558

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,10 @@
"properties": {
"error": {
"type": "string"
},
"error_code": {
"format": "int64",
"type": "integer"
}
},
"required": [
Expand Down
3 changes: 3 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7391,3 +7391,6 @@ components:
properties:
error:
type: string
error_code:
type: integer
format: int64
49 changes: 42 additions & 7 deletions pkg/api/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ 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/tonkeeper/opentonapi/pkg/references"

"github.com/go-faster/jx"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/boc"
Expand All @@ -22,15 +25,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"}}
// ErrorWithExtendedCode helps to pass additional information about an error.
type ErrorWithExtendedCode struct {
Code int
Message string
ExtendedCode references.ExtendedCode
}

func (e ErrorWithExtendedCode) 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 extendedCode(code references.ExtendedCode) oas.OptInt64 {
if code == 0 {
return oas.OptInt64{}
}
return oas.NewOptInt64(int64(code))
}

func toError(defaultCode int, err error) *oas.ErrorStatusCode {
var e ErrorWithExtendedCode
if errors.As(err, &e) {
return &oas.ErrorStatusCode{
StatusCode: e.Code,
Response: oas.Error{
Error: censor(e.Message),
ErrorCode: extendedCode(e.ExtendedCode),
},
}
}
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.

18 changes: 18 additions & 0 deletions pkg/references/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package references

type ExtendedCode int64

const (
ErrGaslessJettonIsNotSupported ExtendedCode = iota + 40_000
ErrGaslessTemporary
ErrGaslessSignature
ErrGaslessPendingMessages
ErrGaslessBadRequest
ErrGaslessOperationIsNotSupported
ErrGaslessUserDisabled
ErrGaslessEstimatingCommission
ErrGaslessCommission
ErrGaslessBalance
ErrGaslessBootstrapTransferDisabled
ErrGaslessUnknown
)
Loading