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 8484a5a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 9 deletions.
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"
},
"extended_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
extended_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),
ExtendedCode: 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
)

0 comments on commit 8484a5a

Please sign in to comment.