package errlist

import (
	"fmt"
	"strings"
)

type ErrList struct {
	errList []error
}

func New() *ErrList {
	return &ErrList{}
}

func (l *ErrList) AddErr(err error) *ErrList {
	if err != nil {
		l.errList = append(l.errList, err)
	}
	return l
}

func (l *ErrList) GetErrListAsOne() (err error) {
	errLen := len(l.errList)
	if errLen > 0 {
		if errLen == 1 {
			err = l.errList[0]
		} else {
			errStrList := make([]string, errLen)
			for k, v := range l.errList {
				errStrList[k] = v.Error()
			}
			err = fmt.Errorf("总共%d个错误，详情如下：\n%s", errLen, strings.Join(errStrList, ",\n"))
		}
	}
	return err
}
