예제 #1
0
// Combined returns a combined error from the set. If there is only one error, it is returned unmolested. If there are
// more, they are all "flattened" into a single error. Where codes differ, they are normalised to that with the lowest
// index.
func (es ErrorSet) Combined() error {
	switch len(es) {
	case 0:
		return nil

	case 1:
		return es[0]

	default:
		msg := fmt.Sprintf("%s, and %d more errors", es[0].Message, len(es)-1)
		result := terrors.New(es[0].Code, msg, nil)

		params := []map[string]string{}
		for _, err := range es {
			if higherPriority(err.Code, result.Code) {
				result.Code = err.Code
			}
			params = append(params, err.Params)
		}

		result.Params = mergeContexts(params...)
		es.sanitiseContext(result.Params)
		return result
	}
}
예제 #2
0
func (suite *errorSetSuite) TestMultiErrorPriority() {
	br := terrors.BadRequest("missing_param", "foo bar", nil)
	is := terrors.InternalService("something_broke", "hello world", nil)
	suite.Assert().True(higherPriority(is.Code, br.Code))
	se := terrors.New("something_else", "baz", nil)
	suite.Assert().True(higherPriority(is.Code, se.Code))
	suite.Assert().True(higherPriority(br.Code, se.Code))

	es := ErrorSet{se, is, br}
	suite.Assert().Equal(is.Code, es.Combined().(*terrors.Error).Code)
}