Skip to content

Commit

Permalink
fixing errs api
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Mar 21, 2024
1 parent 77ab717 commit f9da55c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
74 changes: 37 additions & 37 deletions app/services/engine/handlers/gamegrp/gamegrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ type handlers struct {
func (h *handlers) connect(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
address, err := validateSignature(ctx, h.log, r, h.connectTimeout, h.bank.Client().ChainID())
if err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

token, err := generateToken(h.auth, h.activeKID, address)
if err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

data := struct {
Expand Down Expand Up @@ -182,7 +182,7 @@ func (h *handlers) configuration(ctx context.Context, w http.ResponseWriter, r *
func (h *handlers) usd2Wei(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
usd, err := strconv.ParseFloat(web.Param(r, "usd"), 64)
if err != nil {
return errs.NewTrustedError(fmt.Errorf("converting usd: %s", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("converting usd: %s", err), http.StatusBadRequest)
}

wei := h.converter.USD2Wei(big.NewFloat(usd))
Expand Down Expand Up @@ -215,7 +215,7 @@ func (h *handlers) state(ctx context.Context, w http.ResponseWriter, r *http.Req

gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
Expand All @@ -235,13 +235,13 @@ func (h *handlers) state(ctx context.Context, w http.ResponseWriter, r *http.Req
func (h *handlers) newGame(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
g, err := game.New(ctx, h.log, h.converter, h.storer, h.bank, mid.GetSubject(ctx), h.anteUSD)
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to create game: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to create game: %w", err), http.StatusBadRequest)
}

subjectID := mid.GetSubject(ctx).String()

if err := evts.addPlayerToGame(g.ID(), subjectID); err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to add player %q to game: %w", subjectID, err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to add player %q to game: %w", subjectID, err), http.StatusBadRequest)
}

web.SetParam(r, "id", g.ID().String())
Expand All @@ -253,12 +253,12 @@ func (h *handlers) newGame(ctx context.Context, w http.ResponseWriter, r *http.R
func (h *handlers) join(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("no game exists"), http.StatusBadRequest)
}

n, err := evts.numberOfPlayers(g.ID())
Expand All @@ -267,17 +267,17 @@ func (h *handlers) join(ctx context.Context, w http.ResponseWriter, r *http.Requ
}

if n == 5 {
return errs.NewTrustedError(errors.New("max players sitting"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("max players sitting"), http.StatusBadRequest)
}

subjectID := mid.GetSubject(ctx)

if err := g.AddAccount(ctx, subjectID); err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

if err := evts.addPlayerToGame(g.ID(), subjectID.String()); err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to add player %q to game: %w", subjectID, err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to add player %q to game: %w", subjectID, err), http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "join")
Expand All @@ -289,16 +289,16 @@ func (h *handlers) join(ctx context.Context, w http.ResponseWriter, r *http.Requ
func (h *handlers) startGame(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("no game exists"), http.StatusBadRequest)
}

if err := g.StartGame(ctx); err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "start")
Expand All @@ -310,16 +310,16 @@ func (h *handlers) startGame(ctx context.Context, w http.ResponseWriter, r *http
func (h *handlers) rollDice(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("no game exists"), http.StatusBadRequest)
}

if err := g.RollDice(ctx, mid.GetSubject(ctx)); err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "rolldice")
Expand All @@ -331,28 +331,28 @@ func (h *handlers) rollDice(ctx context.Context, w http.ResponseWriter, r *http.
func (h *handlers) bet(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("no game exists"), http.StatusBadRequest)
}

number, err := strconv.Atoi(web.Param(r, "number"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("converting number: %s", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("converting number: %s", err), http.StatusBadRequest)
}

suit, err := strconv.Atoi(web.Param(r, "suit"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("converting suit: %s", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("converting suit: %s", err), http.StatusBadRequest)
}

address := mid.GetSubject(ctx)

if err := g.Bet(ctx, address, number, suit); err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "bet", "index", g.State().Cups[address].OrderIdx)
Expand All @@ -364,20 +364,20 @@ func (h *handlers) bet(ctx context.Context, w http.ResponseWriter, r *http.Reque
func (h *handlers) callLiar(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("no game exists"), http.StatusBadRequest)
}

if _, _, err := g.CallLiar(ctx, mid.GetSubject(ctx)); err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

if _, err := g.NextRound(ctx); err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "callliar")
Expand All @@ -389,19 +389,19 @@ func (h *handlers) callLiar(ctx context.Context, w http.ResponseWriter, r *http.
func (h *handlers) reconcile(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("no game exists"), http.StatusBadRequest)
}

ctx, cancel := context.WithTimeout(ctx, h.bankTimeout)
defer cancel()

if _, _, err := g.Reconcile(ctx); err != nil {
return errs.NewTrustedError(err, http.StatusInternalServerError)
return errs.NewTrusted(err, http.StatusInternalServerError)
}

evts.send(ctx, g.ID(), "reconcile")
Expand All @@ -418,7 +418,7 @@ func (h *handlers) balance(ctx context.Context, w http.ResponseWriter, r *http.R

balanceGWei, err := h.bank.AccountBalance(ctx, mid.GetSubject(ctx))
if err != nil {
return errs.NewTrustedError(err, http.StatusInternalServerError)
return errs.NewTrusted(err, http.StatusInternalServerError)
}

resp := struct {
Expand All @@ -434,16 +434,16 @@ func (h *handlers) balance(ctx context.Context, w http.ResponseWriter, r *http.R
func (h *handlers) nextTurn(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("no game exists"), http.StatusBadRequest)
}

if err := g.NextTurn(ctx); err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "nextturn")
Expand All @@ -457,23 +457,23 @@ func (h *handlers) nextTurn(ctx context.Context, w http.ResponseWriter, r *http.
func (h *handlers) updateOut(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrusted(errors.New("no game exists"), http.StatusBadRequest)
}

outs, err := strconv.Atoi(web.Param(r, "outs"))
if err != nil {
return errs.NewTrustedError(fmt.Errorf("converting outs: %s", err), http.StatusBadRequest)
return errs.NewTrusted(fmt.Errorf("converting outs: %s", err), http.StatusBadRequest)
}

address := mid.GetSubject(ctx)

if err := g.ApplyOut(ctx, address, outs); err != nil {
return errs.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrusted(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "outs")
Expand Down
28 changes: 14 additions & 14 deletions business/web/errs/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ package errs

import "errors"

// ErrorResponse is the form used for API responses from failures in the API.
type ErrorResponse struct {
// Response is the form used for API responses from failures in the API.
type Response struct {
Error string `json:"error"`
Fields map[string]string `json:"fields,omitempty"`
}

// TrustedError is used to pass an error during the request through the
// Trusted is used to pass an error during the request through the
// application with web specific context.
type TrustedError struct {
type Trusted struct {
Err error
Status int
}

// NewTrustedError wraps a provided error with an HTTP status code. This
// NewTrusted wraps a provided error with an HTTP status code. This
// function should be used when handlers encounter expected errors.
func NewTrustedError(err error, status int) error {
return &TrustedError{err, status}
func NewTrusted(err error, status int) error {
return &Trusted{err, status}
}

// Error implements the error interface. It uses the default message of the
// wrapped error. This is what will be shown in the services' logs.
func (te *TrustedError) Error() string {
func (te *Trusted) Error() string {
return te.Err.Error()
}

// IsTrustedError checks if an error of type TrustedError exists.
func IsTrustedError(err error) bool {
var te *TrustedError
// IsTrusted checks if an error of type TrustedError exists.
func IsTrusted(err error) bool {
var te *Trusted
return errors.As(err, &te)
}

// GetTrustedError returns a copy of the TrustedError pointer.
func GetTrustedError(err error) *TrustedError {
var te *TrustedError
// GetTrusted returns a copy of the TrustedError pointer.
func GetTrusted(err error) *Trusted {
var te *Trusted
if !errors.As(err, &te) {
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions business/web/mid/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@ func Errors(log *logger.Logger) web.MidHandler {
if err := handler(ctx, w, r); err != nil {
log.Error(ctx, "message", "msg", err)

var er errs.ErrorResponse
var er errs.Response
var status int

switch {
case errs.IsTrustedError(err):
trsErr := errs.GetTrustedError(err)
case errs.IsTrusted(err):
trsErr := errs.GetTrusted(err)

if validate.IsFieldErrors(trsErr.Err) {
fieldErrors := validate.GetFieldErrors(trsErr.Err)
er = errs.ErrorResponse{
er = errs.Response{
Error: "data validation error",
Fields: fieldErrors.Fields(),
}
status = trsErr.Status
break
}

er = errs.ErrorResponse{
er = errs.Response{
Error: trsErr.Error(),
}
status = trsErr.Status

case auth.IsAuthError(err):
er = errs.ErrorResponse{
er = errs.Response{
Error: http.StatusText(http.StatusUnauthorized),
}
status = http.StatusUnauthorized

default:
er = errs.ErrorResponse{
er = errs.Response{
Error: http.StatusText(http.StatusInternalServerError),
}
status = http.StatusInternalServerError
Expand Down

0 comments on commit f9da55c

Please sign in to comment.