code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/errors.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package steps
    17  
    18  import (
    19  	"fmt"
    20  	"strconv"
    21  	"strings"
    22  
    23  	"code.vegaprotocol.io/vega/core/integration/stubs"
    24  	"code.vegaprotocol.io/vega/logging"
    25  	types "code.vegaprotocol.io/vega/protos/vega"
    26  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    27  
    28  	"golang.org/x/exp/maps"
    29  )
    30  
    31  type ErroneousRow interface {
    32  	ExpectError() bool
    33  	Error() string
    34  	Reference() string
    35  }
    36  
    37  func DebugTxErrors(broker *stubs.BrokerStub, log *logging.Logger) {
    38  	log.Info("DEBUGGING ALL TRANSACTION ERRORS")
    39  	data := broker.GetTxErrors()
    40  	for _, e := range data {
    41  		p := e.Proto()
    42  		log.Infof("TxError: %s\n", p.String())
    43  	}
    44  }
    45  
    46  func DebugLPSTxErrors(broker *stubs.BrokerStub, log *logging.Logger) {
    47  	log.Info("DEBUGGING LP SUBMISSION ERRORS")
    48  	data := broker.GetLPSErrors()
    49  	for _, e := range data {
    50  		p := e.Proto()
    51  		log.Infof("LP Submission error: %s - LP: %#v\n", p.String(), p.GetLiquidityProvisionSubmission)
    52  	}
    53  }
    54  
    55  // checkExpectedError checks if expected error has been returned,
    56  // if no expectation has been set a regular error check is carried out,
    57  // unexpectedErrDetail is an optional parameter that can be used to return a more detailed error when an unexpected error is encountered.
    58  func checkExpectedError(row ErroneousRow, returnedErr, unexpectedErrDetail error) error {
    59  	if row.ExpectError() && returnedErr == nil {
    60  		return fmt.Errorf("action on %q should have fail", row.Reference())
    61  	}
    62  
    63  	if returnedErr != nil {
    64  		if !row.ExpectError() {
    65  			if unexpectedErrDetail != nil {
    66  				return unexpectedErrDetail
    67  			}
    68  			return fmt.Errorf("action on %q has failed: %s", row.Reference(), returnedErr.Error())
    69  		}
    70  
    71  		if row.Error() != returnedErr.Error() {
    72  			return formatDiff(fmt.Sprintf("action on %q is failing as expected but not with the expected error message", row.Reference()),
    73  				map[string]string{
    74  					"error": row.Error(),
    75  				},
    76  				map[string]string{
    77  					"error": returnedErr.Error(),
    78  				},
    79  			)
    80  		}
    81  	}
    82  	return nil
    83  }
    84  
    85  func formatDiff(msg string, expected, got map[string]string) error {
    86  	var expectedStr strings.Builder
    87  	var gotStr strings.Builder
    88  	padding := findLongestKeyLen(expected) + 1
    89  	formatStr := "\n\t\t%-*s(%s)"
    90  	for name, value := range expected {
    91  		_, _ = fmt.Fprintf(&expectedStr, formatStr, padding, name, value)
    92  		_, _ = fmt.Fprintf(&gotStr, formatStr, padding, name, got[name])
    93  	}
    94  
    95  	return fmt.Errorf("%s\n\texpected:%s\n\tgot:%s",
    96  		msg,
    97  		expectedStr.String(),
    98  		gotStr.String(),
    99  	)
   100  }
   101  
   102  func findLongestKeyLen(expected map[string]string) int {
   103  	keys := maps.Keys(expected)
   104  	maxLen := 0
   105  	for i := range keys {
   106  		iLen := len(keys[i])
   107  		if iLen > maxLen {
   108  			maxLen = iLen
   109  		}
   110  	}
   111  	return maxLen
   112  }
   113  
   114  func u64ToS(n uint64) string {
   115  	return strconv.FormatUint(n, 10)
   116  }
   117  
   118  func u64SToS(ns []uint64) string {
   119  	ss := []string{}
   120  	for _, n := range ns {
   121  		ss = append(ss, u64ToS(n))
   122  	}
   123  	return strings.Join(ss, " ")
   124  }
   125  
   126  func i64ToS(n int64) string {
   127  	return strconv.FormatInt(n, 10)
   128  }
   129  
   130  func errOrderNotFound(reference string, party string, err error) error {
   131  	return fmt.Errorf("order not found for party(%s) with reference(%s): %v", party, reference, err)
   132  }
   133  
   134  func errMarketDataNotFound(marketID string, err error) error {
   135  	return fmt.Errorf("market data not found for market(%v): %s", marketID, err.Error())
   136  }
   137  
   138  type CancelOrderError struct {
   139  	reference string
   140  	request   commandspb.OrderCancellation
   141  	Err       error
   142  }
   143  
   144  func (c CancelOrderError) Error() string {
   145  	return fmt.Sprintf("failed to cancel order [%v] with reference %s: %v", c.request, c.reference, c.Err)
   146  }
   147  
   148  func (c *CancelOrderError) Unwrap() error { return c.Err }
   149  
   150  type SubmitOrderError struct {
   151  	reference string
   152  	request   commandspb.OrderSubmission
   153  	Err       error
   154  }
   155  
   156  func (s SubmitOrderError) Error() string {
   157  	return fmt.Sprintf("failed to submit order [%v] with reference %s: %v", s.request, s.reference, s.Err)
   158  }
   159  
   160  func (s *SubmitOrderError) Unwrap() error { return s.Err }
   161  
   162  func errOrderEventsNotFound(party, marketID string, side types.Side, size, price uint64) error {
   163  	return fmt.Errorf("no matching order event found %v, %v, %v, %v, %v", party, marketID, side.String(), size, price)
   164  }
   165  
   166  func errNoWatchersSpecified(netparam string) error {
   167  	return fmt.Errorf("no watchers specified for network parameter `%v`", netparam)
   168  }
   169  
   170  func errStopOrderEventsNotFound(party, marketID string, status types.StopOrder_Status) error {
   171  	return fmt.Errorf("no matching stop order event found %v, %v, %v", party, marketID, status.String())
   172  }