mirror of
https://github.com/alexgo-io/gaze-indexer.git
synced 2026-04-30 04:35:13 +08:00
fix: use errs in api
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
package httphandler
|
||||
|
||||
import "github.com/cockroachdb/errors"
|
||||
|
||||
// TODO: rewrite entire error handling logic
|
||||
// may create new custom error type in errs package for using to return errors to client
|
||||
|
||||
type ValidationError struct {
|
||||
errs []error
|
||||
}
|
||||
|
||||
func (v ValidationError) Error() string {
|
||||
return errors.Join(v.errs...).Error()
|
||||
}
|
||||
|
||||
func NewValidationError(errs ...error) error {
|
||||
if len(errs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &ValidationError{errs: errs}
|
||||
}
|
||||
|
||||
type UserError struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (u UserError) Error() string {
|
||||
return u.err.Error()
|
||||
}
|
||||
|
||||
func NewUserError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return &UserError{err: err}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package httphandler
|
||||
|
||||
import (
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/gaze-network/indexer-network/common/errs"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
@@ -28,11 +29,11 @@ type getBalancesByAddressResult struct {
|
||||
type getBalancesByAddressResponse = HttpResponse[getBalancesByAddressResult]
|
||||
|
||||
func (r getBalancesByAddressRequest) Validate() error {
|
||||
var errs []error
|
||||
var errList []error
|
||||
if r.Wallet == "" {
|
||||
errs = append(errs, errors.New("'wallet' is required"))
|
||||
errList = append(errList, errors.New("'wallet' is required"))
|
||||
}
|
||||
return NewValidationError(errs...)
|
||||
return errs.WithPublicMessage(errors.Join(errList...), "validation error")
|
||||
}
|
||||
|
||||
func (h *HttpHandler) GetBalancesByAddress(ctx *fiber.Ctx) (err error) {
|
||||
@@ -49,7 +50,7 @@ func (h *HttpHandler) GetBalancesByAddress(ctx *fiber.Ctx) (err error) {
|
||||
|
||||
pkScript, ok := h.resolvePkScript(h.network, req.Wallet)
|
||||
if !ok {
|
||||
return NewUserError(errors.New("unable to resolve pkscript from \"wallet\""))
|
||||
return errs.NewPublicError("unable to resolve pkscript from \"wallet\"")
|
||||
}
|
||||
|
||||
blockHeight := req.BlockHeight
|
||||
|
||||
Reference in New Issue
Block a user