gitlab.com/evatix-go/core@v1.3.55/errcore/RawErrCollection.go (about)

     1  package errcore
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"log"
     8  	"strings"
     9  
    10  	"gitlab.com/evatix-go/core/constants"
    11  )
    12  
    13  type RawErrCollection struct {
    14  	Items []error
    15  }
    16  
    17  func (it RawErrCollection) AddMsg(message string) {
    18  	it.AddString(message)
    19  }
    20  
    21  func (it RawErrCollection) AddMessages(
    22  	messages ...string,
    23  ) {
    24  	if len(messages) == 0 {
    25  		return
    26  	}
    27  
    28  	compiled := strings.Join(
    29  		messages, constants.Space)
    30  
    31  	it.AddString(compiled)
    32  }
    33  
    34  func (it RawErrCollection) AddErrorWithMessage(
    35  	err error,
    36  	message string,
    37  ) {
    38  	if err == nil {
    39  		return
    40  	}
    41  
    42  	it.Add(ConcatMessageWithErr(message, err))
    43  }
    44  
    45  func (it RawErrCollection) AddErrorWithMessageRef(
    46  	err error,
    47  	message string,
    48  	reference interface{},
    49  ) {
    50  	if err == nil {
    51  		return
    52  	}
    53  
    54  	referenceString := constants.NilAngelBracket
    55  	if reference != nil {
    56  		referenceString = fmt.Sprintf(
    57  			constants.ReferenceWrapFormat,
    58  			referenceString)
    59  	}
    60  
    61  	it.AddErrorWithMessage(
    62  		ConcatMessageWithErr(message, err), referenceString)
    63  }
    64  
    65  func (it RawErrCollection) Fmt(format string, v ...interface{}) {
    66  	if format == "" && len(v) == 0 {
    67  		return
    68  	}
    69  
    70  	message := fmt.Sprintf(
    71  		format,
    72  		v...)
    73  
    74  	it.AddString(message)
    75  }
    76  
    77  func (it RawErrCollection) FmtIf(
    78  	isAdd bool,
    79  	format string,
    80  	v ...interface{},
    81  ) {
    82  	if !isAdd {
    83  		return
    84  	}
    85  
    86  	it.Fmt(format, v...)
    87  }
    88  
    89  func (it RawErrCollection) References(
    90  	message string,
    91  	v ...interface{},
    92  ) {
    93  	referencesCompiled := fmt.Sprintf(
    94  		constants.MessageReferenceWrapFormat,
    95  		message,
    96  		v)
    97  
    98  	it.AddString(referencesCompiled)
    99  }
   100  
   101  func (it RawErrCollection) MustBeSafe() {
   102  	if it.IsEmpty() {
   103  		return
   104  	}
   105  
   106  	panic(it.CompiledError())
   107  }
   108  
   109  func (it RawErrCollection) HasAnyIssues() bool {
   110  	return !it.IsEmpty()
   111  }
   112  
   113  func (it RawErrCollection) IsDefined() bool {
   114  	return !it.IsEmpty()
   115  }
   116  
   117  func (it RawErrCollection) CompiledJsonErrorWithStackTraces() error {
   118  	allBytes, err := it.MarshalJSON()
   119  
   120  	if err == nil {
   121  		return errors.New(string(allBytes))
   122  	}
   123  
   124  	return ConcatMessageWithErr(string(allBytes), err)
   125  }
   126  
   127  func (it RawErrCollection) CompiledJsonStringWithStackTraces() (jsonString string) {
   128  	err := it.CompiledJsonErrorWithStackTraces()
   129  
   130  	if err == nil {
   131  		return ""
   132  	}
   133  
   134  	return err.Error()
   135  }
   136  
   137  func (it RawErrCollection) MustBeEmptyError() {
   138  	it.MustBeSafe()
   139  }
   140  
   141  func (it RawErrCollection) IsCollectionType() bool {
   142  	return true
   143  }
   144  
   145  func (it RawErrCollection) ReflectSetTo(toPtr interface{}) error {
   146  	switch v := toPtr.(type) {
   147  	case RawErrCollection:
   148  		return FailedToConvertType.Error(
   149  			"cannot convert to value type for RawErrCollection!",
   150  			toPtr)
   151  	case *RawErrCollection:
   152  		if v == nil {
   153  			return FailedToConvertType.
   154  				Error(
   155  					"cannot convert to value type for RawErrCollection to nil ptr!",
   156  					toPtr)
   157  		}
   158  
   159  		*v = it
   160  
   161  		return nil
   162  	}
   163  
   164  	return NotSupportedType.
   165  		Error(
   166  			"RawErrCollection.ReflectSetTo is not supported for other than ptr same time.",
   167  			toPtr)
   168  }
   169  
   170  func (it RawErrCollection) HandleError() {
   171  	if it.IsEmpty() {
   172  		return
   173  	}
   174  
   175  	panic(it.Items)
   176  }
   177  
   178  func (it *RawErrCollection) IsNull() bool {
   179  	return it == nil || it.Items == nil
   180  }
   181  
   182  func (it *RawErrCollection) IsAnyNull() bool {
   183  	return it == nil || it.Items == nil
   184  }
   185  
   186  func (it RawErrCollection) ErrorString() string {
   187  	return it.String()
   188  }
   189  
   190  func (it RawErrCollection) Compile() string {
   191  	return it.String()
   192  }
   193  
   194  func (it RawErrCollection) HandleErrorWithRefs(
   195  	newMessage string,
   196  	refVar, refVal interface{},
   197  ) {
   198  	if it.IsEmpty() {
   199  		return
   200  	}
   201  
   202  	reference :=
   203  		fmt.Sprintf(
   204  			keyValFormat,
   205  			refVar,
   206  			refVal)
   207  
   208  	panic(newMessage + reference + constants.DefaultLine + it.String())
   209  }
   210  
   211  func (it RawErrCollection) HandleErrorWithMsg(newMessage string) {
   212  	if it.IsEmpty() {
   213  		return
   214  	}
   215  
   216  	panic(newMessage + constants.DefaultLine + it.String())
   217  }
   218  
   219  func (it RawErrCollection) FullString() string {
   220  	return it.String()
   221  }
   222  
   223  func (it RawErrCollection) FullStringWithTraces() string {
   224  	return it.String()
   225  }
   226  
   227  func (it RawErrCollection) FullStringWithTracesIf(isStackTraces bool) string {
   228  	return it.String()
   229  }
   230  
   231  func (it RawErrCollection) ReferencesCompiledString() string {
   232  	return it.String()
   233  }
   234  
   235  func (it RawErrCollection) CompiledErrorWithStackTraces() error {
   236  	return it.CompiledError()
   237  }
   238  
   239  func (it RawErrCollection) CompiledStackTracesString() string {
   240  	return it.String()
   241  }
   242  
   243  func (it RawErrCollection) FullStringSplitByNewLine() []string {
   244  	return it.Strings()
   245  }
   246  
   247  func (it RawErrCollection) FullStringWithoutReferences() string {
   248  	return it.String()
   249  }
   250  
   251  func (it RawErrCollection) SerializeWithoutTraces() ([]byte, error) {
   252  	if it.IsEmpty() {
   253  		return nil, nil
   254  	}
   255  
   256  	return json.Marshal(it.Items)
   257  }
   258  
   259  func (it RawErrCollection) Serialize() ([]byte, error) {
   260  	if it.IsEmpty() {
   261  		return nil, nil
   262  	}
   263  
   264  	return json.Marshal(it.Items)
   265  }
   266  
   267  func (it RawErrCollection) SerializeMust() []byte {
   268  	rawBytes, err := it.Serialize()
   269  
   270  	MustBeEmpty(err)
   271  
   272  	return rawBytes
   273  }
   274  
   275  func (it RawErrCollection) MarshalJSON() ([]byte, error) {
   276  	if it.IsEmpty() {
   277  		return nil, nil
   278  	}
   279  
   280  	return json.Marshal(it.Items)
   281  }
   282  
   283  func (it RawErrCollection) UnmarshalJSON(data []byte) error {
   284  	var errItems []error
   285  	err := json.Unmarshal(data, &errItems)
   286  
   287  	if err == nil {
   288  		it.Items = errItems
   289  	}
   290  
   291  	return err
   292  }
   293  
   294  func (it RawErrCollection) Value() error {
   295  	return it.CompiledError()
   296  }
   297  
   298  func (it RawErrCollection) Log() {
   299  	if it.IsEmpty() {
   300  		return
   301  	}
   302  
   303  	fmt.Println(it.String())
   304  }
   305  
   306  func (it RawErrCollection) LogWithTraces() {
   307  	it.Log()
   308  }
   309  
   310  func (it RawErrCollection) LogFatal() {
   311  	if it.IsEmpty() {
   312  		return
   313  	}
   314  
   315  	log.Fatalln(it.String())
   316  }
   317  
   318  func (it RawErrCollection) LogFatalWithTraces() {
   319  	it.LogFatal()
   320  }
   321  
   322  func (it RawErrCollection) LogIf(isLog bool) {
   323  	if isLog {
   324  		it.LogFatal()
   325  	}
   326  }
   327  
   328  func (it RawErrCollection) AddErrors(errs ...error) {
   329  	it.Adds(errs...)
   330  }
   331  
   332  func (it RawErrCollection) ConditionalAddError(isAdd bool, err error) {
   333  	if !isAdd {
   334  		return
   335  	}
   336  
   337  	it.Add(err)
   338  }
   339  
   340  func (it RawErrCollection) CountStateChangeTracker() CountStateChangeTracker {
   341  	return NewCountStateChangeTracker(&it)
   342  }
   343  
   344  func (it RawErrCollection) IsErrorsCollected(
   345  	errorsItems ...error,
   346  ) bool {
   347  	count := it.Length()
   348  
   349  	it.Adds(errorsItems...)
   350  
   351  	return count != it.Length()
   352  }
   353  
   354  func (it RawErrCollection) IsValid() bool {
   355  	return it.IsEmpty()
   356  }
   357  
   358  func (it RawErrCollection) IsSuccess() bool {
   359  	return it.IsEmpty()
   360  }
   361  
   362  func (it RawErrCollection) IsFailed() bool {
   363  	return !it.IsEmpty()
   364  }
   365  
   366  func (it RawErrCollection) IsInvalid() bool {
   367  	return !it.IsEmpty()
   368  }
   369  
   370  func (it RawErrCollection) ToRawErrCollection() *RawErrCollection {
   371  	return &it
   372  }
   373  
   374  func (it *RawErrCollection) Add(err error) {
   375  	if err == nil {
   376  		return
   377  	}
   378  
   379  	it.Items = append(it.Items, err)
   380  }
   381  
   382  func (it *RawErrCollection) AddError(err error) {
   383  	if err == nil {
   384  		return
   385  	}
   386  
   387  	it.Items = append(it.Items, err)
   388  }
   389  
   390  func (it *RawErrCollection) AddWithTraceRef(
   391  	err error,
   392  	traces []string,
   393  	referenceItem interface{},
   394  ) {
   395  	if err == nil {
   396  		return
   397  	}
   398  
   399  	it.Items = append(
   400  		it.Items,
   401  		ErrorWithTracesRefToError(err, traces, referenceItem))
   402  }
   403  
   404  func (it *RawErrCollection) AddWithCompiledTraceRef(
   405  	err error,
   406  	compiledTrace string,
   407  	referenceItem interface{},
   408  ) {
   409  	if err == nil {
   410  		return
   411  	}
   412  
   413  	compiledErr := ErrorWithCompiledTraceRefToError(
   414  		err,
   415  		compiledTrace,
   416  		referenceItem)
   417  
   418  	it.Items = append(
   419  		it.Items,
   420  		compiledErr)
   421  }
   422  
   423  func (it *RawErrCollection) AddWithRef(
   424  	err error,
   425  	referenceItem interface{},
   426  ) {
   427  	if err == nil {
   428  		return
   429  	}
   430  
   431  	compiledErr := ErrorWithRefToError(
   432  		err,
   433  		referenceItem)
   434  
   435  	it.Items = append(
   436  		it.Items,
   437  		compiledErr)
   438  }
   439  
   440  func (it *RawErrCollection) Adds(
   441  	errorItems ...error,
   442  ) {
   443  	if len(errorItems) == 0 {
   444  		return
   445  	}
   446  
   447  	for _, err := range errorItems {
   448  		if err == nil {
   449  			continue
   450  		}
   451  
   452  		it.Items = append(
   453  			it.Items,
   454  			err)
   455  	}
   456  }
   457  
   458  // AddString
   459  //
   460  //  Empty string will be ignored
   461  func (it *RawErrCollection) AddString(
   462  	message string,
   463  ) {
   464  	if message == "" {
   465  		return
   466  	}
   467  
   468  	it.Items = append(
   469  		it.Items,
   470  		errors.New(message))
   471  }
   472  
   473  func (it *RawErrCollection) AddStringSliceAsErr(
   474  	errSliceStrings ...string,
   475  ) {
   476  	if len(errSliceStrings) == 0 {
   477  		return
   478  	}
   479  
   480  	for _, errString := range errSliceStrings {
   481  		if errString == "" {
   482  			continue
   483  		}
   484  
   485  		it.Items = append(it.Items, errors.New(errString))
   486  	}
   487  }
   488  
   489  func (it *RawErrCollection) AddErrorGetters(
   490  	errorGetter ...errorGetter,
   491  ) {
   492  	if len(errorGetter) == 0 {
   493  		return
   494  	}
   495  
   496  	for _, errGetter := range errorGetter {
   497  		if errGetter == nil {
   498  			continue
   499  		}
   500  
   501  		err := errGetter.Error()
   502  
   503  		if err == nil {
   504  			continue
   505  		}
   506  
   507  		it.Items = append(it.Items, err)
   508  	}
   509  }
   510  
   511  func (it *RawErrCollection) AddCompiledErrorGetters(
   512  	errorGetter ...compiledErrorGetter,
   513  ) {
   514  	if len(errorGetter) == 0 {
   515  		return
   516  	}
   517  
   518  	for _, errGetter := range errorGetter {
   519  		if errGetter == nil {
   520  			continue
   521  		}
   522  
   523  		err := errGetter.CompiledError()
   524  
   525  		if err == nil {
   526  			continue
   527  		}
   528  
   529  		it.Items = append(it.Items, err)
   530  	}
   531  }
   532  
   533  func (it *RawErrCollection) Length() int {
   534  	if it == nil {
   535  		return 0
   536  	}
   537  
   538  	return len(it.Items)
   539  }
   540  
   541  func (it *RawErrCollection) IsEmpty() bool {
   542  	return it == nil || len(it.Items) == 0
   543  }
   544  
   545  func (it *RawErrCollection) HasError() bool {
   546  	return it != nil && len(it.Items) > 0
   547  }
   548  
   549  func (it *RawErrCollection) HasAnyError() bool {
   550  	return it != nil && len(it.Items) > 0
   551  }
   552  
   553  func (it *RawErrCollection) Clear() {
   554  	if it.IsEmpty() {
   555  		return
   556  	}
   557  
   558  	tempItems := it.Items
   559  	clearFunc := func() {
   560  		for i := 0; i < len(tempItems); i++ {
   561  			tempItems[i] = nil
   562  		}
   563  	}
   564  
   565  	go clearFunc()
   566  	it.Items = []error{}
   567  }
   568  
   569  func (it *RawErrCollection) Dispose() {
   570  	if it.IsEmpty() {
   571  		return
   572  	}
   573  
   574  	it.Clear()
   575  	it.Items = nil
   576  }
   577  
   578  func (it RawErrCollection) Strings() []string {
   579  	if it.IsEmpty() {
   580  		return []string{}
   581  	}
   582  
   583  	slice := make([]string, it.Length())
   584  
   585  	for i, err := range it.Items {
   586  		slice[i] = err.Error()
   587  	}
   588  
   589  	return slice
   590  }
   591  
   592  func (it RawErrCollection) StringUsingJoiner(joiner string) string {
   593  	if it.IsEmpty() {
   594  		return ""
   595  	}
   596  
   597  	return strings.Join(
   598  		it.Strings(),
   599  		joiner)
   600  }
   601  
   602  func (it RawErrCollection) StringUsingJoinerAdditional(joiner, additionalMessage string) string {
   603  	if it.IsEmpty() {
   604  		return ""
   605  	}
   606  
   607  	return strings.Join(
   608  		it.Strings(),
   609  		joiner) + additionalMessage
   610  }
   611  
   612  func (it RawErrCollection) String() string {
   613  	if it.IsEmpty() {
   614  		return ""
   615  	}
   616  
   617  	return it.StringUsingJoiner(constants.NewLineUnix)
   618  }
   619  
   620  func (it RawErrCollection) CompiledError() error {
   621  	if it.IsEmpty() {
   622  		return nil
   623  	}
   624  
   625  	toString := it.String()
   626  
   627  	return errors.New(toString)
   628  }
   629  
   630  func (it RawErrCollection) CompiledErrorUsingJoiner(joiner string) error {
   631  	if it.IsEmpty() {
   632  		return nil
   633  	}
   634  
   635  	toString := it.StringUsingJoiner(joiner)
   636  
   637  	return errors.New(toString)
   638  }
   639  
   640  func (it RawErrCollection) CompiledErrorUsingJoinerAdditionalMessage(joiner, additionalMessage string) error {
   641  	if it.IsEmpty() {
   642  		return nil
   643  	}
   644  
   645  	toString := it.StringUsingJoinerAdditional(
   646  		joiner,
   647  		additionalMessage)
   648  
   649  	return errors.New(toString)
   650  }
   651  
   652  func (it RawErrCollection) CompiledErrorUsingStackTraces(joiner string, stackTraces []string) error {
   653  	if it.IsEmpty() {
   654  		return nil
   655  	}
   656  
   657  	return ErrorWithTracesRefToError(
   658  		it.CompiledErrorUsingJoiner(joiner),
   659  		stackTraces,
   660  		nil)
   661  }
   662  
   663  func (it RawErrCollection) StringWithAdditionalMessage(additionalMessage string) string {
   664  	if it.IsEmpty() {
   665  		return ""
   666  	}
   667  
   668  	return it.StringUsingJoiner(constants.NewLineUnix) + additionalMessage
   669  }